鱼C论坛

 找回密码
 立即注册
查看: 3685|回复: 3

这题做怎么做

[复制链接]
发表于 2018-1-3 09:11:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
写出一个类Computer,并由该类做基类派生出子类Notebook和Desktop。其中Computer类具有cpu、ram两个成员变量,分别为String类型、整型,且具有公有的getRam成员方法,用于返回ram变量的值。 Notebook类具有成员变量factory, 为String类型,Desktop类具有comclass成员变量, 为String类型。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-3 11:19:17 | 显示全部楼层
  1. """
  2. 写出一个类Computer,并由该类做基类派生出子类Notebook和Desktop。
  3. 其中Computer类具有cpu、ram两个成员变量,分别为String类型、整型,且
  4. 具有公有的getRam成员方法,用于返回ram变量的值。 Notebook类具有成员
  5. 变量factory, 为String类型,Desktop类具有comclass成员变量, 为String
  6. 类型。
  7. """


  8. class Computer():
  9.     """通用的计算机类型"""
  10.    
  11.     def __init__(self, cpu_model, ram_capacity):
  12.         
  13.         self.cpu_model = cpu_model
  14.         self.ram_capacity = ram_capacity
  15.    
  16.     def getRam(self):
  17.         """用于返回计算机的内存容量"""
  18.         
  19.         return self.ram_capacity
  20.    
  21.     def getCpu(self):
  22.         """用于返回计算机cpu型号和参数"""
  23.         
  24.         cpu_dict = {
  25.                 "Core i3":"1.40GHz",
  26.                 "Core i5":"3.20GHz",
  27.                 "Core i7":"3.40GHz",
  28.                 }
  29.         
  30.         cpu_model_list = []
  31.         
  32.         for per_cpu in cpu_dict.keys():
  33.             cpu_model_list.append(per_cpu)
  34.         
  35.         if self.cpu_model in cpu_model_list:
  36.             cpu_information = "您的计算机CPU型号为 >>> " + self.cpu_model
  37.             cpu_information += "\n-- 主频参数 -- :" + cpu_dict[self.cpu_model]
  38.             print(cpu_information)
  39.         else:
  40.             cpu_information = "您的计算机CPU型号为 >>> " + self.cpu_model
  41.             cpu_information += "\n-- 主频参数 -- :暂无"
  42.             print(cpu_information)


  43. class NoteBook(Computer):
  44.     """笔记本电脑的类继承自通用计算机类"""
  45.    
  46.     def __init__(self, cpu_model, ram_capacity):
  47.         
  48.         super().__init__(cpu_model, ram_capacity)
  49.         self.factory = "DELL"
  50.    
  51.     def set_factory(self, factory):
  52.         
  53.         self.factory = factory
  54.         print("您的电脑生产厂家是 >>> " + self.factory)
  55.         

  56. class DeskTop(Computer):
  57.     """台式电脑的类继承自通用计算机类"""
  58.    
  59.     def __init__(self, cpu_model, ram_capacity):
  60.         
  61.         super().__init__(cpu_model, ram_capacity)
  62.         self.other = NoteBook(cpu_model, ram_capacity)
复制代码


Java我不会,用Python写了一个,你看看.....哈哈哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-3 14:26:58 | 显示全部楼层
package com.wsh;

/**
* Created by Andy_Liu on 2018/1/3.
*/
//写出一个类Computer,并由该类做基类派生出子类Notebook和Desktop。
// 其中Computer类具有cpu、ram两个成员变量,分别为String类型、整型,且具有公有的getRam成员方法,用于返回ram变量的值。
// Notebook类具有成员变量factory, 为String类型,Desktop类具有comclass成员变量, 为String类型。

public class Computer {

    private String cpu;
    private Integer ram;

    public Integer getRam() {
        return ram;
    }
}


class Desktop extends Computer {
    private String comclass;
}

class Notebook extends Computer {

    private String factory;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-15 21:08:36 | 显示全部楼层
感谢大神
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 07:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表