c#编写一个Car类,具有final类型的属性品牌,具有功能drive;定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;定义主类E,在其main方法中分别创建Aodi和Benchi的对象,并测试对象的特性。
时间: 2024-05-13 12:18:42 浏览: 9
以下是基于您的要求编写的代码:
```
using System;
public class Car
{
public readonly string brand;
public Car(string brand)
{
this.brand = brand;
}
public void Drive()
{
Console.WriteLine($"Driving {brand}");
}
}
public class Aodi : Car
{
public double price;
public string model;
public Aodi(string model, double price) : base("Aodi")
{
this.model = model;
this.price = price;
}
public void ChangeGear()
{
Console.WriteLine($"{model} is changing gear.");
}
}
public class Benchi : Car
{
public double price;
public string model;
public Benchi(string model, double price) : base("Benchi")
{
this.model = model;
this.price = price;
}
public void ChangeGear()
{
Console.WriteLine($"{model} is changing gear.");
}
}
public class E
{
static void Main(string[] args)
{
Aodi aodi = new Aodi("A3", 250000);
aodi.Drive();
Console.WriteLine($"Price: {aodi.price}, Model: {aodi.model}");
aodi.ChangeGear();
Benchi benchi = new Benchi("C200L", 350000);
benchi.Drive();
Console.WriteLine($"Price: {benchi.price}, Model: {benchi.model}");
benchi.ChangeGear();
Console.ReadLine();
}
}
```
在这个例子中,我们创建了一个名为 `Car` 的基类,其中包含品牌属性和驾驶方法。然后我们创建了两个子类 `Aodi` 和 `Benchi`,它们继承自 `Car` 并具有价格和型号属性以及变速方法。最后,我们在 `E` 类中创建了 `Aodi` 和 `Benchi` 的对象,并测试它们的属性和方法。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)