[英]C# Use base class property value in a derived class property override
My class structure is as follows: 我的班级结构如下:
public class Animal
{
private int _animalId;
public virtual int AnimalId
{
get { return _animalId; }
}
}
public class Dog : Animal
{
public override int AnimalId
{
get
{
if (Request.Params["New_Animal"] == "true")
return -1;
else
return AnimalId;
}
}
}
I would like to override the AnimalId
property as follows: if it is a new animal the id should be -1, but if we are updating an existing animal I would like to return the AnimalId
from the base class. 我想覆盖AnimalId
属性,如下所示:如果它是一个新动物,id应为-1,但如果我们要更新现有动物,我想从基类返回AnimalId
。
This is an extremely simplified example, but I'm wondering if / how this can be done. 这是一个极其简化的例子,但我想知道是否/如何做到这一点。
Thanks 谢谢
Use base.AnimalId 使用base.AnimalId
public class Animal
{
private int _animalId;
public virtual int AnimalId
{
get { return _animalId; }
}
}
public class Dog : Animal
{
public override int AnimalId
{
get
{
if (Request.Params["New_Animal"] == "true")
return -1;
else
return base.AnimalId;
}
}
}
您可以将动物中的animalId设为私人保护,然后您可以在该属性中返回base.animalid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.