サブクラスのコンストラクターを使用してオブジェクトを作成しようとしていますが、サブクラスコンストラクターでそのオブジェクトに値を割り当てることができません。
これがスーパークラスです。
public class Bike
{
String color = "";
String type = "";
int age = 0;
public static void main (String [] args)
{
}
public Bike (String s, int i) // Constructor
{
color = s;
age = i;
}
public void PrintBike ()
{
if (type == "")
{
System.out.print(" You didn't give the proper kind of bike.");
}
else
{
System.out.print(" Your bike is a " + type + " bike. \n");
}
}
}
これはサブクラスです。
public class BikeAdv extends Bike
{
private String type;
public BikeAdv (String color, int age, String BikeType)
{
super (color, age);
type = BikeType;
}
}
コンストラクターを呼び出すクラスは次のとおりです。
public class Green
{
public static void main (String [] args)
{
Bike greenBike = new BikeAdv ("Green", 20, "Mountain");
greenBike.PrintBike();
}
}
クラス「Green」を実行すると、「適切な種類の自転車を提供しなかった」という出力が表示されます。一方、「あなたのバイクはマウンテンバイクです」と表示されると思います。
ありがとう!