Skip to content

JA CSharp Inheritance Interfaces

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

継承、インターフェース、ポリモーフィズム

マニュアルC# · 例題UO

クラスには 1 つの基本クラスがあり、複数のインターフェイスを実装できます。 virtual はオーバーライドを許可し、override はそれを実装し、abstract は継承者による実装を必要とし、sealed は継承を制限します。 base は基礎となる実装を指します。 new はメンバーを非表示にし、override を置き換えません。

簡単な例 (Example.cs)

この例を 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# は、適切なジェネリック コントラクト内のインターフェイスのデフォルト実装と静的抽象メンバーをサポートしています。

公式ドキュメント: 継承、インターフェイス、ポリモーフィズム - Microsoft Learn。

Clone this wiki locally