静态static关键字概述和修饰成员变量

静态static关键字概述

 

 静态static关键字修饰成员变量

案例:

Student7类:

private int id;
private String name;
private int age;
static String room;
private static int idCounter=0;

public Student7() {
this.id =++idCounter;
}

public Student7(String name, int age) {
this.name = name;
this.age = age;
this.id =++idCounter;
}

public int getId() { return id; }

public void setId(int id) { this.id = id; }

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

DemoStaticTest7类:

public static void main(String[] args) {
Student7 one = new Student7("吴磊",18);
one.room="101教室";
System.out.println("姓名:"+one.getName()+",年龄:"+one.getAge()
+",教室:"+one.room+",学号:"+one.getId());

Student7 two = new Student7("赵露思", 18);
System.out.println("姓名:"+two.getName()+",年龄:"+two.getAge()
+",教室:"+two.room+",学号:"+two.getId());
}
posted @   想见玺1面  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示