重写类方法:在子类方法JAVA中包含超类方法



我有这两个类,一个是我的父类另一个是我的子类。我有麻烦调用我的子类方法在我的超类方法,所以我可以得到这些结果也。方法是超载的,我有一个问题,理解它。我想如果我能得到这个,它将帮助我理解这两者是如何相互联系和工作的。我的复制构造函数,toString方法和子类

中的equals方法出现了问题

超类:

public class Car
{
        private String make;
    private String model;
        private int miles;
  //    The default constructor—use this
        public Car()
        {
                this.make=null;
            this.model=null;
        this.miles=0;
        }
//  The 3-parameter constructor –use this
        public Car(String make,String model,int miles)
        {
            this.make=make;
            this.model=model;
            this.miles=miles;
        }
//  The copy constructor—use this
        public Car(Car obj)
        {
            this.make=obj.make;
            this.model=obj.model;
            this.miles=obj.miles;
        }
 // The toString method—use this
        public String toString()
        {
            String str;
                    str = "The car Brand: "+ this.make +" Mobel: "+this.model+" miles on the car: "+this.miles;
            return str;
        }
//  The equals method—use this
    public boolean equals(Car obj)
    {
        if      (this.make.equals(obj.make)&&this.model.equals(obj.model)&&this.miles==obj.miles)
            return true;
        else
            return false;
    }   
 }
 //My subclass method
public class Convertible extends Car
  {
        private String typeTop;
 // The default constructor—use this
        public Convertible()
        {
            super();
            this.typeTop= null;
        }
//  The 4-parameter constructor—use this
        public Convertible(String make, String model,int miles,String typeTop)
        {
            super(make,model,miles);
        this.typeTop=typeTop;
        }
 // The copy constructor—use this
        public Convertible(Convertible obj)
        {
            super(Convertible obj);
        this.typeTop=obj.typeTop;
        }
 // The toString method—use this
        public String toString()
        {
            String str;
            str =super.toString()+this.typeTop;
            return str;
        }
 // The equals method—use this
    public boolean equals(Convertible obj)
    {
        if(super.equals(super.obj)&&this.typeTop.equals(obj.typeTop))
            return true;
        else
            return false;
    }

 }

将第29行改成super(obj);

和43到if (super.equals(obj) && this.typeTop.equals(obj.typeTop)) {

最新更新