私の割り当てには、基本クラス(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;
}
}
"それは動作しません。" - それはどういう意味ですか?あなたは何を期待していますか? – Rehman
何がうまくいかないのですか? – user2494817
申し訳ありませんが、私はスーパークラスを持つ基本クラスのデフォルトのコンストラクタを呼び出そうとしています。公共のRunningShoes(){ super.Shoes; –