什么是 C# 中的方法隐藏?
方法隐藏也被称为阴影。父类的方法可以通过阴影而不使用 override 关键字在子类中获得。子类有其自己的相同函数版本。
使用 new 关键字执行阴影。
我们看一个例子。
示例
using System;
using System.Collections.Generic;
class Demo {
public class Parent {
public string GetInfo () {
return "This is Parent Class!";
}
}
public class Child : Parent {
public new string GetInfo() {
return "This is Child Class!";
}
}
static void Main(String[] args) {
Child child = new Child();
Console.WriteLine(child. GetInfo());
}
}输出
This is Child Class!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP