当子类拥有和父类相同的方法时,使用new关键字和override关键字的区别
在非多态状态下才会调用被new修饰的和父类相同的方法。
如:
public class Person1 : ReaderBusiness { public override IReaderDTO ReadMessage(string message) { return new model1() { MyProperty = 111 }; } public override string GetSyncType() { return "person1"; } public override IList<IReaderDTO> ReadMessages(string message) { throw new NotImplementedException(); } }
public class Person_son:Person1 { new public IReaderDTO ReadMessage(string message) { return new model1() { MyProperty=2333}; } }
Person_son p = new Person_son();
IReaderDTO m1= p.ReadMessage("");
model1 mm1 = m1 as model1;
Console.WriteLine(mm1.MyProperty);
Console.ReadLine();
控制台输出2333
ReaderInterface rI = new Person_son(); IReaderDTO m= rI.ReadMessage("1"); model1 mm = m as model1; Console.WriteLine(mm.MyProperty);
控制台输出111
override会重写父类的方法,任何时候都是调用子类的。