简体   繁体   English

从具体类派生的抽象类

[英]Abstract class derived from concrete class

I've just got a warning in my IDE that my class is abstract but it is derived from concrete class. 我在IDE中刚刚得到警告,我的类是abstract但它是从concrete类派生的。 And what? 那又怎样 Why can't I do so? 我为什么不能这样做? This suites my needs very well. 这非常适合我的需求。 It's just a warning though so the class is compilable. 尽管这只是一个警告,所以该类是可编译的。 Just interested why it gives me this warning. 只想知道为什么它会给我这个警告。 Thank you. 谢谢。

Update 更新资料

I need to extend my question with some explanations. 我需要用一些解释来扩展我的问题。 I have this class derived from other class and I want nobody to be able to instantiate it. 我有从其他类派生的此类,并且我希望没人能实例化它。 Moreover, I need two subclasses from this abstract class and each of them to have their own implementation of one abstract method. 而且,我需要这个抽象类中的两个子类,并且每个子类都有自己的一个抽象方法的实现。 That's why. 这就是为什么。

Do you think it is bad idea? 您认为这是个坏主意吗? What approach shall I take instead? 我应该采取什么方法呢?

I think the reason is maybe that abstract classes usually serve for basic behavior/information for creating subclasses. 我认为原因可能是抽象类通常用于创建子类的基本行为/信息。 Think about other programmers while making such a decision. 在做出这样的决定时,请考虑其他程序员。 Others don't expect it. 其他人没想到。

Anyway, you can set compiler preferences not to give warnings for this type of "problem". 无论如何,您可以设置编译器首选项,以不针对此类“问题”发出警告。

Update 更新资料

After reading your update I think the simplest way is to declare this class's constructor protected . 阅读您的更新后,我认为最简单的方法是将此类的constructor protected方法声明为constructor protected For the abstract method: you can simply override that from your subclasses, I think. 对于抽象方法:我认为您可以从子类中简单地override它。

You can do that. 你可以做到的。 That's why you got a warning and not an error. 这就是为什么您得到警告而不是错误的原因。

This is a common case and typically points to bad system architecture. 这是一种常见情况,通常会指出系统体系结构不佳。 That's why the compiler shows a warning. 这就是编译器显示警告的原因。

Java compiler warnings are just an indication to the programmer that shouldn't be done normally otherwise the programmer may get bugs due to it in future. Java编译器警告只是对程序员的指示,不应正常执行该操作,否则程序员将来可能会因此而收到错误。

See this link as it explains why to bother about warnings 看到此链接,因为它解释了为什么要打扰警告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM