java 子类有返回其类的方法
好吧,也许这是一个愚蠢的问题。但我只是想知道这是否可以在java中完成。
abstract public class ParentClass<T> {
abstract public T getTest();
}
在子类中
public class SubClass extends ParentClass<MyObject> {
public MyObject getTest() {
// I can return the object with class MyObject
return null;
}
}
我的问题是我可以在子方法中返回类类型吗?我的意思是,是否可以通过在 ParentClass 中添加一些代码来完成,这样我就可以在下面执行此操作?
例如
public class Sub1Class extends parentClass<Object1> {
public Object1 getTest() { }
// I want to have a method that return it's class in the superclass
public Sub1Class getItClassObject() { }
}
另一个例子
public class Sub2Class extends parentClass<Object2> {
public Object2 getTest() { }
// I want to have a method that return it's class in the superclass
public Sub2Class getItClassObject() { }
}
,
public class Sub3Class extends parentClass<Object3> {
public Object3 getTest() { }
// I want to have a method that return it's class in the superclass
public Sub3Class getItClassObject() { }
}
如果你看到,Sub1Class、Sub2Class 和 Sub3Class 中的 getItClassObject 方法将遵循它的类。但我不想为每个子类添加相同的方法,只想在 ParentClass 中添加一些代码(如果可行),因此在子类中,我只需直接调用 getItClassObject 即可,而无需在每个子类中编写所有代码。
通常我会像这样在 ParentClass 中添加方法。
abstract public class ParentClass<T> {
abstract public T getTest();
public Object getItClassObject() { }
}
所以在子类中我只是实例化该类,但我必须再次转换:(
Sub1Class sub1Class = new Sub1Class();
Sub1Class after1Cast = (Sub1Class) sub1Class.getItClassObject();
Sub2Class sub2Class = new Sub2Class();
Sub2Class after2Cast = (Sub2Class) sub2Class.getItClassObject();
我认为它不能在java中完成。但我不知道是否有解决这个问题的线索。谢谢
Ok, maybe this is a stupid question. But i'm just wondering if this can be done in java.
abstract public class ParentClass<T> {
abstract public T getTest();
}
in the subclass
public class SubClass extends ParentClass<MyObject> {
public MyObject getTest() {
// I can return the object with class MyObject
return null;
}
}
My question is can I return the class type in the child method? I mean, is it can be done by adding some code in the ParentClass, so I can do this below?
For example
public class Sub1Class extends parentClass<Object1> {
public Object1 getTest() { }
// I want to have a method that return it's class in the superclass
public Sub1Class getItClassObject() { }
}
other example
public class Sub2Class extends parentClass<Object2> {
public Object2 getTest() { }
// I want to have a method that return it's class in the superclass
public Sub2Class getItClassObject() { }
}
one example again
public class Sub3Class extends parentClass<Object3> {
public Object3 getTest() { }
// I want to have a method that return it's class in the superclass
public Sub3Class getItClassObject() { }
}
if you see, method getItClassObject in Sub1Class, Sub2Class and Sub3Class will follow it's class. But I don't want to add same method for every subclass, just want to add some code (if feasible) in the ParentClasss, so in the subclass, I just can call getItClassObject directly without write all the code in every subclass.
Usually I add method in ParentClass like this.
abstract public class ParentClass<T> {
abstract public T getTest();
public Object getItClassObject() { }
}
so in the subclass I just instance the class, but I have to cast again :(
Sub1Class sub1Class = new Sub1Class();
Sub1Class after1Cast = (Sub1Class) sub1Class.getItClassObject();
Sub2Class sub2Class = new Sub2Class();
Sub2Class after2Cast = (Sub2Class) sub2Class.getItClassObject();
I think it cannot be done in java. But I don't know if there is a clue to solve this. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想这就是你想要的。编译结果如下:
如您所见,A 声明
getA()
方法返回一个A
。但是,您可以限制子类中的返回类型,如图所示。This is what you want I think. The following compiles:
As you can see, A declares that the
getA()
method returns anA
. But, you can restrict the return type in subclasses as shown.我不确定我是否正确理解你的意图,但我认为内置的 Object.getClass() 方法会做你想要的。给定的类定义为:
getClass() 将返回正确的运行时类
I'm not sure if I understand your intent correctly, but I think the built-in Object.getClass() method will do what you want. Given classes defined as:
getClass() will return the correct run-time class
我能想到的唯一方法是在扩展子类时告诉父类子类是什么(就像您对“T”所做的那样)。例如:
他们像这样定义你的子类:
然后你可以做你想做的事情而无需类型转换:
The only way I can think of is by telling the parent class what the subclass is when you extend it (just like you did with 'T'). Eg:
They you define your subclass like so:
Then you can do what you want without the typecast:
如果您的对象具有无参数构造函数(或所有对象具有某种一致形式的构造函数),则可以使用反射来执行此操作。一些伪代码是
“这是使用类的运行时类型,因此子类将返回它们的类型”。但这仅适用于无参数构造函数。
If your objects have no-arg constructors (or some consistent form of constructor across all of them), you can use reflection to do it. Some pseudocode would be
This is using the runtime type of the class, so subclasses will return their type. This works only for a no-arg constructor though.