Home > Net >  C # is the New keyword, I felt like I got into the deep end, strives for the bosses give advice or c
C # is the New keyword, I felt like I got into the deep end, strives for the bosses give advice or c

Time:09-25

 using System; 
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;

The namespace OverrideAndNew2
{
Class Program
{
The static void Main (string [] args)
{
//Declare objects of the derived classes and test which version
//of ShowDetails is run, the base or derived.
TestCars1 ();

//Declare objects of the base class, instantiated with the
//derived classes, and repeat the tests.
TestCars2 ();

//Declare objects of the derived classes and call ShowDetails
//directly.
TestCars3 ();

//Declare objects of the base class, instantiated with the
//derived classes, and repeat the tests.
TestCars4 ();
}

Public static void TestCars1 ()
{
System. The Console. WriteLine (" \ nTestCars1 ");
System. The Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- ");

The Car car1=new Car ();
Car1. DescribeCar ();
System. The Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- ");

//Notice the output from this test case. The new modifier is
//2 in the definition of ShowDetails in the ConvertibleCar
//class.
ConvertibleCar car2=new ConvertibleCar ();
Car2. DescribeCar ();
System. The Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- ");

Jail car3=new jail ();
Car3. DescribeCar ();
System. The Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- ");
}
//the Output:
//TestCars1
//-- -- -- -- -- -- -- -- -- --
//Four wheels and an engine.
//Standard transportation.
//-- -- -- -- -- -- -- -- -- --
//Four wheels and an engine.
//Standard transportation.
//-- -- -- -- -- -- -- -- -- --
//Four wheels and an engine.
//Carries seven people.
//-- -- -- -- -- -- -- -- -- --

Public static void TestCars2 ()
{
System. The Console. WriteLine (" \ nTestCars2 ");
System. The Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- ");

Var cars=new List {new Car (), new ConvertibleCar (),
The new jail ()};

The foreach (var car in cars)
{
Car. DescribeCar ();
System. The Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- ");
}
}
//the Output:
//TestCars2
//-- -- -- -- -- -- -- -- -- --
//Four wheels and an engine.
//Standard transportation.
//-- -- -- -- -- -- -- -- -- --
//Four wheels and an engine.
//Standard transportation.
//-- -- -- -- -- -- -- -- -- --
//Four wheels and an engine.
//Carries seven people.
//-- -- -- -- -- -- -- -- -- --

Public static void TestCars3 ()
{
System. The Console. WriteLine (" \ nTestCars3 ");
System. The Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- ");
ConvertibleCar car2=new ConvertibleCar ();
Jail car3=new jail ();
Car2. ShowDetails ();
Car3. ShowDetails ();
}
//the Output:
//TestCars3
//-- -- -- -- -- -- -- -- -- --
//A roof that opens up.
//Carries seven people.

Public static void TestCars4 ()
{
System. The Console. WriteLine (" \ nTestCars4 ");
System. The Console. WriteLine (" -- -- -- -- -- -- -- -- -- -- ");
The Car car2=new ConvertibleCar ();
The Car car3=new jail ();
Car2. ShowDetails ();
Car3. ShowDetails ();
}
//the Output:
//TestCars4
//-- -- -- -- -- -- -- -- -- --
//Standard transportation.
//Carries seven people.
}

//Define the base class, and the Car. The class defines two virtual methods,
.//DescribeCar and ShowDetails DescribeCar calls ShowDetails, and each derived
//class also defines a ShowDetails method. The example tests which version of
//ShowDetails is 2, the base class method or the derived class method.
The class Car
{
Public virtual void DescribeCar ()
{
System. The Console. WriteLine (" Four wheels and an engine. ");
ShowDetails ();
}

Public virtual void ShowDetails ()
{
System. The Console. WriteLine (" Standard sitting. ");
}
}

//Define the derived classes.

//Class ConvertibleCar USES the new modifier to acknowledge that ShowDetails
//hides the base class method.
The class ConvertibleCar: Car
{
Public new void ShowDetails ()
{
System. The Console. WriteLine (" A roof that opens up. ");
}
}

//Class jail USES the override modifier to specify that ShowDetails
//extends the base class method.
The class jail: Car
{
Public override void ShowDetails ()
{
System. The Console. WriteLine (" Carries seven people. ");
}
}

}


TestCars1, please () the car2. DescribeCar ();
Should not the output is?
//Four wheels and an engine.
//A roof that opens up.

Why the output value is
//Four wheels and an engine.
//Standard transportation.


After using the new, should not be subclasses blocked the same method as the parent class?

Bang!!

CodePudding user response:

You use the override keyword, will have the results you want,
Use new said, I use my, you use your

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related