访问器的目的是什么? [英] What is the purpose of accessors?
问题描述
有人可以帮助我理解获取
& set
?
为什么需要它们?我可以只设置一个公共变量。
Can somebody help me understand the get
& set
?
Why are they needed? I can just make a public variable.
推荐答案
警告:我假设您已经
什么是属性?
属性是语言元素,可让您避免使用重复的 getXYZ()
访问器和 setXYZ()
变异器技术
Warning: I am assuming you already know about object-oriented programming.
What are properties?
Properties are language elements that allow you to avoid the repetitive getXYZ()
accessors and setXYZ()
mutators techniques found in other languages, like Java.
它们旨在解决以下问题:
They aim to solve the following problems:
-
说
get
和set
在每次访问或值突变的开始都是令人讨厌和分散注意力的。
Saying
get
andset
in the beginning of every access or mutation of a value is annoying and distracting.
在Java中,您通常会说:
In Java, you often say:
class person
{
private int _age;
public void setAge(int value) { /*check value first, then set _age*/ }
public int getAge() { return this._age; }
}
然后不断说:
if (person.getAge() > blah || person.getAge() < 10)
{
person.setAge(5);
}
过一会儿,得到
和 set
变得很烦人。
After a while, the get
and set
become rather annoying.
提供对实际变量的直接访问会破坏封装,所以这不是一个选择。
Providing direct access to the actual variable breaks encapsulation, so that's not an option.
如何使用?
使用 就像变量一样。您像对变量一样对它们进行读/写。
How are they used?
They are used just like variables. You read/write to them just like variables.
它们是 创建的方法。您定义了一对方法:
They are created as methods. You define a pair of methods that:
-
返回属性的当前值。通常,它只不过是以下内容:
Return the current value of the property. Oftentimes, this is nothing more than something like the following:
class Person
{
private int _age; //Declare the backing field
public int Age
{
get { return this._age; }
set { ... }
}
}
设置属性的值:
Set the value of the property:
class Person
{
public int Age
{
get { ... }
set
{
if (value < 0) //'value' is what the user provided
{ throw new ArgumentOutOfRangeException(); } //Check validity
this._age = value;
}
}
}
其他说明:
自动实现的属性
C#3.0引入了自动实现的属性:
Other notes:
Auto-implemented Properties
C# 3.0 introduced auto-implemented properties:
public int Age { get; set; }
这等效于:
private int _age; //The name is auto-generated
public int Age { get { return this._age; } set { this._age = value; } }
为什么存在?
它可以帮助您避免破坏客户端可执行文件中的更改。
假设您很懒,不想键入整个内容,并决定公开展示变量。然后,您创建一个可读取或写入该字段的可执行文件。然后,您改变主意,并决定实际上需要某个属性,因此将其更改为一个。
Let's say you're lazy and don't want to type the whole thing, and decide to expose a variable publicly. You then create an executable that reads from or writes to that field. Then you change your mind and decide that you in fact needed a property, so you change it to one.
会发生什么?
依赖的可执行文件会中断,因为代码不再有效。
The depending executable breaks, because the code is no longer valid.
自动实现的属性可以帮助您避免这种情况,而无需
Auto-implemented properties help you avoid that, without extra redundancy in your initial code.
Indexers扩展了属性语法,使您可以 index 对象(惊奇!),就像数组一样。
对于C ++用户:这类似于重载 operator []
。
Indexers extend the property syntax to let you index objects (surprise!), just like arrays.
For C++ users: This is similar to overloading operator []
.
例如:
private int[] _elements;
public int this[int index] //Indexed property
{
get { return this._elements[index]; }
set
{
//Do any checks on the index and value
this._elements[index] = value;
}
}
然后像一样使用它们obj [5] = 10;
,等效于调用 obj
set 方法>的索引器。
实际上, System.Collections.Generic.List< T>
被索引:
You then use them like obj[5] = 10;
, which is equivalent to calling the set
method of obj
's indexer.
In fact, System.Collections.Generic.List<T>
is indexed:
var list = new List<int>();
list.Add(10);
list[0] = 5; //You're indexing list, as though it were an array!
不是很整洁吗? :)
属性还有很多其他功能,但并非所有功能都可以在C#:
There are many more features to properties, not all of which are available in C#:
- 参数化的属性,其中索引器是一种特殊的属性
- 字母/字母访问修饰符(在C#中)
- 多个getter或setter(不在C#中)
- Et cetera
- Parametrized properties, of which indexers are a special kind
- Getter/setter access modifiers (in C#)
- Multiple getters or setters (not in C#)
- Et cetera
这篇关于访问器的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!