类和类之间的包含和依赖关系

  • 案例一 (汽车和轮子)
package com.uncle.class_farmer;

public class Test {
    
    
    public static void main(String[] args){
    
    
        //创建农夫对象
        Farmer farmer = new Farmer();
        //农夫做一件事情--->养猪
        Pig pig = farmer.feedPig(5);
        //创建屠夫对象
        Butcher butcher = new Butcher();
        //屠夫做事--->杀猪
        butcher.killPig(pig);
    }
}

package com.uncle.class_car;

public class Car {
    
    

    //属性
    public String brand;//汽车品牌
    public String type;//型号
    public String color;//颜色
    public Wheel wheel;//车里面有一个轮子--->包含关系

    //构造方法
    public Car(){
    
    }
    public Car(String brand,String type,String color,Wheel wheel){
    
    
        this.brand=brand;
        this.type=type;
        this.color=color;
        this.wheel=wheel;
    }
    //方法
    public void showCar(){
    
    
        System.out.println("这是一辆"+brand+"牌"+type+"型号"+color+"的小汽车");
        System.out.println("车上搭载着"+wheel.brand+"牌的"+wheel.size+"尺寸"+wheel.color+"颜色的车轮子");
        wheel.turn();//方法一定对象调用的  车轮子的方法肯定是车轮子对象调用   可以放置在任何地方
    }
}

package com.uncle.class_car;

public class Wheel {
    
    

    //属性
    public String brand;//品牌
    public int size;//尺寸
    public String color;//颜色

    //构造方法
    public Wheel(){
    
    }
    public Wheel(String brand,int size,String color){
    
    
        this.brand = brand;
        this.size = size;
        this.color = color;
    }

    //方法
    public void turn(){
    
    
        System.out.println("车轮子可以旋转");
    }
}

  • 案例二(农夫 屠夫 猪)
package com.uncle.class_farmer;

public class Test {
    
    
    public static void main(String[] args){
    
    
        //创建农夫对象
        Farmer farmer = new Farmer();
        //农夫做一件事情--->养猪
        Pig pig = farmer.feedPig(5);
        //创建屠夫对象
        Butcher butcher = new Butcher();
        //屠夫做事--->杀猪
        butcher.killPig(pig);
    }
}

package com.uncle.class_farmer;

public class Butcher {
    
    //描述屠夫

    //属性  名字 有刀

    //方法
    //描述一个屠夫杀猪的方法   需要提供条件 一头猪
    public void killPig(Pig pig){
    
    
        System.out.println("屠夫执行了杀猪方法");
        String pigName = pig.getName();
        int pigWeight = pig.getWeight();
        System.out.println(pigName+"的体重为:"+pigWeight);
        pig.beKilled();
    }
}

package com.uncle.class_farmer;

public class Farmer {
    
    //农夫

    //农夫养猪--->
    //    参数--->几个月    返回值-->是一头猪
    public Pig feedPig(int month){
    
    
        Pig pig = new Pig("小花");//依赖--->在屠夫的方法中使用到了猪的对象
        pig.growUp(month);//20 --> 640
        return pig;
    }
}

package com.uncle.class_farmer;

public class Pig {
    
    //描述猪

    //属性
    private String name;//名字
    private int weight = 20;//体重

    //构造方法
    public Pig(){
    
    }
    public Pig(String name){
    
    
        this.name=name;
    }

    //方法
    //描述一个方法  表示小猪被杀啦
    public void beKilled(){
    
    
        System.out.println(this.name+"被杀啦,好惨呀");
    }

    //描述一个方法  让猪长肉
    //    每一个月涨到前一个月的两倍
    public void growUp(int month){
    
    
        for(int i=1;i<=month;i++){
    
    
            this.weight*=2;
        }
    }

    //描述一个方法  猪告知他的体重
    public int getWeight(){
    
    
        return this.weight;
    }
    public String getName(){
    
    
        return this.name;
    }
}

  • 案例三(警车和小汽车)
package com.uncle.class_extends.policecar_car;

public class Test {
    
    
    public static void main(String[] args){
    
    
        Car car = new Car(22);
        Velometer v = new Velometer(5);
        v.measureCar(car);
    }
}

package com.uncle.class_extends.policecar_car;

public class Car {
    
    

    //属性--小汽车自己的速度   匀速直线运动
    private int speed;
    //构造方法
    public Car(){
    
    }
    public Car(int speed){
    
    
        this.speed=speed;
    }
    //提供一个方法获取小气车的速度
    public int getSpeed(){
    
    
        return this.speed;
    }
}

package com.uncle.class_extends.policecar_car;

public class PoliceCar {
    
    

    //属性---警车自己的速度
    private int speed;
    //构造方法
    public PoliceCar(){
    
    }
    public PoliceCar(int speed){
    
    
        this.speed=speed;
    }

