package com.javasm.obj;
public class StudentInfo {
public int id;
public String name;
public String gender;
//Computer类充当学生属性
//能够解决空的问题,弊端:占据很多堆内存
//public Computer computer = new Computer("DELL");//null
public Computer computer;
public void study() {
//每个学生都使用自己的电脑上网学习
//调用上网的方法 Computer.online();
if(computer==null){
System.out.println("computer can not is null");
return;
}
computer.online(name);
}
}
package com.javasm.obj;
public class Computer {
public String brand;//电脑品牌名
public void online(String studentName) {
System.out.println(studentName + "正在使用" + brand + "学习。。。。");
}
public Computer() {
}
public Computer(String brand) {
this.brand = brand;
}
}
package com.javasm.test;标签:java,String,---,javasm,Computer,studentInfo,computer,public,属性 From: https://www.cnblogs.com/wang1999an/p/16773487.html
import com.javasm.obj.Computer;
import com.javasm.obj.StudentInfo;
public class StudentInfoTest {
public static void main(String[] args) {
StudentInfo studentInfo = new StudentInfo();
studentInfo.id = 1001;
studentInfo.name = "张三";
studentInfo.gender = "男";
// Computer myComputer = studentInfo.computer;
// myComputer = new Computer("DELL");
// studentInfo.computer = myComputer;
studentInfo.computer = new Computer("DELL");
studentInfo.study();
}
}