重写类方法:在子类方法中包含超类方法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;
}
}
So I have these to classes, one is my superclass the other is my subclass. I am having trouble calling my sub class method in my superclass method so I can get these results also. the methods are overloading and I am having a problem understanding it. I think If I can get this, it will help me understand how the two interlink and work.
I am having trouble in my copy constructor, toString method and equals method in my subclass
Superclass:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 Convertible 类中的第 29 行更改为
super(obj);
并将第 43 行更改为
if (super.equals(obj) && this.typeTop.equals(obj.typeTop)) {
change line number 29 in Convertible class to
super(obj);
and 43 to
if (super.equals(obj) && this.typeTop.equals(obj.typeTop)) {