[英]C# Inheritance. Console is not opening
I have two classes named car and vehicle.我有两个名为 car 和 vehicle 的类。 I inherited the car class from the vehicle class. I'm trying to run it in program.cs.我从车辆 class 继承了汽车 class。我正在尝试在 program.cs 中运行它。 But the console is opening and closing.但是控制台正在打开和关闭。 Console.Read();控制台.Read(); I did but it still behaves the same.我做到了,但它的行为仍然相同。 It doesn't give an error either.它也不会报错。 I did not understand the reason.我不明白原因。 I am sharing the codes.我正在分享代码。
Program.cs程序.cs
namespace _4_Encapsulation_and_Properties
{
class Program
{
static void Main(string[] args)
{
Car myCar = new Car();
myCar.Honk();
Console.WriteLine("My car brand is: " + myCar.brand + "My car model is: " + myCar.model);
Console.Read();
}
}
}
Car.cs汽车.cs
namespace _4_Encapsulation_and_Properties
{
class Car : Vehicle // Child class
{
public string model = "Corolla";
}
}
Vehicle.cs车辆.cs
namespace _4_Encapsulation_and_Properties
{
class Vehicle // base Class
{
public string brand = "Toyota";
public void Honk()
{
Console.WriteLine("Düüd , Düüd!");
Console.Read();
}
}
}
I wrote my codes in the wrong project.我在错误的项目中编写了代码。 I created a different project for Inheritance, but I created my classes in the project I created for Encapsulation.我为 Inheritance 创建了一个不同的项目,但是我在为封装创建的项目中创建了我的类。 It did not work because the Inheritance project was selected as the Startup project and this project was empty.它没有工作,因为 Inheritance 项目被选为 Startup 项目,而这个项目是空的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.