如图所示
package com.web;
public class Test23 {
public static void main(String[] args) {
Person person1 = new Person("Kate", "Jones", 27, 1.6, 50.0);
person1.printData();
Person person2 = new Person("John", "Christopher", "Smith", 65, 1.75, 80.0);
person2.printData();
Person.printCount();
}
}
class Person {
public static int count = 0;
public String firstName;
public String middleName;
public String lastName;
public int age;
public double height, weight;
Person(String firstName, String lastName, int age, double height, double weight) {
Person.count++;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.height = height;
this.weight = weight;
}
Person(String firstName, String middleName, String lastName, int age, double height, double weight) {
// this()を用いて、コンストラクタを呼び出してください
this(firstName, lastName, age, height, weight);//不加类型已经是实参了this(String firstName, String lastName, int age, double height, double weight)错的;
this.middleName = middleName;
}
public String fullName() {
if (this.middleName != null) {
return this.firstName + " " +this.middleName + " " +this.lastName;
} else {
return this.firstName + " " + this.lastName;
}
}
public void printData() {
System.out.println("私の名前は" + this.fullName() + "です");
System.out.println("年齢は" + this.age + "歳です");
System.out.println("BMIは" + Math.round(this.bmi()) + "です");
}
public double bmi() {
return this.weight / this.height / this.height;
}
public static void printCount() {
System.out.println("合計" + Person.count + "人です");
}
}