Generation

generate functionWed, 25 Jan 2023

問題3, で修正したHumanクラスのインスタンスの作成時に、コンストラクタに値を渡しても渡さなくても、インスタンスを作成できるようオーバーロードを使って プログラムを修正してください。 値を渡した場合は、渡した値をageフィールドに設定し、値を渡さなかった場合は、ageフィールドに99を設定してください。

public class Human { public int age; public String name; public int height; public int weight; public Human(int age, String name, int height, int weight) { this.age = age; this.name = name; this.height = height; this.weight = weight; } public void introduce() { System.out.println("年齢は" + this.age + "です"); System.out.println("名前は" + this.name + "です"); System.out.println("身長は" + this.height + "です"); System.out.println("体重は" + this.weight + "です"); System.out.println("------------------"); } public void eat() { this.weight += 1; } public void walk() { this.weight -= 1; }

Questions about programming?Chat with your personal AI assistant