我有一个情况:
class Dog : Animal {
...
}
我想将其更改为:
class Dog {
...
}
其中Animal中的所有成员变量和方法都复制到Dog中。
有没有办法做到这一点?
只需在基类中使用protected
关键字在您要继承它的属性上并在基类中使用virtual
作为方法和子类中的override
示例:
class Animal
{
protected string Name { set; get; }
protected string Type { set; get; }
public virtual void Test()
{
//some code
}
}
class Dog : Animal
{
public Dog(string name)
{
this.Name = name;
this.Type = "Dog";
}
public override void Test()
{
//this call the test function in the base class
base.Test();
}
}