2016-04-30 8 views
-2

私の割り当てには、基本クラス(Shoes.Java)のすべてを継承するサブクラスを作成する必要があります。ここでdisplay()メソッドをオーバーライドし、superを使用してデフォルトのコンストラクタを呼び出しますか?

は私がこだわっているものです:

基底クラスのデフォルトコンストラクタを呼び出すためにスーパーを使用してサブクラスのデフォルトコンストラクタを作成します。 [1 point] サブクラスとスーパークラスのすべての属性をデフォルト値に設定する必要があります

display()メソッドをオーバーライドして、すべてのインスタンス変数値を基本クラスから、クラス。 [1ポイント]

スーパーを使用してベースのデフォルトコンストラクターを呼び出すデフォルトのコンストラクターを作成しようとしましたが、動作しません。

また、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

"それは動作しません。" - それはどういう意味ですか?あなたは何を期待していますか? – Rehman

+0

何がうまくいかないのですか? – user2494817

+0

申し訳ありませんが、私はスーパークラスを持つ基本クラスのデフォルトのコンストラクタを呼び出そうとしています。公共のRunningShoes(){ super.Shoes; –

答えて

0

あなたのサブクラスの表示は()メソッドはsuper.display()を呼び出しますが、それはまた、このように、サブクラスのカラーフィールドを印刷する必要があります。

ただし、これを行うと、サブクラスのコンストラクタtor が使用されていますは色を設定しません。あなたがクラスを継承した場合、その

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

注:それは次のようになります。子クラスのコンストラクタが呼び出され、親の引数なしのコンストラクタが呼び出されます(存在する場合)。そうでない場合、コンパイル時エラーがスローされます。これを解決するには、親クラスの引数なしのコンストラクタを作成するか、親クラスを明示的に定義したコンストラクタを呼び出す場合は、子クラスのコンストラクタでsuper(args,args,args);を使用します。 Make sure it is at top of statements in child constructor.

Refer to Constructor Chaining for more information. 

happyCoding

関連する問題