关于从基数 class (Java) 延伸的 classes 的问题
Questions on classes extending from a base class (Java)
我是 Java 的初学者,正在尝试为我目前正在编写的游戏编写聚会任务系统,我有几个问题希望得到解答。我已经问过其他人了,但他们不熟悉 Java。
过去我曾尝试制作一堆 classes 并使用多个 get 方法访问它们。我发现写起来非常乏味,并认为我可以将它们统一在一个抽象 class/implemented class 下。因此,代码看起来更像这样......
DynastyPQInterface pq = new CustomPQ // or ....
DynastyPQInterface pq = new OtherCustomPQ
当然,这带来了困难,例如只能使用已实现的方法。它不允许我访问我以后可能想使用的 class' 独有方法。
最终,我想要做的是能够对 return 这些派生的 class 中的任何一个使用单一的 get 方法,但仍然保留普遍使用 get 的能力方法来调用它们共有的方法,例如 execute、create、end,同时允许我专门接触到它们独有的方法。有没有办法做到这一点,或者这是不可能的?
如果还是不清楚...
我现在写的代码是一个基础class,它以...
的方式扩展到其他classes
DynastyPQ (base) -> methods include (run(), execute(), end())
CustomAPQ (inherited from DynastyPQ) -> (has exclusive methods like getPoints())
CustomBPQ (inherited from DynastyPQ) -> (has exclusive methods like revivePlayer())
我想写一个get方法来摆脱多重。我现在拥有的是...
DynastyPQ dynastyPQ;
DynastyPQ getPQ() {
return dynastyPQ;
}
void setPQ(DynastyPQ pq) {
dynastyPQ = pq;
}
这样做...
DynastyPQ pq = new CarnivalPQ();
我只能访问DynastyPQ的方法,不能访问Carnival的方法
有没有一种方法可以在不考虑 class 的类型的情况下普遍执行四个基本函数的同时访问独占方法,还是我之前错过了什么?
tl;dr -> 我想要一个普遍 return 继承自 class X 的所有 classes 的方法;但是,我希望能够访问每个 class 的独有方法。
您可以将对象转换为派生的 class:
DynastyPQ pq = new CustomAPQ();
((CustomAPQ)pq).customAPQmethod();
如果你不知道什么是动态类型(你在new运算符之后使用的类型),你可以使用instanceof关键字:
DynastyPQ pq = getPQ();
if (pq instanceof CustomAPQ) {
CustomAPQ a = (CustomAPQ)pq;
a.customAPQmethod();
} else if (pq instanceof CustomBPQ) {
CustomBPQ b = (CustomBPQ)pq;
b.customBPQmethod();
} else {
// Neither a CustomAPQ nor a CustomBPQ.
}
如果你不想那样做,你可以使用多态:
class DynastyPQ {
final void run() {
// code.
}
final void execute() {
// code.
}
final void create() {
// code.
}
void specific1() {}
void specific2() {}
}
class CustomAPQ extends DynastyPQ {
@Override
void specific1() {
// do stuff specific to CustomAPQ.
}
@Override
void specific2() {
// do stuff specific to CustomAPQ.
}
}
class CustomBPQ extends DynastyPQ {
@Override
void specific1() {
// do stuff specific to CustomBPQ.
}
@Override
void specific2() {
// do stuff specific to CustomBPQ.
}
}
现在,您可以:
DynastyPQ pq = new CustomAPQ();
pq.specific1();
被调用的方法将是CustomAPQ::specific1()
。如果 specific1()
没有在 CustomAPQ
中声明,那么,它什么都不做。
除了@CelineNOEL 表示这是不可能的。因为您声明了 DynastyPQ 类型的 class,所以您只能调用在 class 中定义的方法。在您想调用特定方法而不是共享方法的那一刻,您知道它来自哪个 class 并且您可以使用转换来调用该特定方法。
((CustomAPQ)pq).customAPQmethod()
您在代码中使用的共享方法,当您不知道哪个 class 应该执行相同的和平代码时(或者如果您在每个子 class),然后您委托它在运行时解决。因此,请重新考虑您的设计,并在基础 class 中放置需要动态调用的方法。您确定的所有其他方法都是特定于一个 class 的,只放在那个 class 中。这样你的代码会更干净,你不会弄乱应该分开的东西。
我是 Java 的初学者,正在尝试为我目前正在编写的游戏编写聚会任务系统,我有几个问题希望得到解答。我已经问过其他人了,但他们不熟悉 Java。
过去我曾尝试制作一堆 classes 并使用多个 get 方法访问它们。我发现写起来非常乏味,并认为我可以将它们统一在一个抽象 class/implemented class 下。因此,代码看起来更像这样......
DynastyPQInterface pq = new CustomPQ // or ....
DynastyPQInterface pq = new OtherCustomPQ
当然,这带来了困难,例如只能使用已实现的方法。它不允许我访问我以后可能想使用的 class' 独有方法。
最终,我想要做的是能够对 return 这些派生的 class 中的任何一个使用单一的 get 方法,但仍然保留普遍使用 get 的能力方法来调用它们共有的方法,例如 execute、create、end,同时允许我专门接触到它们独有的方法。有没有办法做到这一点,或者这是不可能的?
如果还是不清楚...
我现在写的代码是一个基础class,它以...
的方式扩展到其他classesDynastyPQ (base) -> methods include (run(), execute(), end())
CustomAPQ (inherited from DynastyPQ) -> (has exclusive methods like getPoints())
CustomBPQ (inherited from DynastyPQ) -> (has exclusive methods like revivePlayer())
我想写一个get方法来摆脱多重。我现在拥有的是...
DynastyPQ dynastyPQ;
DynastyPQ getPQ() {
return dynastyPQ;
}
void setPQ(DynastyPQ pq) {
dynastyPQ = pq;
}
这样做...
DynastyPQ pq = new CarnivalPQ();
我只能访问DynastyPQ的方法,不能访问Carnival的方法
有没有一种方法可以在不考虑 class 的类型的情况下普遍执行四个基本函数的同时访问独占方法,还是我之前错过了什么?
tl;dr -> 我想要一个普遍 return 继承自 class X 的所有 classes 的方法;但是,我希望能够访问每个 class 的独有方法。
您可以将对象转换为派生的 class:
DynastyPQ pq = new CustomAPQ();
((CustomAPQ)pq).customAPQmethod();
如果你不知道什么是动态类型(你在new运算符之后使用的类型),你可以使用instanceof关键字:
DynastyPQ pq = getPQ();
if (pq instanceof CustomAPQ) {
CustomAPQ a = (CustomAPQ)pq;
a.customAPQmethod();
} else if (pq instanceof CustomBPQ) {
CustomBPQ b = (CustomBPQ)pq;
b.customBPQmethod();
} else {
// Neither a CustomAPQ nor a CustomBPQ.
}
如果你不想那样做,你可以使用多态:
class DynastyPQ {
final void run() {
// code.
}
final void execute() {
// code.
}
final void create() {
// code.
}
void specific1() {}
void specific2() {}
}
class CustomAPQ extends DynastyPQ {
@Override
void specific1() {
// do stuff specific to CustomAPQ.
}
@Override
void specific2() {
// do stuff specific to CustomAPQ.
}
}
class CustomBPQ extends DynastyPQ {
@Override
void specific1() {
// do stuff specific to CustomBPQ.
}
@Override
void specific2() {
// do stuff specific to CustomBPQ.
}
}
现在,您可以:
DynastyPQ pq = new CustomAPQ();
pq.specific1();
被调用的方法将是CustomAPQ::specific1()
。如果 specific1()
没有在 CustomAPQ
中声明,那么,它什么都不做。
除了@CelineNOEL 表示这是不可能的。因为您声明了 DynastyPQ 类型的 class,所以您只能调用在 class 中定义的方法。在您想调用特定方法而不是共享方法的那一刻,您知道它来自哪个 class 并且您可以使用转换来调用该特定方法。
((CustomAPQ)pq).customAPQmethod()
您在代码中使用的共享方法,当您不知道哪个 class 应该执行相同的和平代码时(或者如果您在每个子 class),然后您委托它在运行时解决。因此,请重新考虑您的设计,并在基础 class 中放置需要动态调用的方法。您确定的所有其他方法都是特定于一个 class 的,只放在那个 class 中。这样你的代码会更干净,你不会弄乱应该分开的东西。