创建“派生类”类型的对象
我最近一直在研究类继承,并且不断遇到这段特定的代码。
public class Foo {
public bool DoSomething()
{
return false;
}
}
public class Bar : Foo {
public new bool DoSomething()
{
return true;
}
}
public cass Test {
public static void Main () {
Foo test = new Bar ();
Console.WriteLine (test.DoSomething ());
}
}
这里让我感到困惑的是,如果是我,我会按 Bar 类型创建一个 Bar 实例...
Bar test = new Bar();
我不明白为什么它会按照代码中的方式创建。
I've been studying class inheritance recently and I keep coming across this specific piece of code.
public class Foo {
public bool DoSomething()
{
return false;
}
}
public class Bar : Foo {
public new bool DoSomething()
{
return true;
}
}
public cass Test {
public static void Main () {
Foo test = new Bar ();
Console.WriteLine (test.DoSomething ());
}
}
What confuses me here is that, if it were me I would create an instance of Bar by type Bar...
Bar test = new Bar();
I don't understand why it would be created the way it is in the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
编写此代码可能是为了演示重写和隐藏基类方法之间的区别:
在本例中,使用
实例化
变量将使用基类方法Bar
对象>FooDoSomething()
并打印出false
。如果DoSomething
方法在基类中被声明为 virtual 并在派生类中被重写,则输出将为true
,如下例所示:This code was probably written to demonstrate the difference between overriding and hiding a base class method:
In this case instantiating a
Bar
object using aFoo
variable will use the base class methodDoSomething()
and print outfalse
. Had theDoSomething
method been declared as virtual in the base class and been overridden in the derived class the output would betrue
as in the following example:在现实世界的物体中思考这些东西会更容易。
尝试考虑一辆车。您可以拥有不同品牌/型号的汽车,但每辆车都执行相同的基本功能。
您的代码可以以相同的方式处理对象。您编写的代码可以与任何汽车一起使用,但您实际上可以指定您想要的任何汽车品牌/型号:
现在,通过这些定义...您可以创建一个负责驾驶汽车的类。什么车都没关系。您只需要担心驾驶:
It's easier to think of these things in real world objects.
Try thinking about a car. You can have different makes/models of car but each car performs the same base functionality.
Your code can work with objects in the same way. You write the code to work with any car, but you can really specify any make/model of car you want:
Now, with those definitions...you could create a class responsible for driving the car. It wouldn't matter what car. You just have to worry about driving:
一般来说,不会的。更现实的例子是
在这种情况下,
f
可以是Foo
或Bar
,而DoSomethingWithAFoo
则不会不必知道或关心。Generally, it wouldn't be. A more realistic example would be
In this case,
f
could be aFoo
or aBar
, andDoSomethingWithAFoo
doesn't have to know or care.Bar
派生自Foo
,因此创建Bar
实例并分配给 aaFoo
是完全有效的-参考。我怀疑您显示的示例代码仅显示Bar
是Foo
因此是可分配的。Bar
derives fromFoo
, and so it is perfectly valid to create an instance ofBar
and assigned to a aFoo
-reference. I suspect the example code that you have shown is merely showing thatBar
is aFoo
and is therefore assignable.您可以使用不太通用的类型( Boo )分配更通用的( Foo ),这通常在利用多态性时完成。经典的例子是有 Animal、Dod 和 Cat,然后你可以说
Animal=new Dog()
或Animal=new Cat()
,然后你可以调用虚函数它并自动调用适当的函数。这不是你的情况,因为你将函数 DoSomething 与 new ... 重叠You can assign a more generic (Foo) with a less generic type ( Boo ) this is usually done when you leverage the polymorphism. The classical example is having Animal, Dod and Cat and then you can say
Animal=new Dog()
orAnimal=new Cat()
, then you can call virtual function on it and having the proper function automatically called. This Is not your case since you are overlapping the function DoSomething with new ...如果您认为它只需要
Bar
实现的Foo
定义的功能,那么它可能更有意义。Bar
可以自由添加更多与Bar
相关的功能。现在,调用代码只关心
Foo
定义的抽象,因此,尽管您正在创建Bar
,您仅获得由Foo
(只是DoSomething
),因此不是我刚刚定义的Bar.IsValid
功能。If you consider that it only wants the functionality defined by
Foo
thatBar
implements, it might make more sense.Bar
is free to add more functionality related to whateverBar
is.Now the calling code only cares about the abstraction defined by
Foo
so, despite the fact you are creatingBar
, you only get the contract defined byFoo
(justDoSomething
) and therefore notBar.IsValid
functionality I just defined.