Fai*_*ist 7 c# oop polymorphism inheritance upcasting
鉴于以下代码,我从Shape继承了一个Circle:
class Shape
{
void Draw();
}
class Circle : Shape
{
}
void Main(string[] args)
{
Shape s = new Shape();
Shape s2 = new Shape();
Circle c = new Circle();
List<Shape> ShapeList = new List<Shape>();
ShapeList.Add(s);
ShapeList.Add(s2);
ShapeList.Add(c);
}
Run Code Online (Sandbox Code Playgroud)
怎么可以c加入ShapeList?
A Circle 是a Shape,因为Circle扩展Shape.因此,您可以始终将Circle对象视为一个对象,Shape因为我们可以绝对确定可以在a上执行的所有操作也可以在a Shape上执行Circle.
| 归档时间: |
|
| 查看次数: |
3780 次 |
| 最近记录: |