public class Person {
static int totalNums=0;
String name;
int age;
int times;
//方法
void eat(String food){
if(times>=3){
System.out.println("不能再吃了");
}else{
System.out.println("吃了"+food);
times++;
}
}
//构造器
Person(String n,int a){
name=n;
age=a;
Person.totalNums++;
}
public static void main(String[] args) {
Person per1= new Person("zs",18);
System.out.println("目前人数"+Person.totalNums);
per1.eat("米饭");
per1.eat("面条");
per1.eat("粥");
per1.eat("米线");
Person per2= new Person("ls",20);
System.out.println("目前人数"+Person.totalNums);
System.out.println(per2.name+per2.age);
}
}
练习:静态变量的引用以及自定义构造器
最新推荐文章于 2025-10-30 15:05:01 发布
2137

被折叠的 条评论
为什么被折叠?



