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 " );
}
}
复制代码

 本文转自最课程陆敏技博客园博客,原文链接:http://www.cnblogs.com/luminji/archive/2011/02/08/1950001.html,如需转载请自行联系原作者


相关文章
|
5天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1131 2
|
4天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
640 11
|
14天前
|
人工智能 运维 安全
|
5天前
|
弹性计算 Kubernetes jenkins
如何在 ECS/EKS 集群中有效使用 Jenkins
本文探讨了如何将 Jenkins 与 AWS ECS 和 EKS 集群集成,以构建高效、灵活且具备自动扩缩容能力的 CI/CD 流水线,提升软件交付效率并优化资源成本。
311 0
|
12天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
13天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
830 23
|
5天前
|
缓存 供应链 监控
VVIC seller_search 排行榜搜索接口深度分析及 Python 实现
VVIC搜款网seller_search接口提供服装批发市场的商品及商家排行榜数据,涵盖热销榜、销量排名、类目趋势等,支持多维度筛选与数据分析,助力选品决策、竞品分析与市场预测,为服装供应链提供有力数据支撑。

热门文章

最新文章