这意味着什么? [c#]
如果我们将一个属性定义为 public
属性,并且在该属性中我们有一个 protected
getter。这意味着什么?如果财产是公共的,那么为其定义受保护的 getter 意味着什么? 请看下面的代码:
public ISessionFactory SessionFactory
{
protected get { return sessionFactory; }
set { sessionFactory = value; }
}
If we define a property as public
property and in this property we have a protected
getter. what does it means? if property is public, what does defining a protected getter for that, means?
please see below code:
public ISessionFactory SessionFactory
{
protected get { return sessionFactory; }
set { sessionFactory = value; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这意味着,getter 只能由子类调用。可以说,吸气剂之前的“受保护”覆盖了属性吸气剂部分的“公共”。
It means, that the getter can only be called by subclasses. The 'protected' before the getter so to say overwrites the 'public' for the getter part of the property.
在 C# 中,您可以拥有具有访问级别 (请参阅与整体属性不同的访问修饰符)。这是最常见的模式,
它允许实例化 FooObject 的对象检索 Foo 的值,但不设置它的值。 setter 上的 private 修饰符表示只有 FooObject 本身具有此能力(不包括使用反射)。
这样做有两个优点:
通过添加自动属性(不需要变量分配 get 和 set 值),这允许完成属性变量的私有设置(它是在编译时为您创建的)因此无需显式创建变量。如果没有这个,你就不能使用自动属性,除非你总是希望 get 和 set 函数全部是公共的、全部私有的等等。
它允许一定程度的抽象,以便所有方法,无论是公共的、私有的还是其他方法遍历属性而不是直接访问私有变量。请参阅此问题了解更多信息。
在您的实例中,其他对象可以设置会话工厂值,但只有从它继承的类可以检索它。大多数时候,如果一个对象可以设置该值,它也可以检索该值,但在某些情况下,不允许它会有利。这是允许的,因为设置事件不允许比为整个属性定义的访问量更大的访问量。
我能想到的最好的例子是,如果在设置事件中,设置对象被修改。例如,如果设置事件正在设置连接对象,并且在设置事件中,添加了连接字符串并打开了与数据库的连接(在我的示例中,我可能会重构代码以不以这种方式操作,但可能会出现类似的情况)。
In C# you are allowed to have getters and setters with access levels (see access modifiers) different than the overall property. This is the most common pattern
This allows objects instantiating FooObject to retrieve the value of Foo but not set it's value. The private modifier on the setter signifies that only FooObject itself has this ability (not including the use of reflection).
There are two advantages to this:
With the addition of automatic properties (no variables needed assign the get and set value), this allows the private setting of the property variable (it's created for you at compile time), to be done so without having to explicitly create the variable. Without this you couldn't use an automatic property, unless you always wanted the get and set function to be all public, all private etc.
It allows a level of abstraction, so that all methods whether public, private, or otherwise to go through the property and not directly access the private variable. See this question for more information.
In your instance other objects may set the session factory value, but only classes which inherit from it may retrieve it. Most of the time, if an object can set the value, it can also retrieve it, but there are instances where it would beneficial to not allow it. This is allowed, because the set event does not allow a greater amount of access than what was defined for the overall property.
The best example I can think of would be if on the set event, the set object was modified. For example, if it the set event was setting a connection object, and on the setting event, the connection string was added and the connection to the database was opened (In my example, I would probably refactor the code not to act in this way, but something like that could arise).
protected 关键字是成员访问修饰符。受保护的成员可以从声明它的类中以及从声明该成员的类派生的任何类中访问。
http://msdn.microsoft.com/en-us /library/bcd5672a(VS.71).aspx
The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.
http://msdn.microsoft.com/en-us/library/bcd5672a(VS.71).aspx
protected get 意味着该属性的 getter 只能从该类的继承类访问。该集合被假定为公开的,因此该属性可以公开设置。
The protected get means that this property's getter can only be accessed from a inherited class of this class. the set is assumed public so this property can be setted publicly.