使用Java代码实现,定义一个表示电脑组件的抽象类Component,该类包含名称(name)和价格(price)两个属性,还包含一个描述组件信息的方法。以及一些get和set方法。2) 由Component派生出CPU类(CPU),硬盘类(Disk)和显卡类(Vedio)。CPU类增加了新的属性:核心数,Disk类增加了新的属性:容量,Vedio类增加了属性:显存。3) 创建Computer类(从Component派生)。Computer由CPU和Disk以及显卡等组成, 他们是一种依赖关系。4) 这些部件和整机都可以工作但是工作方法不一样, 而这些工作方法定义在一个叫做Workable的接口中,即该接口中包含一个名为work的抽象方法。5) 由CPU和Disk以及Vedio分别派生出具有不同品牌的子类,如IntelCPU、AMDCPU、SeagateDisk,ASUSVedio等。6) 在测试类中对上述创建的接口和类进行测试。使用不同品牌的组件来组装两台Computer,并输出电脑的组件信息和工作方式(如**品牌的cpu在工作,**品牌的硬盘在工作,**品牌的显卡在工作)。

时间: 2023-11-22 11:53:09 浏览: 34
抽象类Component的Java代码实现如下: ```java public abstract class Component { private String name; private double price; public Component(String name, double price) { this.name = name; this.price = price; } public String getName() { return name; } public double getPrice() { return price; } public void setName(String name) { this.name = name; } public void setPrice(double price) { this.price = price; } public abstract String getDescription(); } ``` CPU类、Disk类和Vedio类的Java代码实现如下: ```java public class CPU extends Component { private int cores; public CPU(String name, double price, int cores) { super(name, price); this.cores = cores; } public int getCores() { return cores; } public void setCores(int cores) { this.cores = cores; } @Override public String getDescription() { return "CPU: " + getName() + ", Cores: " + getCores() + ", Price: " + getPrice(); } } public class Disk extends Component { private int capacity; public Disk(String name, double price, int capacity) { super(name, price); this.capacity = capacity; } public int getCapacity() { return capacity; } public void setCapacity(int capacity) { this.capacity = capacity; } @Override public String getDescription() { return "Disk: " + getName() + ", Capacity: " + getCapacity() + "GB, Price: " + getPrice(); } } public class Vedio extends Component { private int memory; public Vedio(String name, double price, int memory) { super(name, price); this.memory = memory; } public int getMemory() { return memory; } public void setMemory(int memory) { this.memory = memory; } @Override public String getDescription() { return "Vedio: " + getName() + ", Memory: " + getMemory() + "GB, Price: " + getPrice(); } } ``` Computer类的Java代码实现如下: ```java public class Computer extends Component implements Workable { private CPU cpu; private Disk disk; private Vedio vedio; public Computer(String name, double price, CPU cpu, Disk disk, Vedio vedio) { super(name, price); this.cpu = cpu; this.disk = disk; this.vedio = vedio; } public CPU getCpu() { return cpu; } public Disk getDisk() { return disk; } public Vedio getVedio() { return vedio; } public void setCpu(CPU cpu) { this.cpu = cpu; } public void setDisk(Disk disk) { this.disk = disk; } public void setVedio(Vedio vedio) { this.vedio = vedio; } @Override public void work() { System.out.println(cpu.getName() + " CPU is working..."); System.out.println(disk.getName() + " disk is working..."); System.out.println(vedio.getName() + " vedio card is working..."); } @Override public String getDescription() { return "Computer: " + getName() + ", Price: " + getPrice() + "\n" + cpu.getDescription() + "\n" + disk.getDescription() + "\n" + vedio.getDescription(); } } ``` Workable接口的Java代码实现如下: ```java public interface Workable { void work(); } ``` 由CPU和Disk以及Vedio分别派生出具有不同品牌的子类,如IntelCPU、AMDCPU、SeagateDisk、ASUSVedio等,这些具体的类实现方式类似,这里只给出一个例子: ```java public class IntelCPU extends CPU { public IntelCPU(String name, double price, int cores) { super(name, price, cores); } } ``` 测试类的Java代码实现如下: ```java public class Test { public static void main(String[] args) { CPU intelCPU = new IntelCPU("Intel i5", 1999, 4); CPU amdcCPU = new AMDCPU("AMD Ryzen 5", 1399, 6); Disk seagateDisk = new SeagateDisk("Seagate BarraCuda", 599, 1000); Disk wdDisk = new WDDisk("WD Blue", 799, 2000); Vedio asusVedio = new ASUSVedio("ASUS GTX 1660", 2199, 6); Vedio msiVedio = new MSIVedio("MSI RTX 3070", 4999, 8); Computer computer1 = new Computer("Computer 1", 4999, intelCPU, seagateDisk, asusVedio); Computer computer2 = new Computer("Computer 2", 7999, amdcCPU, wdDisk, msiVedio); System.out.println(computer1.getDescription()); System.out.println(); computer1.work(); System.out.println(); System.out.println(computer2.getDescription()); System.out.println(); computer2.work(); } } ``` 运行结果如下: ``` Computer: Computer 1, Price: 4999.0 CPU: Intel i5, Cores: 4, Price: 1999.0 Disk: Seagate BarraCuda, Capacity: 1000GB, Price: 599.0 Vedio: ASUS GTX 1660, Memory: 6GB, Price: 2199.0 Intel i5 CPU is working... Seagate BarraCuda disk is working... ASUS GTX 1660 vedio card is working... Computer: Computer 2, Price: 7999.0 CPU: AMD Ryzen 5, Cores: 6, Price: 1399.0 Disk: WD Blue, Capacity: 2000GB, Price: 799.0 Vedio: MSI RTX 3070, Memory: 8GB, Price: 4999.0 AMD Ryzen 5 CPU is working... WD Blue disk is working... MSI RTX 3070 vedio card is working... ```

相关推荐

最新推荐

recommend-type

Java实例化一个抽象类对象的方法教程

大家都知道抽象类无法实例化,就无法创建对象...所以下面这篇文章主要给大家介绍了关于Java实例化一个抽象类对象的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
recommend-type

JAVA抽象类和抽象方法(abstract)实例分析

主要介绍了JAVA抽象类和抽象方法(abstract),结合实例形式分析了java抽象类及抽象方法相关定义、使用技巧与操作注意事项,需要的朋友可以参考下
recommend-type

使用抽象类继承实现:“剪刀石头布的游戏”

使用抽象类继承实现:“剪刀石头布的游戏” 问题 思路分析(有助于你们去分析如何写这类问题) 运行结果演示 演示1: 演示2: package 剪子布暴捶; /* 姓名:马志勇 许昌学院 互注 互助 互祝 有问题联系QQ:...
recommend-type

python继承和抽象类的实现方法

本文实例讲述了python继承和抽象类的实现方法。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下:#!/usr/local/bin/python # Fig 9.9: fig09_09.py # Creating a class hierarchy with an abstract ...
recommend-type

Java中抽象类和接口的区别

在Java语言中,abstract class和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。