在 C# 中,继承是可能的,作为一流的概念:
public class BaseClass
{
public virtual string Name { get { return "Adam"; } }
}
public class ChildClass : BaseClass
{
public override string Name { get { return "Foo"; } }
}
对于override
某些东西,您需要实现它virtual
并且需要具有除private
.
在您的情况下,您可能会执行以下操作:
class clsMain
{
public virtual int P1{set; get;}
public virtual int P2{set; get;}
public virtual int P3{set; get;}
public virtual string P4{set; get;}
public virtual string P5{set; get;}
}
class object1 : clsMain
{
public override int P1{set; get;}
public override int P2{set; get;}
public override int P3{set; get;}
}
class object2 : clsMain
{
public override int P3{set; get;}
public override string P4{set; get;}
public override string P5{set; get;}
}
虽然如果我说实话,你的问题不是很清楚。但是请注意,C# 不支持类的多重继承。
听起来你想过滤掉属性。您不能阻止继承的成员被访问(嗯,有new
修饰符,但这充其量只是粗略)。相反,您可以使用接口:
interface Iobject1
{
int P1 { set; get; }
int P2 { set; get; }
int P3 { set; get; }
}
interface Iobject2
{
int P3{set; get;}
string P4{set; get;}
string P5{set; get;}
}
class clsMain : Iobject1, Iobject2
{
public int P1{set; get;}
public int P2{set; get;}
public int P3{set; get;}
public string P4{set; get;}
public string P5{set; get;}
}