Repository navigation
ZH TW CSharp Inheritance Interfaces
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
一個類別有一個基類,可以實作多個介面。 virtual允許覆蓋,override實現它,abstract需要繼承人實現,sealed限制繼承。 base指底層實作;new 隱藏成員且不會取代override。
請將此範例另存為 Example.cs,並與本頁其他範例分開執行。
Example.cs:
// PROJECT-EXAMPLE
ILabel item = new Item();
System.Console.WriteLine(item.Label);
interface ILabel { string Label { get; } }
class Item : ILabel { public string Label => "Ore"; }結果:
Ore
interface IDescription
{
string Describe();
}
abstract class Job : IDescription
{
public abstract string Describe();
}
sealed class GatherJob : Job
{
public override string Describe() => "Gathering job";
}
IDescription job = new GatherJob();
UO.Print(job.Describe());class NamedJob
{
public string Name { get; }
public NamedJob(string name) => Name = name;
public virtual string Report() => Name;
}
sealed class TimedJob : NamedJob
{
public TimedJob(string name) : base(name) { }
public override string Report() => base.Report() + " — timed";
}
UO.Print(new TimedJob("Search").Report());介面描述的是合同,而不是已完成的遊戲操作。介面的明確實作可讓您分隔同名的成員。現代C#支援適當的通用合約中介面的預設實作和靜態抽象成員。
ClassicUO — Bad Newbie & C# Scripts IDE