C# 基础 new 、override实现多台区别
一、new只是隐藏父类中的同名方法。基类和父类中都存在这个方法。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BaseClass bcdc = new DerivedClass();
bcdc.Method2(); //结果:Base - Method2
Console.Read();
}
public class BaseClass
{
public void Method2()
{
Console.WriteLine("Base - Method2");
}
}
public class DerivedClass : BaseClass
{
public new void Method2() //在子类中隐藏了父类的这个方法,父类总还存在这个方法。
{
Console.WriteLine("Derived - Method2");
}
}
}
}
二、override.在子类中重写父类的相同方法名。父类只能用 abstract、virtual修饰。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BaseClass bcdc = new DerivedClass();
bcdc.Method2(); //结果: Derived - Method2
Console.Read();
}
public class BaseClass
{
public virtual void Method2()
{
Console.WriteLine("Base - Method2");
}
}
public class DerivedClass : BaseClass
{
public override void Method2() //重写了父类的方法
{
Console.WriteLine("Derived - Method2");
}
}
}
}
C# 基础 new 、override实现多台区别的更多相关文章
- Java基础之comparator和comparable的区别以及使用
Java基础之comparator和comparable的区别以及使用 1: 区别: .Comparable类需要实现此接口,定义在类内,不利于扩展 2 .Comparator更灵活,可以随时自定义 ...
- override与重载的区别
override与重载的区别override 与重载的区别,重载是方法的名称相同.参数或参数类型不同,进行多次重载以适应不同的需要 Override 是进行基类中函数的重写.
- overload和override二者之间的区别
overload和override三者之间的区别 Overload是重载,是有相同的方法名,但参数类型或个数彼此不同Override是重写,是在子类与父类中,子类中的方法的方法名,参数个数.类型都与父 ...
- python 基础-文件读写'r' 和 'rb'区别
原文链接: python基础-文件读写'r' 和 'rb'区别 一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r ...
- C#基础知识四之override和new的区别
override override是派生类用来重写基类方法的.调用的派生类方法,如需调用基类方法用base关键字 override不能重写非虚方法或静态方法. override重写必须用abstrac ...
- C# virtual override 和 new 的区别
一直以来我都对 virtual override 和 new 之间的区别感到疑惑不解. 特别笔试的时候特别容易考到,真的很容易弄错啊,畜生! 光看理论永远记不住,那不如写几行代码就懂了. 首先看看v ...
- C#中override和overload的区别
重载应该叫overload,重写叫override:重载某个方法是在同一个类中发生的!重写是在子类中重写父类中的方法. 1.override: 父类:public virtual string T ...
- C#多态;父类引用指向子类对象;new和override的区别;new、abstract、virtual、override,sealed关键字区别和使用代码示例;c#类的初始化顺序
关于父类引用指向子类对象 例如: 有以下2个类 public class Father { public int age = 70; public static string name = " ...
- override和new的区别
override 1. override是派生类用来重写基类中方法的: 2. override不能重写非虚方法和静态方法: 3. override只能重写用virtual.abstract.overr ...
随机推荐
- C#语言集合
switch 用法 int x = int.Parse(Console.ReadLine()); switch(x){ case 1: Console.WriteLine("这是1" ...
- vue打印html
# vue打印功能 console.log(data.doPrint); // html字符串 let newContent =data.doPrint; let oldContent = docum ...
- WPF 异步刷新页面,创建定时器
#region 异步,刷新页面 /// <summary> /// 页面加载事件 /// </summary> /// <param name="sender& ...
- vue搭配axios踩坑
客户端项目中有一个小需求“我的卡券”,有单独入口,所以综合考虑之后,采用了vue来实现,因为是初次使用,导致了选型不当,先用了SUI-Mobile来搭建页面,当决定使用vue的时候,页面也搭建完毕了, ...
- MySQL Developer
1.The mysql Client Program 2.Data Types 3.Joins 4.Subqueries 5.Views 6.StoredRoutine . 1.Client/Serv ...
- mysql之pymsql的使用
# -*- coding:utf-8 -*- import pymysql user = input('请输入用户名:') pwd = input('请输入密码:') # 1.连接 conn = py ...
- [Unity算法]弧度和角度
参考链接: https://zhidao.baidu.com/question/576596182.html 1.弧度和角度的转换 2.sin函数 3.cos函数 4.tan函数 5.特殊的三角函数值 ...
- 使用docker生成centos7系统
因为缺乏系统,所以使用docker镜像模拟生成多个系统,用于练习集群的安装. 查看已有镜像 docker images 下载镜像: docker pull centos: docker pull 镜 ...
- scrapy中deferred的回调
def _next_request_from_scheduler(self, spider):#在引擎中处理一个请求 slot = self.slot request = slot.scheduler ...
- Gson 解决时间解析问题
异常: at org.eclipse.jdt.) at org.eclipse.jdt.) Caused by: java.text.ParseException: Failed to parse d ...