c# - 覆盖派生类中的默认值 (c#)

标签 c# default overriding

如果我在基类中有一个访问器和默认属性,如下所示:

class base{
protected int _foo = 5;
public int foo {get{return _foo;}set{_foo = value;}}
}

那我派生这个类,是否可以覆盖_foo的默认值?

class derived:base{
// foo still returns 5?
protected new int _foo = 10;
}

最佳答案

_foo = 5 语句有效地在基类的构造函数中执行。您可以将代码添加到派生类构造函数中,该构造函数随后立即更改 foo 的值:

class derived:base{
    public derived()
    {
        foo = 10;
    }
}

关于c# - 覆盖派生类中的默认值 (c#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3510783/

相关文章:

c# - Linq 查询从 Sql 数据库返回重复记录

c# - 将 2 个字节转换为有符号整数

c++ - 如何启用默认构造函数?

python 在将参数传递给 init 时引用类变量

JAVA,无法绕过更具体的直接父类(super class)型

language-agnostic - 轮询 : Correct behavior of equality when passed object does not match LHS type?

c# - DynamicDictionary在ElasticSearch 2.0(Elasticsearch.Net)上不再可用

c# - Unity容器,解析单个对象

sqlite - 未捕获的TypeError:对象app.data.proxy.CustomSql没有方法“应用” sencha touch

java - 抛出时自定义 Java 异常打印