首页 > 其他分享 >创建一个人类,类里面有属性,计算创造人类的次数

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

时间:2022-10-18 12:45:32浏览次数:55  
标签:People int people private id 次数 人类 public 属性

@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);
}

标签:People,int,people,private,id,次数,人类,public,属性
From: https://www.cnblogs.com/Liku-java/p/16802204.html

相关文章