package com.javasm.bean; //创建Student类 public class Student { //学号 public int stuId; //姓名 public String stuName; //性别 public String stuSex; //年龄 public int stuAge; //班级 public String stuClass; //一台电脑 public Computer computer; /** * 无参构造 */ public Student() { this(1000, "测试学生", "测试性别", 0, "测试班级"); System.out.println("调用无参构造,学生进行了初始化操作!"); } /** * 有参构造 * * @param stuId * @param stuName * @param stuSex * @param stuAge * @param stuClass */ public Student(int stuId, String stuName, String stuSex, int stuAge, String stuClass) { System.out.println("调用有参构造!"); this.stuId = stuId; this.stuName = stuName; this.stuSex = stuSex; this.stuAge = stuAge; this.stuClass = stuClass; } /** * 显示 */ public void show() { System.out.println("学号:" + this.stuId); System.out.println("姓名:" + this.stuName); System.out.println("性别:" + this.stuSex); System.out.println("年龄:" + this.stuAge); System.out.println("班级:" + this.stuClass); } /** * 读书 */ public void read() { System.out.println("学生具备读书的行为!"); } /** * 写 */ public void writer() { System.out.println("学生具备写作的行为!"); } }
StudentTest:创建对象
package com.javasm.test; import com.javasm.bean.Computer; import com.javasm.bean.Student; public class StudentTest { public static void main(String[] args) { //准备一台电脑 Computer computer = new Computer(1, "荣耀", 15, "intel i3"); System.out.println("computer-->" + computer); Computer computer1 = computer.getComputer(); System.out.println("computer1-->" + computer1); //创建对象: 类名 对象名 = new 类名(); //赋值:对象名.属性名 = 值; Student student1 = new Student(1001, "小白", "女", 30, "Java1班"); //配备一台电脑 student1.computer = computer; //取值:通过对象名来操作 System.out.println("student1-->" + student1); student1.show(); System.out.println("电脑的基本信息:"); System.out.println("电脑编号:" + student1.computer.computerId); System.out.println("电脑品牌:" + student1.computer.brand); System.out.println("电脑尺寸:" + student1.computer.size); System.out.println("电脑CPU:" + student1.computer.cpu); //行为 student1.read(); student1.writer(); System.out.println("------------------------------------------------------------------------"); //第二个学生 Student student2 = new Student(1002, "小黑", "男", 20, "Java2班"); //取值:通过对象名来操作 System.out.println("student2-->" + student2); student2.show(); Student student3 = new Student(); student3.show(); } }
computer类
package com.javasm.bean; public class Computer { //编号 public int computerId; //品牌 public String brand; //尺寸大小 public int size; //cpu public String cpu; /** * 无参构造 */ public Computer() { } /** * 有参构造 * * @param computerId * @param brand * @param size * @param cpu */ public Computer(int computerId, String brand, int size, String cpu) { this.computerId = computerId; this.brand = brand; this.size = size; this.cpu = cpu; } /** * 上网 */ public void internet() { System.out.println("电脑具备上网的行为!"); } /** * 将当前对象直接返回 * * @return */ public Computer getComputer() { return this; } }
computerTest
package com.javasm.test; import com.javasm.bean.Computer; /** * @Author:Zxb * @Version:1.0 * @Date:2022/11/17-11:46 * @Since:jdk1.8 * @Description: */ public class ComputerTest { public static void main(String[] args) { Computer computer0 = new Computer(1,"荣耀",15,"intel i3"); // // computer.productId=1001; // computer.brand="华为荣耀"; // computer.size=15; // computer.cpu="inter i7"; computer0.show(); // System.out.println("编号:" + computer.productId); // System.out.println("品牌:" + computer.brand); // System.out.println("尺寸:" + computer.size); // System.out.println("cpu:" + computer.cpu); computer0.internet(); } }
标签:String,电脑,System,学生,computer,println,public,out From: https://www.cnblogs.com/19981206-zxb/p/16899855.html