创建一个人类,类里面有属性,计算创造人类的次数

@Setter
@Getter
@NoArgsConstructor
public class People {
//    private People people;
    private int id;
    private String name;
    private int age;
    public static int count;//让所有对象共享一个count,而且要public,不然无法访问
    public void Compare(@NonNull People people){
        if(people.getAge()>=getAge()){
            System.out.println(people.getName()+"年龄不小于"+getName());
        }
        else System.out.println(getName()+"年龄不小于"+people.getName());
    }

    public People(int id, String name, int age) {
        count++;
        this.id = id;
        this.name = name;
        this.age = age;
    }
}
public static void main(String[] args) {
    People people1 = new People(1,"张三",23);
    People people2 = new People(2,"李四",18);
    people1.Compare(people2);
    System.out.println(People.count);
}
posted @ 2022-10-18 12:32  Liku007  阅读(18)  评论(0编辑  收藏  举报