How to extend a readonly property ?????

简介:

BloodyAngel(9474649) 11:24:33
问下override的问题:

在C#中,基类如果定义了一个只读了属性,如:
class Parent{
public abstract string Name{get;}
}

子类Child中,如何把这个属性扩展为可写的?如:
public override string Name{
    get{return this._name;}
    set{this._name = value;}
}

语法层面上,会报错。但是我觉得在逻辑层面上,出现以上扩展是很正常的。


凹凸(360232736) 11:25:30
将override改成virtual


BloodyAngel(9474649) 11:26:34
不符合逻辑,因为父类无法实现此属性。


北田.(7376854) 11:26:40
这样不行吧。。

北田.(7376854) 11:27:44
我觉得只能用new关键字覆盖。。。

凹凸(360232736) 11:28:01
我理解错了,我以为是让这个child的字类继续重写。

北田.(7376854) 11:28:25
感觉没有办法扩展。。。


凹凸(360232736) 11:28:26
同意北田的观点


凹凸(360232736) 11:28:36
只能覆盖


凹凸(360232736) 11:29:23
如果你能随意扩展,那接口的设计和基类的设计就有一定的危险性,因为它不能充分信任字类的行为。


BloodyAngel(9474649) 11:29:27
很郁闷的问题 .


北田.(7376854) 11:29:42
你为什么要扩展呢?

BloodyAngel(9474649) 11:30:03
我现在的方法是:
class Child
public void SetName(string value){..}


北田.(7376854) 11:30:21
我觉得通过其他途径可以解决这个问题。。


凹凸(360232736) 11:30:51
你这样做,虽然从语法层面解决了,但其实破坏了自己的设计,得不偿失。

BloodyAngel(9474649) 11:32:17
这样设计,逻辑上还算可以理解吧:
父类规定了所有子类都有一个可读的Name属性。
子类满足以上规定,并有一个SetName的行为改变 .

说白了,就是没办法走属性那套 .

 

凹凸(360232736)  11:32:45
那为何不在父类中提供set呢?


BloodyAngel(9474649)  11:34:10
父类说了,不可以set。嘿嘿。

 

凹凸(360232736)  11:35:48
就把你的关键字从override改成new就行了


哇哈哈(306121260)  11:35:58
将override改成new 试试


凹凸(360232736)  11:36:45
但是,你现在解决了这个问题,显然你的设计是存在一些矛盾的,如果都用这种New的方法来解决,那多个子类就会与父类之间产生不一致了。


北田.(7376854)  11:37:12
同意凹凸的说法。。


哇哈哈(306121260)  11:37:45
没必要的话,不要刻意去追求继承


北田.(7376854)  11:39:00
接口的作用就是这个。。说明当时在设计父类的时候他的目的就是不允许改的。。如果现在硬要改那 你刚才那种public void SetName(string value){..}
就可以。。这这样做感觉很不优雅。。


凹凸(360232736)  11:39:01
在设计原则中有一条是少用继承多用接口。
但我们无法从这两个类中来判断是用继承和接口,只是,继承的时候尽量保持派生类和基类的一致性吧。


BloodyAngel(9474649)  11:39:24
还有,方案二

public override string Name{get{return this.RealName;}}

public string RealName{get;set;}


这个是目前我觉得最好的方案了  逻辑上觉得不错,语法上也还行。


凹凸(360232736) 11:40:06
你用setName和RealName效果是一样的。


BloodyAngel(9474649) 11:40:37
这里的意思是说,子类使用RealName来欺骗式的实现了父类的Name


BloodyAngel(9474649) 11:40:51
都不怎么样,哈哈。

目录
相关文章
|
17天前
|
JavaScript
const和readonly的区别
const和readonly的区别
|
2月前
|
JavaScript 安全
Cannot read property ‘querySelectorAll‘ of undefined问题解决
Cannot read property ‘querySelectorAll‘ of undefined问题解决
26 2
|
2月前
|
JavaScript
【报错】Cannot read property ‘parseComponent‘ of undefined
【报错】Cannot read property ‘parseComponent‘ of undefined
127 2
|
9月前
|
Java 数据库连接 mybatis
There is no getter for property named ‘null‘ in ‘class
There is no getter for property named ‘null‘ in ‘class
114 0
There is no getter for property named ‘null‘ in ‘class
|
JavaScript 定位技术
Cannot read property 'echarts' of undefined
Cannot read property 'echarts' of undefined
136 0
|
JavaScript 前端开发
vue+ts:Property ‘searchMes‘ is private and only accessible within class ‘HomeComponent‘.
vue+ts:Property ‘searchMes‘ is private and only accessible within class ‘HomeComponent‘.
165 0
vue+ts:Property ‘searchMes‘ is private and only accessible within class ‘HomeComponent‘.
|
JavaScript
void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property b
void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: vue项目示例,请参考甄佰 单向数据流所有的 prop 都使得其父子 prop 之间形成了一个单向下行绑定:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。
2864 0