posts - 45,comments - 0,views - 4815
复制代码
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();
    }
}
复制代码

 

posted on   小贤看世界  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示