java 子类有返回其类的方法

发布于 2024-09-27 07:22:53 字数 1826 浏览 4 评论 0原文

好吧,也许这是一个愚蠢的问题。但我只是想知道这是否可以在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海似她心 2024-10-04 07:22:53

我想这就是你想要的。编译结果如下:

abstract class A {
    public abstract A getA();
}

class B extends A {
    // Declared to return a B, but it still properly overrides A's method
    @Override
    public B getA() {
        return new B();
    }
}

class C extends A {
    // Declared to return a B, but it still properly overrides A's method
    @Override
    public C getA() {
        return new C();
    }
}

如您所见,A 声明 getA() 方法返回一个 A。但是,您可以限制子类中的返回类型,如图所示。

This is what you want I think. The following compiles:

abstract class A {
    public abstract A getA();
}

class B extends A {
    // Declared to return a B, but it still properly overrides A's method
    @Override
    public B getA() {
        return new B();
    }
}

class C extends A {
    // Declared to return a B, but it still properly overrides A's method
    @Override
    public C getA() {
        return new C();
    }
}

As you can see, A declares that the getA() method returns an A. But, you can restrict the return type in subclasses as shown.

活雷疯 2024-10-04 07:22:53

我不确定我是否正确理解你的意图,但我认为内置的 Object.getClass() 方法会做你想要的。给定的类定义为:

public abstract class ParentClass<T> {
  public abstract T getTest();
}

class SubClassString extends ParentClass<String> {
   public String getTest() {
      return "";
   }
}

class SubClassInteger extends ParentClass<Integer> {
   public Integer getTest() {
      return Integer.valueOf(0);
   }
}

getClass() 将返回正确的运行时类

  public static void main(String[] args) {
      SubClassString subString = new SubClassString();
      // displays "class SubClassString"
      System.out.println(subString.getClass()); 

      SubClassInteger subInteger = new SubClassInteger();
      // displays "class SubClassInteger"
      System.out.println(subInteger.getClass());

      ParentClass<?> parentInstance = new SubClassInteger();
      // displays "class SubClassInteger"
      System.out.println(parentInstance.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:

public abstract class ParentClass<T> {
  public abstract T getTest();
}

class SubClassString extends ParentClass<String> {
   public String getTest() {
      return "";
   }
}

class SubClassInteger extends ParentClass<Integer> {
   public Integer getTest() {
      return Integer.valueOf(0);
   }
}

getClass() will return the correct run-time class

  public static void main(String[] args) {
      SubClassString subString = new SubClassString();
      // displays "class SubClassString"
      System.out.println(subString.getClass()); 

      SubClassInteger subInteger = new SubClassInteger();
      // displays "class SubClassInteger"
      System.out.println(subInteger.getClass());

      ParentClass<?> parentInstance = new SubClassInteger();
      // displays "class SubClassInteger"
      System.out.println(parentInstance.getClass());  
  }
单身情人 2024-10-04 07:22:53

我能想到的唯一方法是在扩展子类时告诉父类子类是什么(就像您对“T”所做的那样)。例如:

public abstract class ParentClass<T,U> { 
    abstract public T getTest();
    abstract public U getItClassObject();
}

他们像这样定义你的子类:

public class Sub1Class extends ParentClass<Object1,Sub1Class> {
    public Object1 getTest() { }
    public Sub1Class getItClassObject() { }
}

然后你可以做你想做的事情而无需类型转换:

Sub1Class sub1Class = new Sub1Class();
Sub1Class after1Cast = sub1Class.getItClassObject();

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:

public abstract class ParentClass<T,U> { 
    abstract public T getTest();
    abstract public U getItClassObject();
}

They you define your subclass like so:

public class Sub1Class extends ParentClass<Object1,Sub1Class> {
    public Object1 getTest() { }
    public Sub1Class getItClassObject() { }
}

Then you can do what you want without the typecast:

Sub1Class sub1Class = new Sub1Class();
Sub1Class after1Cast = sub1Class.getItClassObject();
云醉月微眠 2024-10-04 07:22:53

如果您的对象具有无参数构造函数(或所有对象具有某种一致形式的构造函数),则可以使用反射来执行此操作。一些伪代码是

public class MyClass {

    public MyClass instantiateType() {
       Class<?> actualClass = getClass();
       return actualClass.newInstance();
    }

}

“这是使用类的运行时类型,因此子类将返回它们的类型”。但这仅适用于无参数构造函数。

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

public class MyClass {

    public MyClass instantiateType() {
       Class<?> actualClass = getClass();
       return actualClass.newInstance();
    }

}

This is using the runtime type of the class, so subclasses will return their type. This works only for a no-arg constructor though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文