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

发布于 2024-12-17 02:40:48 字数 2384 浏览 3 评论 0原文

所以我有这些类,一个是我的超类,另一个是我的子类。我在调用超类方法中的子类方法时遇到问题,因此我也可以获得这些结果。这些方法正在超载,我在理解它时遇到了问题。我想如果我能得到这个,它将帮助我理解两者是如何相互联系和工作的。 我的子类超类中的复制构造函数、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2024-12-24 02:40:48

将 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)) {

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文