Access Modifiers on Overriding Accessors 


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

Access Modifiers on Overriding Accessors



When you override a property or indexer, the overridden accessors must be accessible to the overriding code. Also, the accessibility level of both the property/indexer, and that of the accessors must match the corresponding overridden property/indexer and the accessors. For example:

public class Parent { public virtual int TestProperty { // Notice the accessor accessibility level. protected set { }   // No access modifier is used here. get { return 0; } } } public class Kid: Parent { public override int TestProperty { // Use the same accessibility level as in the overridden accessor. protected set { }   // Cannot use access modifier here. get { return 0; } } }

 


Модификаторы доступа в переопределяющих методах доступа

При переопределении свойства или индексатора переопределенные методы доступа должны быть доступны переопределяющему коду. Кроме того, уровень доступности свойства/индексатора, а также методов доступа должен перехватывать соответствующее свойство/индексатор и методы доступа. Пример.

public class Parent { public virtual int TestProperty { // Notice the accessor accessibility level. protected set { }   // No access modifier is used here. get { return 0; } } } public class Kid: Parent { public override int TestProperty { // Use the same accessibility level as in the overridden accessor. protected set { }   // Cannot use access modifier here. get { return 0; } } }

 


Implementing Interfaces

When you use an accessor to implement an interface, the accessor may not have an access modifier. However, if you implement the interface using one accessor, such as get, the other accessor can have an access modifier, as in the following example:

public interface ISomeInterface { int TestProperty { // No access modifier allowed here // because this is an interface. get; } }   public class TestClass: ISomeInterface { public int TestProperty { // Cannot use access modifier here because // this is an interface implementation. get { return 10; }   // Interface property does not have set accessor, // so access modifier is allowed. protected set { } } }

 


Реализация интерфейсов

При использовании метода доступа для реализации интерфейса, метод доступа не может иметь модификатор доступа. Однако если интерфейс реализуется при помощи одного метода доступа, такого как get, другой метод доступа может иметь модификатор доступа, как в следующем примере:

public interface ISomeInterface { int TestProperty { // No access modifier allowed here // because this is an interface. get; } }   public class TestClass: ISomeInterface { public int TestProperty { // Cannot use access modifier here because // this is an interface implementation. get { return 10; }   // Interface property does not have set accessor, // so access modifier is allowed. protected set { } } }

 


Accessor Accessibility Domain

If you use an access modifier on the accessor, the accessibility domain of the accessor is determined by this modifier.

If you did not use an access modifier on the accessor, the accessibility domain of the accessor is determined by the accessibility level of the property or indexer.

Example

The following example contains three classes, BaseClass, DerivedClass, and MainClass. There are two properties on the BaseClass, Name and Id on both classes. The example demonstrates how the property Id on DerivedClass can be hidden by the property Id on BaseClass when you use a restrictive access modifier such as protected or private. Therefore, when you assign values to this property, the property on the BaseClass class is called instead. Replacing the access modifier by public will make the property accessible.

The example also demonstrates that a restrictive access modifier, such as private or protected, on the set accessor of the Name property in DerivedClass prevents access to the accessor and generates an error when you assign to it.

public class BaseClass { private string name = "Name-BaseClass"; private string id = "ID-BaseClass";   public string Name { get { return name; } set { } }   public string Id { get { return id; } set { } } }



Поделиться:


Последнее изменение этой страницы: 2017-01-19; просмотров: 97; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 18.224.32.86 (0.004 с.)