Java实现数据共享的三种方式
目录
文章目录
内容
1、类的静态变量
示例:老师和学生共用一间教室
Classroom类代码1-1:教室类
package innerclass; public class Classroom { private String no; public Classroom(String no) { super(); this.no = no; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } @Override public String toString() { return this.no + "号教室"; } }
Teacher类代码1-2:老师类
package innerclass; public class Teacher { private String name; private Student stu; public Teacher(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Student类代码1-3:学生类
package innerclass; public class Student { private String name; private Teacher teacher; public Student(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Test2类代码1-4:测试类
package innerclass; public class Test2 { static Classroom room = new Classroom("002"); public static void main(String[] args) { Teacher t = new Teacher("高老师"); Student s = new Student("小飞"); System.out.println(t.getName() + "在" + Test2.room + "教课"); System.out.println(s.getName() + "在" + Test2.room + "上课"); } }
测试结果:
高老师在002号教室教课 小飞在002号教室上课
解析:类的静态成员在内存中只有1个
2、类内声明共享数据类型的引用
示例:一名老师负责指导一名学生
Teacher类代码2-1:老师类
package innerclass; public class Teacher { private String name; private Student stu; public Teacher(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setStu(Student stu) { this.stu = stu; } @Override public String toString() { return this.name; } public void teacher() { System.out.println(this.name + "指导" + this.stu.getName() + "同学"); } }
Student类代码2-2:学生类
package innerclass; public class Student { private String name; private Teacher teacher; public Student(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @Override public String toString() { return this.name; } public void study() { System.out.println(this.name + "同学"+ "由" + this.teacher.getName() + "指导"); } }
Test1类代码2-3:测试类
package innerclass; public class Test1 { public static void main(String[] args) { Teacher t = new Teacher("王老师"); Student stu = new Student("小明"); t.setStu(stu); stu.setTeacher(t); t.teacher(); stu.study(); } }
测试结果:
王老师指导小明同学 小明同学由王老师指导
3、内部类
示例:同上
代码3-1:
package innerclass; public class Test3 { public static void main(String[] args) { Classroom1 room = new Classroom1("003"); Classroom1.Teacher t = room.new Teacher("李老师"); Classroom1.Student s = room.new Student("小田"); t.display(); s.display(); } } class Classroom1 { private String no; class Teacher { private String name; public Teacher(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void display() { System.out.println(this.name + "在" + no + "号教室" + "教课"); } } class Student { private String name; public Student(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void display() { System.out.println(this.name + "在" + no + "号教室" + "上课"); } } public Classroom1(String no) { super(); this.no = no; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } }
后记 :
本项目为参考某马视频开发,相关视频及配套资料可自行度娘或者联系本人。上面为自己编写的开发文档,持续更新。欢迎交流,本人QQ:806797785
前端项目源代码地址:https://gitee.com/gaogzhen/vue-leyou
后端JAVA源代码地址:https://gitee.com/gaogzhen/JAVA