面向对象程序设计,请问代码应该如何编写

定义计算机类,为其扩展台式机类和笔记本电脑
类。
1.声明计算机类,属性有品牌、型号、处理器
2.声明台式机类,继承自计算机类,添加显示器类型属性
3.声明笔记本电脑类,继承自计算机类,添加电池类型属性
4.使用构造函数赋值,在继承类中使用base() 方法,向父类构造函数传递参数


using System;

namespace ComputerNamespace
{
    // 父类:计算机类
    class Computer
    {
        // 品牌
        public string Brand { get; set; }
        // 型号
        public string Model { get; set; }
        // 处理器
        public string Processor { get; set; }

        // 构造函数
        public Computer(string brand, string model, string processor)
        {
            Brand = brand;
            Model = model;
            Processor = processor;
        }
    }

    // 子类:台式机类,继承自计算机类
    class Desktop : Computer
    {
        // 显示器类型
        public string MonitorType { get; set; }

        // 构造函数:使用 base() 方法,向父类构造函数传递参数
        public Desktop(string brand, string model, string processor, string monitorType) : base(brand, model, processor)
        {
            MonitorType = monitorType;
        }
    }

    // 子类:笔记本电脑类,继承自计算机类
    class Laptop : Computer
    {
        // 电池类型
        public string BatteryType { get; set; }

        // 构造函数:使用 base() 方法,向父类构造函数传递参数
        public Laptop(string brand, string model, string processor, string batteryType) : base(brand, model, processor)
        {
            BatteryType = batteryType;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // 创建 Desktop 类对象,并使用构造函数赋值
            Desktop desktop = new Desktop("Dell", "Inspiron", "i5", "LED");
            // 输出 Desktop 对象的属性
            Console.WriteLine($"Desktop: {desktop.Brand}, {desktop.Model}, {desktop.Processor}, {desktop.MonitorType}");

            // 创建 Laptop 类对象,并使用构造函数赋值
            Laptop laptop = new Laptop("HP", "ENVY", "i7", "Lithium-Ion");
            // 输出 Laptop 对象的属性
            Console.WriteLine($"Laptop: {laptop.Brand}, {laptop.Model}, {laptop.Processor}, {laptop.BatteryType}");
        }
    }
}
// 计算机类
public class Computer
{
    public string Brand { get; set; } // 品牌
    public string Model { get; set; } // 型号
    public string Processor { get; set; } // 处理器

    public Computer(string brand, string model, string processor)
    {
        Brand = brand;
        Model = model;
        Processor = processor;
    }
}

// 台式机类,继承自计算机类
public class Desktop : Computer
{
    public string MonitorType { get; set; } // 显示器类型

    public Desktop(string brand, string model, string processor, string monitorType)
        : base(brand, model, processor)
    {
        MonitorType = monitorType;
    }
}

// 笔记本电脑类,继承自计算机类
public class Laptop : Computer
{
    public string BatteryType { get; set; } // 电池类型

    public Laptop(string brand, string model, string processor, string batteryType)
        : base(brand, model, processor)
    {
        BatteryType = batteryType;
    }
}

在上面的示例中,我们定义了一个 Computer 类来表示计算机,其中包含属性 Brand、Model 和 Processor。然后,我们使用 Desktop 和 Laptop 类分别继承 Computer 类,并且添加了适用于台式机和笔记本电脑的特定属性 MonitorType 和 BatteryType。在每个继承类的构造函数中,我们使用 base() 方法来调用父类(即 Computer 类)的构造函数,并传递所需参数。这样,我们可以通过实例化 Desktop 和 Laptop 类来创建具有特定属性的台式机和笔记本电脑对象。