using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OverrideAndNew2
{
class Program
{
static void Main(string[] args)
{
// Declare objects of the derived classes and test which version
// of ShowDetails is run, 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.Console.WriteLine("\nTestCars1");
System.Console.WriteLine("----------");
Car car1 = new Car();
car1.DescribeCar();
System.Console.WriteLine("----------");
// Notice the output from this test case. The new modifier is
// used in the definition of ShowDetails in the ConvertibleCar
// class.
ConvertibleCar car2 = new ConvertibleCar();
car2.DescribeCar();
System.Console.WriteLine("----------");
Minivan car3 = new Minivan();
car3.DescribeCar();
System.Console.WriteLine("----------");
}
// 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.Console.WriteLine("\nTestCars2");
System.Console.WriteLine("----------");
var cars = new List<Car> { new Car(), new ConvertibleCar(),
new Minivan() };
foreach (var car in cars)
{
car.DescribeCar();
System.Console.WriteLine("----------");
}
}
// 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.Console.WriteLine("\nTestCars3");
System.Console.WriteLine("----------");
ConvertibleCar car2 = new ConvertibleCar();
Minivan car3 = new Minivan();
car2.ShowDetails();
car3.ShowDetails();
}
// Output:
// TestCars3
// ----------
// A roof that opens up.
// Carries seven people.
public static void TestCars4()
{
System.Console.WriteLine("\nTestCars4");
System.Console.WriteLine("----------");
Car car2 = new ConvertibleCar();
Car car3 = new Minivan();
car2.ShowDetails();
car3.ShowDetails();
}
// Output:
// TestCars4
// ----------
// Standard transportation.
// Carries seven people.
}
// Define the base class, 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 used, the base class method or the derived class method.
class Car
{
public virtual void DescribeCar()
{
System.Console.WriteLine("Four wheels and an engine.");
ShowDetails();
}
public virtual void ShowDetails()
{
System.Console.WriteLine("Standard transportation.");
}
}
// Define the derived classes.
// Class ConvertibleCar uses the new modifier to acknowledge that ShowDetails
// hides the base class method.
class ConvertibleCar : Car
{
public new void ShowDetails()
{
System.Console.WriteLine("A roof that opens up.");
}
}
// Class Minivan uses the override modifier to specify that ShowDetails
// extends the base class method.
class Minivan : Car
{
public override void ShowDetails()
{
System.Console.WriteLine("Carries seven people.");
}
}
}
請問, TestCars1() 中的car2.DescribeCar();
不是應該輸出為嗎?
// Four wheels and an engine.
// A roof that opens up.
為什么輸出的值為
// Four wheels and an engine.
// Standard transportation.
使用了new之后,不應該是子類屏蔽了父類的相同方法嗎?
爆炸了!!
uj5u.com熱心網友回復:
你用override關鍵字,才會有你想要的結果。用new表示,我用我的,你用你的。
不然,你想想,new能做到這種效果,那設計override有什么用?
uj5u.com熱心網友回復:
這種問題,我不喜歡用老外的東西解釋,new阿,overide啊,繞了來繞去,依舊不知道。我們用中國人都明白的事情一說你就明白了。
“偷梁換柱,李代桃僵,張冠李戴,指鹿為馬”
new,算“指鹿為馬”,鹿永遠不會成馬的,不過在特殊情況下“你說是馬那就是馬了”
同樣我們天朝人都知道,開會怎么說,酒桌上怎么說
開會或者酒桌上,你得說“行,沒問題,這事明天找我”,但回頭你明天找他就是“公事啊,公辦,你先去走個流程,我們在說后面的”
對,你這個new就這意思
公事 公事=new 酒桌上談的公事()
你想,現在到底輸出什么?是輸出“這事包在我身上”,還是輸出“公事啊還是公辦,我說的不算”
uj5u.com熱心網友回復:
公事 公事=new 酒桌上談的公事()酒桌上談的公事 公事=new 酒桌上談的公事()
區別一下這個,雖然是new的一個東西,但官字兩張口,說什么話取決與最前面的,而不是后面的。這是new的在程式里的意思
uj5u.com熱心網友回復:
精辟
uj5u.com熱心網友回復:
而且,new是隱含的,也就是說,也寫new,默認效果也還是new。父類和子類的同名方法,本來就是各用各的,除非你用了override。各用各的才是常態,override才是特殊轉載請註明出處,本文鏈接:https://www.uj5u.com/net/81383.html
標籤:C#
上一篇:c#期末考試演算法題,怎么雙面列印(列印機只能單面)
下一篇:wifi上不了電視盒子
