鱼蛋代码 发表于 2018-1-3 09:11:42

这题做怎么做

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

铁棍阿童木 发表于 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
            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写了一个,你看看.....哈哈哈哈

一个坏人 发表于 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;
}

最佳观众丶 发表于 2018-1-15 21:08:36

感谢大神
页: [1]
查看完整版本: 这题做怎么做