Also für meine Aufgabe muss ich eine Unterklasse erstellen, die alles von Ihrer Basisklasse erbt (Shoes.Java). HierOverride display() -Methode und Verwenden von Super, um Standardkonstruktor aufzurufen?
ist, was ich bin fest auf:
erstellen Standardkonstruktor für die Unterklasse, die Super verwendet die Basisklasse Standardkonstruktor aufzurufen. [1 Punkt] Es sollte alle Attribute in der Unterklasse sowie die Superklasse auf die Standardwerte setzen
Überschreiben Sie die display() -Methode, um alle Werte der Instanzvariablen aus der Basisklasse und auch von der Unter auszudrucken Klasse. [1 Punkt]
Ich habe versucht, einen Standardkonstruktor zu erstellen, der Super verwendet, um den Basisstandardkonstruktor aufzurufen, aber es funktioniert nicht.
Auch für die Override-Methode; Wie bekomme ich es, die Werte aus der Unterklasse auszudrucken?
Base Class
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);
}
}
SUBKLASSE
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;
}
}
"es funktioniert nicht." Was bedeutet das? Was erwarten Sie? – Rehman
Was funktioniert nicht? – user2494817
Sorry, ich versuche, den Standardkonstruktor der Basisklasse mit einem Super aufzurufen. public RunningShoes() { super.Schuhe; –