C# 单一继承示例
csharpserver side programmingprogramming更新于 2025/9/24 15:35:00
以下是 C# 中单一继承的示例。在该示例中,基类为 Father,并像以下代码片段一样声明 −
class Father {
public void Display() {
Console.WriteLine("Display");
}
}
我们的派生类为 Son,并像以下代码片段一样声明 −
class Son : Father {
public void DisplayOne() {
Console.WriteLine("DisplayOne");
}
}
示例
以下是在 C# 中实现单继承的完整示例。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyAplication {
class Demo {
static void Main(string[] args) {
// Father class
Father f = new Father();
f.Display();
// Son class
Son s = new Son();
s.Display();
s.DisplayOne();
Console.ReadKey();
}
class Father {
public void Display() {
Console.WriteLine("Display");
}
}
class Son : Father {
public void DisplayOne() {
Console.WriteLine("DisplayOne");
}
}
}
}
输出
Display Display DisplayOne

