想知道以下两者之间的区别:
案例1:基类
public void DoIt();
案例1:继承类
public new void DoIt();
案例2:基类
public virtual void DoIt();
案例2:继承类
public override void DoIt();
根据我运行的测试,情况1和2似乎具有相同的效果。有区别吗,或者有更好的方式吗?
想知道以下两者之间的区别:
案例1:基类
public void DoIt();
案例1:继承类
public new void DoIt();
案例2:基类
public virtual void DoIt();
案例2:继承类
public override void DoIt();
根据我运行的测试,情况1和2似乎具有相同的效果。有区别吗,或者有更好的方式吗?
当前回答
在情况1中,如果您使用调用继承类的DoIt()方法,而类型被声明为基类,您甚至可以看到基类的操作。
/* Results
Class1
Base1
Class2
Class2
*/
public abstract class Base1
{
public void DoIt() { Console.WriteLine("Base1"); }
}
public class Class1 : Base1
{
public new void DoIt() { Console.WriteLine("Class1"); }
}
public abstract class Base2
{
public virtual void DoIt() { Console.WriteLine("Base2"); }
}
public class Class2 : Base2
{
public override void DoIt() { Console.WriteLine("Class2"); }
}
static void Main(string[] args)
{
var c1 = new Class1();
c1.DoIt();
((Base1)c1).DoIt();
var c2 = new Class2();
c2.DoIt();
((Base2)c2).DoIt();
Console.Read();
}
其他回答
请尝试以下操作:(case1)
((BaseClass)(new InheritedClass())).DoIt()
编辑:virtual+override在运行时被解析(因此override真正覆盖虚拟方法),而new只是创建同名的新方法,并隐藏旧方法,它在编译时被解析->你的编译器将调用它'看到'的方法
在情况1中,如果您使用调用继承类的DoIt()方法,而类型被声明为基类,您甚至可以看到基类的操作。
/* Results
Class1
Base1
Class2
Class2
*/
public abstract class Base1
{
public void DoIt() { Console.WriteLine("Base1"); }
}
public class Class1 : Base1
{
public new void DoIt() { Console.WriteLine("Class1"); }
}
public abstract class Base2
{
public virtual void DoIt() { Console.WriteLine("Base2"); }
}
public class Class2 : Base2
{
public override void DoIt() { Console.WriteLine("Class2"); }
}
static void Main(string[] args)
{
var c1 = new Class1();
c1.DoIt();
((Base1)c1).DoIt();
var c2 = new Class2();
c2.DoIt();
((Base2)c2).DoIt();
Console.Read();
}
new means respect your REFERENCE type(left-hand side of =) , thereby running reference types's method. If redefined method doesn't have new keyword, it is behaved as it has. Moreover, it also known as non-polymorphic inheritance. That is, “I’m making a brand new method in the derived class that has absolutely nothing to do with any methods by the same name in the base class.” - by said Whitaker override, which must be used with virtual keyword in its base class, means respect your OBJECT type(right-hand side of =), thereby running method overriden in regardless of reference type. Moreover, it also known as polymorphic inheritance.
我的方法是记住这两个关键字,它们是彼此相反的。
Override:必须定义virtual关键字来覆盖该方法。使用override关键字的方法,无论引用类型(基类或派生类的引用),如果它是用基类实例化的,基类的方法都会运行。否则,派生类的方法运行。
新:如果关键字被一个方法使用,与override关键字不同,引用类型很重要。如果它是用派生类实例化的,并且引用类型是基类,则运行基类的方法。如果它是用派生类实例化的,并且引用类型是派生类,则运行派生类的方法。即override关键字的对比。顺便说一下,如果你忘记或忽略向方法添加new关键字,编译器默认行为为使用new关键字。
class A
{
public string Foo()
{
return "A";
}
public virtual string Test()
{
return "base test";
}
}
class B: A
{
public new string Foo()
{
return "B";
}
}
class C: B
{
public string Foo()
{
return "C";
}
public override string Test() {
return "derived test";
}
}
主呼叫:
A AClass = new B();
Console.WriteLine(AClass.Foo());
B BClass = new B();
Console.WriteLine(BClass.Foo());
B BClassWithC = new C();
Console.WriteLine(BClassWithC.Foo());
Console.WriteLine(AClass.Test());
Console.WriteLine(BClassWithC.Test());
输出:
A
B
B
base test
derived test
新代码示例,
通过逐个注释来处理代码。
class X
{
protected internal /*virtual*/ void Method()
{
WriteLine("X");
}
}
class Y : X
{
protected internal /*override*/ void Method()
{
base.Method();
WriteLine("Y");
}
}
class Z : Y
{
protected internal /*override*/ void Method()
{
base.Method();
WriteLine("Z");
}
}
class Programxyz
{
private static void Main(string[] args)
{
X v = new Z();
//Y v = new Z();
//Z v = new Z();
v.Method();
}
none、virtual、override、new和abstract的所有组合:
new:它只是隐藏基类的方法,但如果你想要访问它。
override:它覆盖了基类的方法,即使你想访问它也无法访问。
例子
using System;
public class Program
{
public static void Main()
{
BaseClass test = new DerivedClass();
var result = test.DoSomething();
Console.WriteLine(result);
}
class BaseClass
{
public string DoSomething()
{
return "Base result";
}
}
class DerivedClass : BaseClass
{
public new string DoSomething()
{
return "Derived result";
}
}
}
结果:基础结果
附注:我从下面的链接复制并稍微改变了上面的例子: https://stackoverflow.com/a/45822233/10995103