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

简介:

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

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

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

 

代码

   
   
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 " );
}
}

 
















本文转自cnn23711151CTO博客,原文链接: http://blog.51cto.com/cnn237111/541067,如需转载请自行联系原作者






相关文章
|
安全 C#
C# List基本用法
C# List基本用法
|
8月前
|
C#
C# Hashtable的用法
哈希表(HashTable)是一种通过键值对直接访问的数据结构。Add 方法用于添加成员,先检查成员是否已存在,若不存在则计算其 ASCII 码值作为散列值并添加到表中。Remove 方法用于移除成员,Size 方法返回集合成员数量。代码实现了这些功能,确保集合操作的高效性。
|
11月前
|
C#
c#中switch case语句的用法
C#中的 `switch case`语句提供了一种简洁而高效的方式来处理多个条件分支。通过了解其基本语法、注意事项和高级用法,可以在实际开发中灵活运用 `switch case`,提高代码的可读性和维护性。希望本文能帮助你更好地理解和使用C#中的 `switch case`语句。
629 0
|
C#
C#中的overload,overwrite,override的语义区别
以上概念是面向对象编程中实现多态性和继承的重要基石。理解它们之间的区别对于编写清晰、可维护的代码至关重要。
447 7
|
C# 索引
C#中的virtual和override关键字
C#中的virtual和override关键字
114 3
|
7天前
|
XML 前端开发 C#
C#编程实践:解析HTML文档并执行元素匹配
通过上述步骤,可以在C#中有效地解析HTML文档并执行元素匹配。HtmlAgilityPack提供了一个强大而灵活的工具集,可以处理各种HTML解析任务。
57 19
|
2月前
|
监控 算法 C#
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
279 0
|
11月前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
206 3
|
10月前
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
560 12