简体   繁体   English

Java-超类和子类中的不同方法

[英]Java - Different methods in superclass and subclass

I'm a bit confused with inheritance/interfaces in Java/Android and am not sure if I'm on the right track. 我对Java / Android中的继承/接口有点困惑,不确定我是否走对了。 Basically I'm using Parcelable in Android and am getting errors because methods are undefined. 基本上,我在Android中使用的是Parcelable,并且由于方法未定义而出现错误。

I've got an Animal superclass, and a few subclasses (Dog, cat etc). 我有一个动物超类,还有几个子类(狗,猫等)。 On the first activity you select an animal then it parcels it up and passes it to the second activity: 在第一个活动中,选择一个动物,然后将其打包并将其传递给第二个活动:

/* Second activity */
Animal myAnimal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_current_status);

    Intent intent = getIntent();
    myAnimal = intent.getParcelableExtra("animal"); // Gets the animal subclass (dog, cat etc) from the previous activity
    myAnimal.updateImage(myAnimal.getCurrentState());
}

The problem is 'updateImage' only exists in the subclass, therefore it won't compile here. 问题是“ updateImage”仅存在于子类中,因此不会在此处编译。 I don't want this method to be in the superclass, because the output varies depending on the type of animal (eg picture of a dog). 我不希望这种方法属于超类,因为输出会根据动物的类型(例如狗的图片)而变化。

I also don't know which type of animal it is going to be, so I can't define the variable as anything other than 'Animal' at the top. 我也不知道它将是哪种动物,所以我不能将变量定义为顶部的“动物”以外的任何东西。 I did try defining the variable as an interface which all subclasses implement, which did make 'updateImage' available, but then it wasn't find methods in the superclass as that does not implement this particular interface. 我确实尝试将变量定义为所有子类都实现的接口,这确实使'updateImage'可用,但是后来在超类中找不到方法,因为它没有实现此特定接口。

Hopefully that makes sense, any advice on how I should approach this will be much appreciated. 希望这是有道理的,任何有关我应如何解决此问题的建议将不胜感激。

I don't want this method to be in the superclass, because the output varies depending on the type of animal 我不希望此方法属于超类,因为输出会根据动物的类型而有所不同

This is fine. 这可以。 Define it in the superclass as an abstract method and then each subclass is required to implement it. 在超类中将其定义为抽象方法,然后需要每个子类来实现它。

public abstract class Animal {

    public void abstract updateImage();
}

public class Dog extends Animal {

    public void updateImage() {
        // Do dog-specific stuff here
    }
}

public class Cat extends Animal {

    public void updateImage() {
        // Do cat-specific stuff here
    }
}

Note, I have not added the state parameter to the updateImage method as you have not provided information on the type returned from getCurrentState . 注意,由于您没有提供有关从getCurrentState返回的类型的信息,因此我没有将state参数添加到updateImage方法。

This link will hopeful help: http://www.java-made-easy.com/java-inheritance.html 该链接将对您有所帮助: http : //www.java-made-easy.com/java-inheritance.html

Declare the Animal class abstract with updateImage method abstract and implement it in the subclasses. 用updateImage方法abstract声明Animal类抽象,并在子类中实现它。 You can find something helpful here 您可以在这里找到有用的东西

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM