java 如何在非抽象类中声明抽象方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35133900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare abstract method in non-abstract class
提问by dabadaba
I want to declare a couple of abstract methods (so the implementation is required in the classes that inherit from this one) to fit my situation, which is:
我想声明几个抽象方法(因此从这个继承的类中需要实现)以适应我的情况,即:
I am making a puzzles solver program. So far I have 3 packages:
我正在制作一个谜题解决程序。到目前为止,我有 3 个包:
games.puzzles
games.puzzles.rivercrossing
games.puzzles.rivercrossing.wolfgoatcabbage
games.puzzles
games.puzzles.rivercrossing
games.puzzles.rivercrossing.wolfgoatcabbage
I don't want to get too specific but in the games.puzzles.rivercrossing
package I have two classes that represent a bank and a state: GenericBank
and GenericState
.
我不想太具体,但在games.puzzles.rivercrossing
包中我有两个类代表银行和州:GenericBank
和GenericState
.
Now, they define some behavior, but there are some methods that the classes that inherit from these must have, like move()
to move one element from one bank to the other or isPermitted()
and isFinal()
to check the states.
现在,他们定义一些行为,但也有一些方法的类,从这些继承必须有,像move()
一个元素从一个银行移动到另一个或isPermitted()
和isFinal()
检查的状态。
For example, in the last package I have the WolfGoatCabbageGame
class and it must have its own Bank
and State
classes which will inherit from the generic ones. These particular Bank
and State
classes must implement the methods I mentioned above, for example in the Wolf, Goat and Cabbage game, to check if the goat and the wolf are not in the same bank, etc.
例如,在过去的包我有WolfGoatCabbageGame
类,它必须有自己Bank
和State
班将从一般的人继承。这些特定的Bank
和State
类必须实现我上面提到的方法,例如在 Wolf, Goat and Cabbage 游戏中,检查山羊和狼是否不在同一个银行等。
So initially I declared the generic classes as abstract
, and these methods to be implemented abstract
as well:
所以最初我将泛型类声明为abstract
,并且这些方法也将被实现abstract
:
public abstract class GenericBank {
// more members ...
public abstract boolean move(Element element, GenericBank dst);
// more members...
}
public abstract class GenericState {
// more members...
public abstract boolean isPermitted(GenericBank bank);
public abstract boolean isFinal(GenericBank bank);
// more members...
}
And this looked like it'd work until I found out I had to instantiate GenericBank
and GenericState
objects, which of course can't be done if these classes are abstract.
这看起来是可行的,直到我发现我必须实例化GenericBank
和GenericState
对象,如果这些类是抽象的,这当然无法完成。
So I had to remove the abstract
qualifier from the classes.
所以我不得不abstract
从类中删除限定符。
So... what can I do? How can I declare abstract
methods (or achieve the same behavior) in a non-abstract
class?
所以……我能做什么?如何abstract
在非abstract
类中声明方法(或实现相同的行为)?
回答by Draco18s no longer trusts SE
How to declare abstract method in non-abstract class?
如何在非抽象类中声明抽象方法?
Answer: You can't. It's kind of the definition of abstract.It's the same reason you can't instantiate an object as an abstract class.
回答:你不能。这是抽象的定义。这与您不能将对象实例化为抽象类的原因相同。
Either:
任何一个:
A) You need to use Interfaces
A)你需要使用接口
B) Leave the methods empty in the parent class:
B) 将父类中的方法留空:
//technically this needs to return a value, but it doesn't need to *do* anything
public boolean isPermitted(GenericBank bank){ return false; }
C) Refactor your code so that you aren't instantiating abstract objects. I cannot advise how to do this as you haven't provided any code regarding this.
C) 重构你的代码,这样你就不会实例化抽象对象。我无法建议如何执行此操作,因为您尚未提供任何相关代码。
回答by toKrause
You could replace the abstract methods with empty methods that do nothing and return the default value of their respective return type (and, if necessary, make it part of the generic classes contract, that subclasses must override these methods).
您可以用空方法替换抽象方法,这些方法什么也不做,并返回其各自返回类型的默认值(并且,如有必要,使其成为泛型类合同的一部分,子类必须覆盖这些方法)。
Alternatively, you could keep your abstract Generic*
-classes and add Null*
-classes with abovementioned empty implementations, following the Null object pattern.
或者,您可以按照Null 对象模式保留抽象Generic*
类并添加Null*
具有上述空实现的类。
回答by Waleed A.K.
You can use Virtualinstead!
您可以改用虚拟!
internal class ClassA
{
public void Print()
{
Console.WriteLine("A");
PrintVirtual();
Console.WriteLine("--------------------------------------------------");
}
protected virtual void PrintVirtual()
{
Console.WriteLine("Virtual");
}
}
internal class ClassB : ClassA
{
protected override void PrintVirtual()
{
Console.WriteLine("B");
}
}
internal class ClassC : ClassA
{
protected override void PrintVirtual()
{
Console.WriteLine("C");
base.PrintVirtual();
}
}
and you can run the test
你可以运行测试
new ClassA().Print();
new ClassB().Print();
new ClassC().Print();
回答by Mena
You cannot declare abstract
methods in a non-abstract class, final dot.
您不能abstract
在非抽象类、final 点中声明方法。
That would simply defile the concept of abstract
methods.
那只会玷污abstract
方法的概念。
What you cando is have your class hierarchy implement interfaces dictating the required methods to implement.
您可以做的是让您的类层次结构实现接口,指示要实现的所需方法。
If you found your formerly abstract
classes were actually better designed as concrete classes, do convert them to concrete classes and implement the methods, even with a default, general implementation.
如果您发现以前的abstract
类实际上更适合作为具体类设计,请务必将它们转换为具体类并实现这些方法,即使使用默认的通用实现也是如此。
You can then fine-tune the overrides in your child classes.
然后,您可以微调子类中的覆盖。
回答by S.L. Barth - Reinstate Monica
You cannot, the very definition of an abstract class is that it has abstract methods.
你不能,抽象类的定义就是它有抽象方法。
What you can do, is define default behaviour, that can be overruled by subclasses.
您可以做的是定义默认行为,可以被子类否决。
However, I would carefully consider your class hierarchy before doing this. The fact that you need to instantiate some classes before their actual implementations are known, suggests that your design may need re-thinking.
但是,在执行此操作之前,我会仔细考虑您的类层次结构。您需要在了解某些类的实际实现之前对其进行实例化,这一事实表明您的设计可能需要重新思考。
If you're going to re-design, you will want to look at the time of instantiation - and underlying that, the reasons for instantiating.
Right now, you want to use some of the common behaviour of a class, before the actual instance of that class is known.
如果您要重新设计,您将需要查看实例化的时间 - 以及在此基础上实例化的原因。
现在,您想在知道该类的实际实例之前使用该类的一些常见行为。
It goes a bit beyond the scope of answering the question, but: consider explaining the design of the code to a friend. Or to a rubber duck. This may help you to find a fresh approach.
这有点超出了回答问题的范围,但是:考虑向朋友解释代码的设计。或者给一只橡皮鸭。这可能会帮助您找到一种新的方法。
回答by yugo
Remove the abstract qualifier and add a empty body, or throwing some runtime exception.
删除抽象限定符并添加一个空体,或者抛出一些运行时异常。
Or instantiate these generic classes as anonymous sub classes
或者将这些泛型类实例化为匿名子类