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(); } }
原文地址:http://www.cnblogs.com/19981206-zxb/p/16899855.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性