重写display()方法和使用super调用默认的构造函数?

时间:2016-04-30 05:54:40

标签: java inheritance methods override

因此,对于我的作业,我必须创建一个继承基类(Shoes.Java)中的所有内容的子类。

这是我坚持的:

为子类创建一个默认构造函数,它使用super来调用基类的默认构造函数。 [1分] 它应该将子类中的所有属性以及超类设置为默认值

重写display()方法,从基类中打印出所有实例变量值,也从子类中打印出来。 [1分]

我尝试创建一个默认的构造函数,它使用super来调用基本的默认构造函数,但它不起作用。

也适用于Override方法;如何让它打印出子类中的值?

基类

    public class Shoes {


    //Instance Variables
    private String brand;
    private int price;
    private double size; 


    //Parameterized Constructor
        public Shoes (String brand, int price, double size)
        {
            this.brand = brand;
            this.price = price;
            this.size = size;
        }


    // Assigns instance variables to default values
        public Shoes(){
            brand = "";
            price = 0;
            size = 0;
        }


    /**
     * The setBrand method stores a value in the brand field.
     * @param brand The value to store in Brand.
     */

    public void setBrand (String brand)
    {
        this.brand=brand;
    }

    /**
     * The setPrice method stores a value in the price field.
     * @param price the value to store in price
     */

    public void setPrice (int price)
    {
        this.price=price;
    }

    /**
     * The setSize method stores a value in the size field.
     * @param size the value to store in size
     */

    public void setSize (double size)
    {
        this.size=size;
    }

    /**
     * The getBrand method returns Shoes brand.
     * @return the value in the brand field
     */

    public String getBrand()
    {
        return brand;
    }

    /**
     * the getPrice method returns Shoes price.
     * @return the value in the price field
     */

    public int getPrice()
    {
        return price;
    }

    /**
     * the getSize method returns Shoes size
     * @return the value in the size field
     */

    public double getSize()
    {
        return size;
    }

    // Prints out the values of all instance variables of your object
    public void display()
    {
        System.out.println("The Shoes brand is: " + brand);
        System.out.println("The Shoes price is: " + price);
        System.out.println("The Shoes size is: " + size);

    }


}

子类

    public class RunningShoes extends Shoes{


    private String color;

public void display() {
     super.display();
     }

public static void main(String[] args) {

    RunningShoes shoesA = new RunningShoes();
    shoesA.setBrand("Nike");
    shoesA.setPrice(90);
    shoesA.setSize(8);
    shoesA.setColor("Pink");
    shoesA.display();

    RunningShoes shoesB = new RunningShoes("Adidas", 60, 7, "Blue");
    shoesB.display();}

    public RunningShoes (String color){

        this.setColor(color);


    }

    public RunningShoes(String brand, int price, double size, String color){
        super(brand, price, size);

    }

    //Create a default constructor that uses super to call the base class default constructor.
    //Need help here super.Shoes wont work

    public RunningShoes() {
        super();

    }

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }


}

2 个答案:

答案 0 :(得分:0)

您的子类display()方法调用super.display(),但它还需要打印子类颜色字段,如下所示:

    public void display()
    {
        super.display();
        System.out.println("The Shoe color is: "+color);
    }

但是,如果您只是这样做,您将获得颜色的null,因为正在使用的子类构造函数 没有设置颜色。它应该是这样的:

    public RunningShoes(String brand, int price, double size, String color)
    {
        super(brand, price, size);
        this.color = color;
    }

答案 1 :(得分:0)

  

请注意,如果继承了一个类。调用子类的构造函数,并调用它的父级无参数构造函数(如果它在那里)否则会抛出编译时错误。要解决此问题,请创建父类no-arg构造函数,或者如果要调用父类显式定义的构造函数,请在子类的构造函数中使用super(args,args,args);Make sure it is at top of statements in child constructor.

Refer to Constructor Chaining for more information.

happyCoding