    //警车追击小汽车   依赖关系
    public void chase(Car car){
    
    
        //获取小气车速度
        int carSpeed = car.getSpeed();
        //比较两车速度
        if(this.speed>carSpeed){
    
    //可以追到
            System.out.println("警车开始追击");
            int time = 100/(this.speed-carSpeed);
            try {
    
    
                Thread.sleep(3000);//编译时异常
            }catch(Exception e){
    
    
                e.printStackTrace();
            }
            System.out.println("小样儿跑了你啦,经过"+time+"秒追到啦");
        }else{
    
    //追不到啦
            System.out.println("小汽车飞的太快啦,望尘莫及");
        }
    }
}

package com.uncle.class_extends.policecar_car;

public class Velometer {
    
    

    //属性--测速器规定好的标准时间
    private int standardTime;
    //构造方法
    public Velometer(){
    
    }
    public Velometer(int standardTime){
    
    
        this.standardTime=standardTime;
    }

    //测速器测量小汽车速度   依赖关系
    public void measureCar(Car car){
    
    
        //获取小气车的速度
        int carSpeed = car.getSpeed();
        //计算小气车运行时间
        int carTime = 100/carSpeed;
        //比较
        if(this.standardTime<=carTime){
    
    //小气车时间长 跑得慢
            System.out.println("速度正常,请保持安全行驶,祝您一路平安");
        }else{
    
    //超速啦
            System.out.println("经过测量,小气车超速啦,警车可以追击啦");
            //需要一个警车对象 来做事 追车
            //测速器发现小汽车超速  通知警车做事  (观察者设计模式)
            PoliceCar pc = new PoliceCar(25);
            pc.chase(car);
        }
    }
}

  • 案例四(学生和机房)
package com.uncle.class_extends.student_computer;

public class Test {
    
    
    public static void main(String[] args){
    
    
        MachineRoom room = new MachineRoom();
        Student student1 = new Student("步尔斯特");
        room.welcomeStudent(student1);
        Student student2 = new Student("张三");
        room.welcomeStudent(student2);
        Student student3 = new Student("李四");
        room.welcomeStudent(student3);
        Student student4 = new Student("王五");
        room.welcomeStudent(student4);
        Student student5 = new Student("赵六");
        room.welcomeStudent(student5);
        Student student6 = new Student("钱七");
        room.welcomeStudent(student6);
    }
}

package com.uncle.class_extends.student_computer;

public class Computer {
    
    

    //属性---表示电脑的编号
    private int number;
    //属性---自己的状态 开着的或者关着的
    private boolean used = false;//true开着的  false关着的

    //设计构造方法
    public Computer(){
    
    }
    public Computer(int number){
    
    
        this.number=number;
    }
    //设计两个方法 获取电脑的编号 电脑的状态
    public int getNumber(){
    
    
        return this.number;
    }
    public boolean isUsed(){
    
    //获取电脑状态
        return this.used;
    }

    //设计普通方法---被打开 使用 关闭
    public void beOpen(){
    
    
        this.used = true;//状态切换成开着的
        System.out.println(this.number+"号电脑被打开啦");
    }
    public void beUsing(){
    
    
        System.out.println(this.number+"号电脑正在被使用中。。。");
    }
    public void beClose(){
    
    
        this.used = false;//状态切换成关着的
        System.out.println(this.number+"号电脑被关闭啦");
    }
}

package com.uncle.class_extends.student_computer;

public class MachineRoom {
    
    

    //机房--电脑   聚合关系   机房内有电脑     聚集
    //数组--存放5台电脑   Computer[]
    public Computer[] computers = new Computer[5];//电脑数组

    //设计一个程序块  用来给电脑数组进行初始化(赋值)
    {
    
    
        for(int i=0;i<computers.length;i++){
    
    
            computers[i] = new Computer(i+1);
        }
    }

    //机房--学生   依赖关系   机房欢迎学生来使用
    public void welcomeStudent(Student student){
    
    
        String studentName = student.getName();
        System.out.println("欢迎"+studentName+"学生进入机房");
        //学生进入机房后 挨个找寻 选择一台状态为关闭的电脑
        for(int i=0;i<computers.length;i++){
    
    
            boolean computersState = computers[i].isUsed();//找寻某一台电脑 获取他的状态
            if(!computersState){
    
    //表示电脑没有人使用
                student.useComputer(computers[i]);
                break;
            }
        }
    }
}

package com.uncle.class_extends.student_computer;

public class Student {
    
    

    //属性--学生的名字
    private String name;
    //属性--学生的人品 素质   0-9的整数    0-4人品好  5-9人品不好
    private int RP = (int)(Math.random()*10);

    //构造方法
    public Student(){
    
    }
    public Student(String name){
    
    
        this.name=name;
    }
    //设计一个获取学生名字的方法
    public String getName(){
    
    
        return this.name;
    }

    //依赖关系  学生使用电脑
    public void useComputer(Computer computer){
    
    
        System.out.println(this.name+"开始使用电脑啦");
        computer.beOpen();
        computer.beUsing();
        if(this.RP<5){
    
    
            computer.beClose();
        }else{
    
    
            System.out.println(this.name+"RP有问题 没关电脑 什么素质啊~");
        }
    }
}

  • 警车和小汽车在这里插入图片描述
  • 学生和机房
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_51945027/article/details/112781156