Tar*_*rik 1 c# inheritance class
我在想这样的场景:
class Utopia => 一个基类,它将字段和方法传递给派生类.
class Watashi => 派生类,派生自Utopia并继承所有内容
class Baka => 派生类,从Utopia继承一些字段
上面有一些类型和类型Baka应该继承一些特定的字段和方法,但如何?我怎样才能指定只有Baka会继承的字段和方法,而Watashi从Utopia继承了一切.
示例代码:
class Utopia {
public string Moshi;
[Exclude(ClassName("Baka"))]
public string HaveIt;
}
class Baka : Utopia
{
// only Moshi appears here
base.[Moshi]
}
class Watashi : Utopia
{
base.[Moshi][HaveIt];
}
Run Code Online (Sandbox Code Playgroud)
如果我想使用多态性:
Utopia _utopiaBaka = new Baka();
_utop.[Moshi];
Utopia _utopiaWatashi = new Watashi();
_utopiaWatashi.[Moshi][HaveIt];
Run Code Online (Sandbox Code Playgroud)
当然,Framework还会检查派生类是否是其他类型的基类.
将乌托邦划分为多个班级.有一个班级,任何班级都可以继承,所以巴卡会从中继承.
然后,扩展这个PartialUtopia类,添加其余的方法并让Watashi从中继承.
通过这种方式,您可以让类通过它们扩展的基类来选择所需的方法.