C#中new, override, virtual的具体用法

一句话:你是否真的了解new, override, virtual

下面代码的结果,如果基类使用接口代替,也是一样的效果。

主要注意看override或new了基类的方法后,调用方将子类对象转型为父类后的输出会有什么不同;

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
class Program
{
static void Main( string [] args)
{
TestShape();
Console.WriteLine(
" TestShape end ============= " + Environment.NewLine);
TestDerive();
Console.WriteLine(
" TestDerive end ============= " + Environment.NewLine);
TestDerive2();
Console.WriteLine(
" TestDerive2 end ============= " + Environment.NewLine);
Console.ReadKey();
}

private static void TestShape()
{
System.Collections.Generic.List
< Shape > shapes = new System.Collections.Generic.List < Shape > ();
shapes.Add(
new Circle());
shapes.Add(
new Rectangle());
shapes.Add(
new Triangle());
shapes.Add(
new Diamond());
foreach (Shape s in shapes)
{
s.MethodVirtual();
s.Method();
Console.WriteLine();
}

}

private static void TestDerive()
{
Circle circle
= new Circle();
Rectangle rectangle
= new Rectangle();
Triangle triangel
= new Triangle();
Diamond diamond
= new Diamond();
circle.MethodVirtual();
circle.Method();
Console.WriteLine();
rectangle.MethodVirtual();
rectangle.Method();
Console.WriteLine();
triangel.MethodVirtual();
triangel.Method();
Console.WriteLine();
diamond.MethodVirtual();
diamond.Method();
Console.WriteLine();
}

private static void TestDerive2()
{
Circle circle
= new Circle();
PrintShape(circle);
Rectangle rectangle
= new Rectangle();
PrintShape(rectangle);
Triangle triangel
= new Triangle();
PrintShape(triangel);
Diamond diamond
= new Diamond();
PrintShape(diamond);
/// out put:
// circle override MethodVirtual
// base Method call

// base MethodVirtual call
// base Method call

// base MethodVirtual call
// base Method call

// base MethodVirtual call
// base Method call
}

static void PrintShape(Shape sharpe)
{
sharpe.MethodVirtual();
sharpe.Method();
Console.WriteLine();
}
}

public class Shape
{
public virtual void MethodVirtual()
{
Console.WriteLine(
" base MethodVirtual call " );
}

public void Method()
{
Console.WriteLine(
" base Method call " );
}
}

/// 类描述:override了基类的virtual方法
///
/// 第一种使用方法:转型为父类
/// sharp s = new Circle()
/// s.MethodVirtual();
/// s.Method();
/// 因为子类已经override了父类的MethodVirtual,所以即使子类转型为了sharp,调用的还是子类的方法
/// out put:
/// circle override MethodVirtual
/// base Method call
///
/// 第二类使用方法:使用子类本身
/// 这很好理解,全部输出的是子类的方法
/// Circle circle = new Circle();
/// circle.MethodVirtual();
/// circle.Method();
/// out put:
/// circle override MethodVirtual
/// base Method call
class Circle : Shape
{
public override void MethodVirtual()
{
Console.WriteLine(
" circle override MethodVirtual " );
}
}

/// 类描述:未做任何处理
///
/// 第一种使用方法
/// sharp s = new Rectangle()
/// s.MethodVirtual();
/// s.Method();
/// out put:
/// base MethodVirtual call
/// base Method call
///
/// 第二类使用方法:使用子类本身
/// 这很好理解,全部输出的是子类的方法
/// Rectangle rectangle = new Rectangle();
/// rectangle.MethodVirtual();
/// rectangle.Method();
/// out put:
/// base MethodVirtual call
/// base Method call
class Rectangle : Shape
{

}

/// 类描述:new了基类的虚方法即非虚方法
///
/// 第一种使用方法
/// sharp s = new Triangle()
/// s.MethodVirtual();
/// s.Method();
/// 因为子类已经new了父类的方法,所以s输出的是父类的方法
/// out put:
/// base MethodVirtual call
/// base Method call
///
/// 第二类使用方法:使用子类本身
/// 这很好理解,全部输出的是子类的方法
/// Triangle triangel = new Triangle();
/// triangel.MethodVirtual();
/// triangel.Method();
/// out put:
/// triangle new MethodVirtual
/// triangle new Method
class Triangle : Shape
{
public new void MethodVirtual()
{
Console.WriteLine(
" triangle new MethodVirtual " );
}

public new void Method()
{
Console.WriteLine(
" triangle new Method " );
}
}

/// 类描述:创建了基类方法相同的方法,未new及override
/// 编译器会做提示“隐藏继承”,并有如存在 new 关键字一样执行操作
///
/// 第一种使用方法
/// sharp s = new Diamond()
/// s.MethodVirtual();
/// s.Method();
/// 因为默认new的效果,所以输出和显式new修饰的一样
/// out put:
/// base MethodVirtual call
/// base Method call
///
/// 第二类使用方法:使用子类本身
/// 这很好理解,全部输出的是子类的方法
/// Diamond diamond = new Diamond();
/// diamond.MethodVirtual();
/// diamond.Method();
/// out put:
/// Diamond default MethodVirtual
/// Diamond default Method
class Diamond : Shape
{
public void MethodVirtual()
{
Console.WriteLine(
" Diamond default MethodVirtual " );
}

public void Method()
{
Console.WriteLine(
" Diamond default Method " );
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值