|
发表于 2018-1-3 11:19:17
|
显示全部楼层
- """
- 写出一个类Computer,并由该类做基类派生出子类Notebook和Desktop。
- 其中Computer类具有cpu、ram两个成员变量,分别为String类型、整型,且
- 具有公有的getRam成员方法,用于返回ram变量的值。 Notebook类具有成员
- 变量factory, 为String类型,Desktop类具有comclass成员变量, 为String
- 类型。
- """
- class Computer():
- """通用的计算机类型"""
-
- def __init__(self, cpu_model, ram_capacity):
-
- self.cpu_model = cpu_model
- self.ram_capacity = ram_capacity
-
- def getRam(self):
- """用于返回计算机的内存容量"""
-
- return self.ram_capacity
-
- def getCpu(self):
- """用于返回计算机cpu型号和参数"""
-
- cpu_dict = {
- "Core i3":"1.40GHz",
- "Core i5":"3.20GHz",
- "Core i7":"3.40GHz",
- }
-
- cpu_model_list = []
-
- for per_cpu in cpu_dict.keys():
- cpu_model_list.append(per_cpu)
-
- if self.cpu_model in cpu_model_list:
- cpu_information = "您的计算机CPU型号为 >>> " + self.cpu_model
- cpu_information += "\n-- 主频参数 -- :" + cpu_dict[self.cpu_model]
- print(cpu_information)
- else:
- cpu_information = "您的计算机CPU型号为 >>> " + self.cpu_model
- cpu_information += "\n-- 主频参数 -- :暂无"
- print(cpu_information)
- class NoteBook(Computer):
- """笔记本电脑的类继承自通用计算机类"""
-
- def __init__(self, cpu_model, ram_capacity):
-
- super().__init__(cpu_model, ram_capacity)
- self.factory = "DELL"
-
- def set_factory(self, factory):
-
- self.factory = factory
- print("您的电脑生产厂家是 >>> " + self.factory)
-
- class DeskTop(Computer):
- """台式电脑的类继承自通用计算机类"""
-
- def __init__(self, cpu_model, ram_capacity):
-
- super().__init__(cpu_model, ram_capacity)
- self.other = NoteBook(cpu_model, ram_capacity)
复制代码
Java我不会,用Python写了一个,你看看.....哈哈哈哈 |
|