feat(class15): 添加静态变量和实例变量的示例代码- 新增 Student 类,包含静态变量 name 和实例变量 age

- 新增 Test 类,演示静态变量的共享特性
- 新增 User 类,使用静态变量 count 记录用户数量
- 新增 Test2 类,测试 User 类的静态变量
This commit is contained in:
2025-07-09 02:26:15 +08:00
parent ccb4da0550
commit 11d02d1b48
4 changed files with 47 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
package class15.staticfield;
public class Student {
//静态变量,属于类,所有对象共享
static String name;
//实例变量,属于对象,对象有自己
}

View File

@@ -0,0 +1,21 @@
package class15.staticfield;
public class Test {
public static void main(String[] args) {
Student.name = "袁华";
System.out.println(Student.name);
Student s1 = new Student();
s1.name = "马冬梅";
System.out.println(s1.name);
Student s2 = new Student();
s2.name = "秋雅";
System.out.println(s1.name);
System.out.println(s2.name);
}
}

View File

@@ -0,0 +1,11 @@
package class15.staticfield;
public class Test2 {
public static void main(String[] args) {
new User();
new User();
new User();
new User();
System.out.println(User.count);
}
}

View File

@@ -0,0 +1,8 @@
package class15.staticfield;
public class User {
public static int count = 0;
public User(){
count++;
}
}