属性的获取/设置方法上可能有不同的访问修饰符吗?证明合法 [英] Is it possible to have different access modifiers on the get/set methods of a property? Justify

查看:80
本文介绍了属性的获取/设置方法上可能有不同的访问修饰符吗?证明合法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
是否可以为属性的get和set方法使用访问修饰符?

它仅在属性声明时有效吗?

预先感谢

Hello friends,
Is it possible to have access modifiers for get and set method of a property??
Or
Is it only valid at property declaration?

Thanks in advance

推荐答案

是的-但是所有访问修饰符都在声明该项时设置,此后不能更改.

不同的修饰符对于属性特别有用,因为它们允许外界对变量的访问受到限制.

例如,您可能具有字符串属性:
Yes - but all access modifiers are set when you declare the item and cannot be changed after that.

Different modifiers are particularly useful with properties, as they allow the outside world to have limited access to your variables.

For example, you might have a string property:
public string Text { get; private set; }

外部世界可以随时读取您正在处理的文本,但是只有创建该属性的类才可以对其进行修改.

Where the outside world can read the text you are processing at any time, but only the class that created the property can ever modify it.


这篇关于属性的获取/设置方法上可能有不同的访问修饰符吗?证明合法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