• 【设计模式_实验①_第六题】设计模式——接口的实验&模拟应用实验作业一


    【实验要求】 货车要装载一批货物,货物由三种商品组成:电视、计算机和洗衣机。卡车需要计算出整批货物的重量。

    【实验步骤】UML

    过程

    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述在这里插入图片描述

    在这里插入代码片

    public interface ComputerWeight {
        public abstract double computerWeight();
    }
    
    • 1
    • 2
    • 3
    public class Truck {
        ComputerWeight  [] goods;
        double totalWeight=0;
    
        public Truck(ComputerWeight[] cw) {
            this.goods = cw;
        }
    
        public void setGoods(ComputerWeight [] goods){
            this.goods = goods;
        }
        public double getTotalWeight(){
            for (int i= 0;i<goods.length;i++){
                totalWeight+=goods[i].computerWeight();
            }
            return totalWeight;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    public class Television implements ComputerWeight{
        /**
         *  tv
         * @return
         */
        @Override
        public double computerWeight() {
            return 2;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
     * @Version 1.0
     */
    
    public class WashMachine implements ComputerWeight{
    
        @Override
        public double computerWeight() {
            return 5;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    
    * * @Date 2023/10/1 22:23
     * @Version 1.0
     */
    
    public class Computer implements ComputerWeight{
        @Override
        public double computerWeight() {
            return 3;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    public class TruckMain {
        public static void main(String[] args) {
            // one
            ComputerWeight[] goods = new ComputerWeight[650];
            // another
            ComputerWeight[] goods2 = new ComputerWeight[68];
            // truck
            Truck truck1 = new Truck(goods);
            // truck2
            Truck truck2 = new Truck(goods2);
            //one
            for (int i = 0; i < goods.length; i++) {
                if (0 == i % 3) {
                    goods[i] = new Television();
                } else if (1 == i % 3) {
                    goods[i] = new Computer();
                } else if (2 == i % 3) {
                    goods[i] = new WashMachine();
                }
            }
            // two 2
            for (int j = 0; j < goods2.length; j++) {
                if (0 == j % 2) {
                    goods2[j] = new Television();
                } else {
                    goods2[j] = new WashMachine();
                }
            }
            truck1.setGoods(goods);
            truck2.setGoods(goods2);
            // 1
            System.out.println("第一辆货车的装载量:"+truck1.getTotalWeight()+"kg");
            // 2
            System.out.println("第er辆货车的装载量:"+truck2.getTotalWeight()+"kg");
    
    
        }
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    英伟达 GPU 架构简史
    卷积神经网络
    pytorch打印模型信息——torchinfo
    CNN反向求导推导
    Linux环境下JDK1.8的安装(保姆级教程)
    Golang net/http 标准库源码学习
    【机器学习】LoRA:大语言模型中低秩自适应分析
    13个Redis面试题,你都知道哪些?
    四、线程安全问题以及锁的概念
    Elasticsearch 面试题
  • 原文地址:https://blog.csdn.net/weixin_52372189/article/details/133471286