覆盖类方法:包括子类方法中的超类方法JAVA

问题描述:

所以我有这些类,一个是我的超类,另一个是我的子类。我无法在我的超类方法中调用我的子类方法,所以我也可以得到这些结果。该方法是超载,我有一个理解它的问题。我想如果我能得到这个,它会帮助我理解这两个链接和工作。 我有我的拷贝构造函数,toString方法的麻烦和equals方法在我的子类覆盖类方法:包括子类方法中的超类方法JAVA

超类:

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; 
    } 


} 
+0

我想我找到了一种方法来制作我的复制方法,这是正确的吗? \t公共转换(转换OBJ) \t \t { \t \t \t超级(OBJ); \t \t \t this.typeTop = obj.typeTop; \t \t} –

变线数29转换类super(obj);

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

+0

嘿谢谢,虽然如此,我想知道我做错了什么。 –

+0

你可以使用super(Convertible obj)类型的参数来调用super构造函数。但它必须直接在对象上调用super(obj);好 ? –

+0

是的,先生,谢谢我看到我做错了什么。这些小小的错误和忽略它们给我带来了问题。 –