简体   繁体   English

Java 中是否有办法根据 object 的子类调用不同的子方法?

[英]Is there a way in Java to call different child method depending on which subclass is the object?

In my project I have a superclass and two subclasses extending from it.在我的项目中,我有一个超类和两个从它扩展的子类。 There is a method in the superclass that is overriden differently in each subclass.超类中有一个方法在每个子类中被不同地覆盖。 I want to know if it's possible to introduce a method (in another class) that takes object of either subclass as a parameter and calls a method overriden in one of subclasses (depending on to which subclass does the object belong).我想知道是否可以引入一种方法(在另一个类中),该方法将任一子类的 object 作为参数并调用在其中一个子类中覆盖的方法(取决于 object 属于哪个子类)。

public class Superclass{
    public int method(){return 0;}
}
public class Subclass1 extends Superclass{
    public int method(){return 1;}
}
public class Subclass2 extends Superclass{
    public int method(){return 2;}
}
public class CallingClass{
    public static int dependantCall(Superclass parameter){return parameter.method}

I want to be able to do something like我希望能够做类似的事情

Subclass1 subclassObject = new Subclass1;
System.out.println(CallingClass.dependantCall(subclassObject));

and get output并得到 output

1

That is what Polymorphism is for.这就是多态性的用途。 Defining the Superclass as a parameter type will allow you to pass either subclass in.将超类定义为参数类型将允许您传入任一子类。

For example in your other class you can define it like this:例如,在您的其他 class 中,您可以像这样定义它:

// classes Dog and Cat extend Animal and override makeNoise()
class Owner{

  playWith(Animal a){
    a.makeNoise();
  }

}

Now the Owner can accept owner.makeNoise(cat) and owner.makeNoise(dog)现在 Owner 可以接受 owner.makeNoise(cat) 和 owner.makeNoise(dog)

More reading: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html更多阅读: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html

Yes, it is entirely possible.是的,这是完全可能的。 Here's how that method would look like:该方法如下所示:

public <T extends Superclass> void foo(T subclassObject) {
...
} 

Or:或者:

public void foo(Superclass obj) {
...
}

Note that in the above method, you can pass subclasses' objects as well (they are covariant data types).请注意,在上述方法中,您也可以传递子类的对象(它们是协变数据类型)。

This is what Java does by default when you create subclases, so no need to do anything special.这是 Java 在创建子类时默认执行的操作,因此无需做任何特别的事情。 Each object carries it's type information at run time, and the method invoked would always be the most specific one for the object.每个 object 在运行时都携带其类型信息,调用的方法始终是 object 最具体的方法。 Example:例子:

public class Doer {
  public void doSomething() {
    // Body presence
  };
}

public class Painter extends Doer {
  @Override
  public void doSomething() {
    // Paint here
  }
}

public class Manager extends Doer {
  @Override
  public void doSomething() {
    // Micromanage here
  }
}

// Elsewhere in your code: 
public void busyness(Doer doer) {
   doer.doSomething();
}

A style note: if it is possible, one should prefer using interfaces instead of base classes (base classes those should be used only if you want to share implementation between subclasses).风格说明:如果可能的话,应该更喜欢使用接口而不是基类(只有当你想在子类之间共享实现时才应该使用基类)。 Example with interfaces:接口示例:

public interface Doer {
  void doSomething();
}

public class JackOfAllTrades implements Does {
  @Override
  public void doSomething() {
     // Do whatever necessary
  }
}

// Client code stays exactly the same as above: 
public void busyness(Doer doer) {
   doer.doSomething();
}

Note that in Java a class can have only one base class but can implement multiple interfaces.请注意,在 Java 中,class 只能有一个基础 class,但可以实现多个接口。

@Override annotations are not strictly required, but they help Java compiler to spot some errors for you (eg if you misprint method name). @Override注释不是严格要求的,但它们可以帮助 Java 编译器为您发现一些错误(例如,如果您打印错误的方法名称)。

In your example it would look like在您的示例中,它看起来像

public class CallingClass {
  public static int dependantCall(Superclass parameter) {
    return parameter.method();
  }
}

Subclass1 subclassObject = new Subclass1();
System.out.println(CallingClass.dependantCall(subclassObject));

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

相关问题 如何调用传递给JNI`jobject`的Java Object的子类/子类的方法 - How to call a method of a subclass / child class of an Java Object passed to JNI `jobject` 根据Java中的对象类型调用子类的方法 - Call Subclasses' Method Depending on Object Type in Java 在Java中调用子类的方法 - Call a method of subclass in Java 有什么方法可以通过子类对象调用超类的方法,而不必通过同名的子类方法吗? - Is there any way to call superclass's method on a subclass object without going through the subclass method of the same name? 允许Java方法调用参数对象的子类的方法吗? - Allowing a Java method to call methods of a parameter object's subclass? Java多态性如何调用子类对象的超类方法 - Java Polymorphism How to call to super class method for subclass object JAVA:如何在Object的子类中调用方法? - JAVA: How to call a method in a child class of an Object? 如何调用子类方法(Java) - How to call subclass method (Java) 在Java中,有没有更简单的方法可以在子类和超类的ArrayList上调用子类的方法 - Is there an easier way to call a subclass's method on an ArrayList of both the subclass and superclass in Java 具有不同方法参数的Java子类 - Java subclass with different method parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM