抽象超类中的私有字段和非抽象超类中的私有字段有什么区别?

时间:2018-10-06 19:53:02

标签: abstract-class superclass abstract-data-type

public abstract class PlayableItem{
    private int duration;
    protected PlayableItem(int duration){
    this.duration = duration;
    }

    //getters and setters 

    public abstract String play();


public Song extends PlayableItem(){
    private String name = name;
    private String title = title;

    protected Song(int duration, String name, String title){
        super(duration);
        this.name = name;
        this.title = title;
    }

    //getters and setters 

    @Override
    public String play(){
    return "artist name - " + name + "song title - " + title + "duration - "
            + duration;
    }

由于某种原因,我在Override方法上一直遇到错误,特别是在持续时间上。但是我的教授希望我将私有int字段保留在抽象类中。我们在抽象类上获得的所有课程都使用私有字段,但从未真正用作子类中参数的一部分。我忍不住认为我们在上课时忽略了一些非常重要的事情,因为很明显,抽象类中的字段似乎与常规超类字段不同。

即,在动物的常规超类中,其具有私有String字段类型

public class Animals{
    private String type;

    public Animals(String type){
         this.type = type;
    }
 }

public class Pet extends Animals{
    private String color;

    public Pet(String type, String color){
        super(type);
        this.color = color;
    }
 }

在创建Pet对象时,需要使用Parent类参数,但对于抽象类,它似乎有所不同。有人可以解释吗?

0 个答案:

没有答案