简体   繁体   中英

C# Inheritance. Console is not opening

I have two classes named car and vehicle. I inherited the car class from the vehicle class. I'm trying to run it in program.cs. But the console is opening and closing. Console.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

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

namespace _4_Encapsulation_and_Properties
{
    class Car :  Vehicle // Child class
    {
        public string model = "Corolla";
    }
}

Vehicle.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. It did not work because the Inheritance project was selected as the Startup project and this project was empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM