}
Accessor usuli uchun Accessor modifikatoridan foydalanganda ushbu modifikator ushbu usul uchun kirish domenini belgilaydi.
Agar kiruvchiga kirish modifikatori qo'llanilmasa, ushbu usulning kirish domeni mulk yoki indeksatorning kirish darajasi bilan belgilanadi.
Misol Quyidagi misol, uch sinflarni belgilaydi BaseClass, DerivedClassva MainClass. Sinfda ikkala sinf uchun ham xususiyatlari va aniqlanadi . Bu, masalan, bir, qanday qilib ko'rsatadi sinf mulk bir tomonidan yashirin bo'lishi mumkin sinf mol-mulk sifatida modifikatori bunday cheklov uchun ruxsatdan foydalanayapsiz qachon himoyalangan yoki xususiy . Shunday qilib, ushbu xususiyatga qiymatlarni berishda uning o'rniga class xususiyati chaqiriladi . Ushbu xususiyatni taqdim etish uchun siz kirish modifikatorini public bilan almashtirishingizkerak . BaseClassNameId IdDerivedClassIdBaseClass BaseClas
Ushbu misol, shuningdek , sinfdagi mulkka kiruvchi uchun cheklovli kirish modifikatori ( privateyoki protected) ushbu usulga kirishni rad etishini va unga qiymat berishga urinishda xatolikni qaytarishini namoyish etadi.setNameDerivedClass
C #
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 { }
}
}
public class DerivedClass : BaseClass
{
private string _name = "Name-DerivedClass";
private string _id = "ID-DerivedClass";
new public string Name
{
get
{
return _name;
}
// Using "protected"
// Using "protected" would make the set accessor not accessible.
set
{
_name = value;
}
}
// Using private on the following property hides it in the Main Class.
// Any assignment to the property will use Id in BaseClass.
new private string Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
}
class MainClass
{
static void Main()
{
BaseClass b1 = new BaseClass();
DerivedClass d1 = new DerivedClass();
b1.Name = "Mary";
Do'stlaringiz bilan baham: |