diff --git a/src/chapter3/Example14.java b/src/chapter3/Example14.java index 19c4dd5..a03f757 100644 --- a/src/chapter3/Example14.java +++ b/src/chapter3/Example14.java @@ -1,2 +1,48 @@ -package chapter3;public class Example14 { +package chapter3; + +/** + * 静态属性示例: + * 改变一个对象的静态属性,所有同类的其他对象,其静态属性会同时改变 + * 即静态属性被所有同类对象共享 + * 通过 类名.静态属性名 可以访问该属性 + */ + +// Student 类 +class Student14 { + String name; + int age; + static String school = "A大学"; + public Student14(String name, int age) { + this.name = name; + this.age = age; + } + + public void info() { + System.out.println("姓名:" + this.name + ",年龄:" + this.age + ",学校:" + school); + } +} + +// 为了安放 main 方法 +public class Example14 { + public static void main(String[] args) { + Student14 stu1 = new Student14("张三", 18); + Student14 stu2 = new Student14("李四", 19); + Student14 stu3 = new Student14("王五", 20); + + stu1.info(); + stu2.info(); + stu3.info(); + + stu1.school = "B大学"; + stu1.info(); + stu2.info(); + stu3.info(); + + Student14.school = "C大学"; + stu1.info(); + stu2.info(); + stu3.info(); + + + } }