简体   繁体   English

重载采用基类并指导扩展类的方法实现的方法

[英]To overload a method taking a base class and directing the extending classes method implementation

The logic below is all three classes AppleLeaf, MangoLeaf and BananaLeaf extend Leaf class. 下面的逻辑是AppleLeaf,MangoLeaf和BananaLeaf扩展Leaf类的所有三个类。 I have a method that takes a leaf object only. 我有一个只接受叶对象的方法。 It should redirect me to the methods below based on the class type correctly. 它应该根据类类型正确地将我重定向到下面的方法。 But I get error like, "The method handleLeaf(AppleLeaf) in the type main is not applicable for the arguments (Leaf)" 但是我收到错误消息,例如“类型main中的handleLeaf(AppleLeaf)方法不适用于参数(Leaf)”

public void main(String args[]){

      for (Leaf leaf : leafList) {
      handleLeaf(leaf);     
   }
}

private void handleLeaf(AppleLeaf node) {   
    String colour = leaf.getColour();
    System.print.out(colour);
}

private void handleLeaf(MangoLeaf mLeaf) {
     Int veins = mLeaf.getVeinCount;
     System.print.out(veins);
}

 private void handleLeaf(BananaLeaf bLeaf) {    
    Boolean withered = bLeaf.getTexture();
}

That's not quite how it works. 这不是很有效。 You've defined three methods, none of which take Leaf as a parameter. 您已经定义了三种方法,没有一种以Leaf为参数。 Each one takes a subclass of Leaf , but we don't know which subclass of Leaf the given argument might be. 每个对象都有一个Leaf的子类,但是我们不知道给定参数可能是Leaf的子类。

One workaround is to make a delegating method such as: 一种解决方法是制作一种委托方法,例如:

private void handleLeaf(Leaf leaf) {
    if(leaf instanceof AppleLeaf)
        handleLeaf((AppleLeaf)leaf);
    else if(leaf instanceof MangoLeaf)
        handleLeaf((MangoLeaf)leaf);
    ...//And so on
}

You may consider reworking your logic in general, however. 但是,您可能会考虑重新构建逻辑。 Perhaps you can flip things around to define an abstract method in the Leaf class and have every leaf type handle itself in its own definition of the method. 也许您可以四处翻转以在Leaf类中定义一个抽象方法,并使每种叶子类型在其自己的方法定义中进行处理。

You cannot upcast automatically like that. 您不能像这样自动上架。 What you're looking for is dynamic method binding, which is not in Java. 您正在寻找的是动态方法绑定,这不是Java中的。 Java is a compiletime linked language, and hence, it will not compile/run. Java是一种编译时链接的语言,因此它将无法编译/运行。

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

相关问题 在java中扩展包含抽象泛型方法的基类 - Extending base class containing abstract generic method in java 可以使用默认方法实现来在基类中实现抽象方法 - Can a default method implementation be used to implement an abstract method in a base class 查看一个抽象类的方法是否未被其中一个扩展类覆盖的方法 - Way to see if a method of an abstract class is not overridden by one of the extending classes 在继承类重写方法中使用基类方法值 - Using Base Class Method Value In Inherited Classes Overridden Method 关于从基类扩展的类的问题(Java) - Questions on classes extending from a base class (Java) 无法打开从Eclipse中的基类添加到接口的方法的实现 - Failed to open implementation for method added to interface from base class in Eclipse 在基类中编写一个方法实现,需要在子类中重写? - Write a method implementation in base class which requires override in subclasses? 如何在Java中使用匿名类重载方法? - How to overload a method using anonymous classes in Java? 抽象类实现接口,接口方法的实现可以确定扩展抽象超类的子类吗? - Abstract class implements interface, can implementation of interface method determine subclass extending abstract super class? 如何在不更改所有派生类的情况下调用基 class 方法 - How to call a base class method without changing all derived classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM