关于属性及其用法 [英] On properties and their usage

查看:105
本文介绍了关于属性及其用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


好​​的,首先我知道自VB6以来的属性,这是我已经广泛使用的
。在C#和VB.NET中,似乎属性得到了
和set基本相同的概念,所以

没问题。在使用VB6进行编程时,我从来没有写过任何使用过属性的

程序,到目前为止,我仍然没有在C#中使用它们。我真正想知道的是,我应该什么时候才能使用它们?我的意思是,我们有变量(我认为现在称为

字段),可以公开或私有,但所有

时间我看到代码:


- 首先将字段声明为私有

- 然后使用公共财产读取或写入私有字段


现在,我可以理解当你需要一个只读字段时,它们会如何真正方便,但对于我的生活,我看不到

点为某个类创建另一个成员时除了向/从私有字段分配/读取值之外什么都没有。

可能刚刚开始公开。我还想

表示如果你想在阅读或写完后向现场做点什么,那么我可以看到它对他们很有用。

只能用于物业。毕竟,一个公共领域是

从它之外的代码访问它包含的类就像一个

属性,所以为什么要打扰这样的东西:


private int MyInt;


public int MyIntP

{

get

{

返回MyInt;

}

set

{

MyInt =值;

}

}


现在,如果设置部分是这样的:

......

套装

{

MyInt =价值* 2;

}

......

我能理解没有问题。


在我看来,它确实完全正确和正常的公共场地一样。我想听听一个解释,为什么这个

经常被使用,当财产对私人领域什么都不做时

除了读/写它的价值。如果我在这里遗漏了一些非常重要的东西,我想了解它,但如果没有,我也想知道为什么这么多人使用它们(b
)经常)如果没有合法的需求。


谢谢。

Hello all,

OK, first of all I have known about properties since VB6, which
I have and have used extensively. It seems that property get
and set are basically the same concept in C# and VB.NET, so
no problem there. In programming with VB6, I never wrote any
program that used properties, and to date I still haven''t ever
used them in C#. What I really want to know is, when should I
use them at all? I mean, we have variables (now referred to as
fields, I think), which can be made public or private, but all the
time I see code which:

- first declares a field as private
- then uses a public property to read or write to the private field

Now, I can understand how they would be really handy when
you need a read-only field, but for the life of me I cannot see the
point in creating yet another member for a class that does
nothing except assign/read a value to/from a private field that
could have just been made public to begin with. I also want to
state that I can see a good use for them if you wanted to DO
something to the field after it''s read or written, and that''s the
only use I can see for properties. After all, a public field is
accessed from code outside it''s containing class just like a
property is, so why bother with something like:

private int MyInt;

public int MyIntP
{
get
{
return MyInt;
}
set
{
MyInt = value;
}
}

Now, if the set part went like this:
......
set
{
MyInt = value * 2;
}
......
I could understand that with no problem.

It just seems to me that it does exactly the same thing as a normal
public field does. I would like to hear an explanation as to why this
is used so often when the property does nothing to the private field
except to read/write its value. If I''m missing something really
important here, I want to know about it, but if not, I also want to
know why so many people use them (constantly) if there is not a
legitimate need.

Thanks.

推荐答案



" Gary Morris" <克******* @ carolina.rr.com>在消息中写道

news:ew ************** @ tk2msftngp13.phx.gbl ...

"Gary Morris" <gm*******@carolina.rr.com> wrote in message
news:ew**************@tk2msftngp13.phx.gbl...
大家好,

好的,首先我知道自VB6以来的属性,我已经广泛使用过。看来属性得到和设置在C#和VB.NET中的概念基本相同,所以
没有问题。在使用VB6进行编程时,我从未编写任何使用过属性的程序,到目前为止,我还没有在C#中使用它们。我真正想知道的是,我什么时候应该使用它们?我的意思是,我们有变量(现在称为
字段,我认为),可以公开或私有,但所有的时间我看到代码:

- 首先声明一个字段为私有
- 然后使用公共财产来读取或写入私有字段

现在,我可以理解它们如何真正方便当你/你需要一个只读字段,但对于我的生活,我无法看到创建另一个成员的另一个成员除了为私有字段分配/读取值之外什么都没有。
可能刚开始公开。我还想声明,如果你想在阅读或写完之后做些什么,那么我可以看到它对他们很有用,那就是
只使用我能看到的属性。毕竟,公共领域是从代码之外的代码访问的,它包含类就像
属性一样,所以为什么还要烦恼:

private int MyInt;

public int MyIntP

{
返回MyInt;
}
设置
{
MyInt = value;
}
}
现在,如果设置部分是这样的:
.....
设置
{
MyInt = value * 2;
}
.....
我明白没问题。

它似乎对我而言,它与正常的公共领域完全相同。我希望听到一个解释,为什么这个
经常被使用,当财产对私人领域什么都不做时
除了读/写它的价值。如果我错过了一些非常重要的东西,我想知道它,但如果没有,我也想知道为什么这么多人使用它们(不断)如果没有<合法需要。


这是一个相当常见的问题,基本上,它的完成只是因为它的良好实践并提供了一致性。通过始终使用属性,每个

类总是使用属性,并且更容易理解。我个人

写*方式*比read\write更多只读属性,所以提供一个

一致的接口我在写一个read\write时使用属性物业。

当然使用属性也意味着您还可以在设置处理程序中输入验证码,

事件等,更改后备存储,虚拟和

否则将客户代码与你班级的内部人员分开。


但是,我只会在公众和受保护的人身上遵循这些规则

成员。对于内部成员,您可以随意使用任何您想要的东西,使用

属性,您可能会关注简单的

类的内容和字段。然而,我总是写一个严格的属性或严格地给定类中的
字段,再次保持一致性(字段的问题

这里是如果我改变一个属性我通常将所有其他人改为

属性。)
谢谢。
Hello all,

OK, first of all I have known about properties since VB6, which
I have and have used extensively. It seems that property get
and set are basically the same concept in C# and VB.NET, so
no problem there. In programming with VB6, I never wrote any
program that used properties, and to date I still haven''t ever
used them in C#. What I really want to know is, when should I
use them at all? I mean, we have variables (now referred to as
fields, I think), which can be made public or private, but all the
time I see code which:

- first declares a field as private
- then uses a public property to read or write to the private field

Now, I can understand how they would be really handy when
you need a read-only field, but for the life of me I cannot see the
point in creating yet another member for a class that does
nothing except assign/read a value to/from a private field that
could have just been made public to begin with. I also want to
state that I can see a good use for them if you wanted to DO
something to the field after it''s read or written, and that''s the
only use I can see for properties. After all, a public field is
accessed from code outside it''s containing class just like a
property is, so why bother with something like:

private int MyInt;

public int MyIntP
{
get
{
return MyInt;
}
set
{
MyInt = value;
}
}

Now, if the set part went like this:
.....
set
{
MyInt = value * 2;
}
.....
I could understand that with no problem.

It just seems to me that it does exactly the same thing as a normal
public field does. I would like to hear an explanation as to why this
is used so often when the property does nothing to the private field
except to read/write its value. If I''m missing something really
important here, I want to know about it, but if not, I also want to
know why so many people use them (constantly) if there is not a
legitimate need.
This is a fairly common question, and basically, its done simply because its
good practice and provides consistency. By always using properties, every
class always uses properties, and its easier to understand. I personally
write *way* more read only properties than read\write, so to provide a
consistent interface I use properties when I do write a read\write property.
Of course using properties also means you can also put in validation code,
events, etc on the set handler, change the backing storage, be virtual and
otherwise decouple client code from the internals of your class.

However, I only follow those rules nessecerily on public and protected
members. For internal members you are free to use whatever you want, using
properties where you may be concerned with something and fields for simple
classes. I however always write either strictly properties or strictly
fields in a given class, again to retain consistency(problem with fields
here is if I change one to a property I usually change all others to
properties too).
Thanks.



Gary,
除了丹尼尔的评论之外。


恕我直言最好将你的字段设为私有并使用属性作为它

促进封装。


记住Encapsulation是OOP的前三大租户之一。另外两个是

继承和多态。


属性是封装字段或属性数据的.NET方法。


此外,框架中的某些类与属性很好地匹配,而它们完全忽略公共字段。比如System.Windows.Forms.PropertyGrid

和数据绑定。


希望这有帮助

Jay


" Gary Morris" <克******* @ carolina.rr.com>在消息中写道

news:ew ************** @ tk2msftngp13.phx.gbl ...
Gary,
In addition to Daniel''s comments.

IMHO it is better to make your fields Private and use Properties as it
promotes encapsulation.

Remember Encapsulation is one of the top 3 tenants of OOP. The other 2 being
Inheritance and Polymorphism.

Properties are .NET method of encapsulating field or attribute data.

Also some classes in the Framework play nicely with properties while they
totally ignore public fields. Such as the System.Windows.Forms.PropertyGrid
and Data Binding.

Hope this helps
Jay

"Gary Morris" <gm*******@carolina.rr.com> wrote in message
news:ew**************@tk2msftngp13.phx.gbl...
大家好,

好的,首先我知道自VB6以来的属性,我已经广泛使用过。看来属性得到和设置在C#和VB.NET中的概念基本相同,所以
没有问题。在使用VB6进行编程时,我从未编写任何使用过属性的程序,到目前为止,我还没有在C#中使用它们。我真正想知道的是,我什么时候应该使用它们?我的意思是,我们有变量(现在称为
字段,我认为),可以公开或私有,但所有的时间我看到代码:

- 首先声明一个字段为私有
- 然后使用公共财产来读取或写入私有字段

现在,我可以理解它们如何真正方便当你/你需要一个只读字段,但对于我的生活,我无法看到创建另一个成员的另一个成员除了为私有字段分配/读取值之外什么都没有。
可能刚开始公开。我还想声明,如果你想在阅读或写完之后做些什么,那么我可以看到它对他们很有用,那就是
只使用我能看到的属性。毕竟,公共领域是从代码之外的代码访问的,它包含类就像
属性一样,所以为什么还要烦恼:

private int MyInt;

public int MyIntP

{
返回MyInt;
}
设置
{
MyInt = value;
}
}
现在,如果设置部分是这样的:
.....
设置
{
MyInt = value * 2;
}
.....
我明白没问题。

它似乎对我而言,它与正常的公共领域完全相同。我希望听到一个解释,为什么这个
经常被使用,当财产对私人领域什么都不做时
除了读/写它的价值。如果我错过了一些非常重要的东西,我想知道它,但如果没有,我也想知道为什么这么多人使用它们(不断)如果没有<合法需要。

谢谢。
Hello all,

OK, first of all I have known about properties since VB6, which
I have and have used extensively. It seems that property get
and set are basically the same concept in C# and VB.NET, so
no problem there. In programming with VB6, I never wrote any
program that used properties, and to date I still haven''t ever
used them in C#. What I really want to know is, when should I
use them at all? I mean, we have variables (now referred to as
fields, I think), which can be made public or private, but all the
time I see code which:

- first declares a field as private
- then uses a public property to read or write to the private field

Now, I can understand how they would be really handy when
you need a read-only field, but for the life of me I cannot see the
point in creating yet another member for a class that does
nothing except assign/read a value to/from a private field that
could have just been made public to begin with. I also want to
state that I can see a good use for them if you wanted to DO
something to the field after it''s read or written, and that''s the
only use I can see for properties. After all, a public field is
accessed from code outside it''s containing class just like a
property is, so why bother with something like:

private int MyInt;

public int MyIntP
{
get
{
return MyInt;
}
set
{
MyInt = value;
}
}

Now, if the set part went like this:
.....
set
{
MyInt = value * 2;
}
.....
I could understand that with no problem.

It just seems to me that it does exactly the same thing as a normal
public field does. I would like to hear an explanation as to why this
is used so often when the property does nothing to the private field
except to read/write its value. If I''m missing something really
important here, I want to know about it, but if not, I also want to
know why so many people use them (constantly) if there is not a
legitimate need.

Thanks.



人们为什么要这样做?一个很好的问题。




属性getter和setter中重构现有类是没有问题的。实际上,您可以从具有

非隐藏属性的类继承,并创建一个具有相同名称的隐藏属性,

而不会破坏界面。


例如,让我们说你有


类MyClass

{

public int MyValue1 = 0;

}


//然后声明子类


类MyChildClass:MyClass

{

private int _MyValue1 = 0;

public new int MyValue1

{

get {return _MyValue1 * 2; }

set {_MyValue1 = value * 2; }

}

}


这很好用。新的属性getter和setter隐藏了原始的

属性。


那么为什么要从一开始就创建属性getter和setter?

我没有任何世俗的想法,除了一些纯粹主义者似乎认为它更符合面向对象设计的目标。也就是说,我承认我自己这样做,出于为什么不的意义。和其他人说这是一件很好的事情。


我知道这没有用......我是只是同意你:-)我正在寻找

转发其他回复。


---尼克


Gary Morris <克******* @ carolina.rr.com>在消息中写道

news:ew ************** @ tk2msftngp13.phx.gbl ...
Why do folks do it? A good question.

There is no problem with refactoring an existing class to add in the
property getter and setter. In fact, you can inherit from a class that has
non-hidden properties and create a hidden property with the same name,
without breaking the interface.

For example, let''s say you have

class MyClass
{
public int MyValue1 = 0;
}

// and then declare a child class

class MyChildClass : MyClass
{
private int _MyValue1 = 0;
public new int MyValue1
{
get { return _MyValue1 * 2; }
set { _MyValue1 = value * 2; }
}
}

This works just fine. The new property getter and setter hides the original
property.

So why create the property getter and setter from the outset?

I have no earthly idea, except some purists seem to feel that it is more in
keeping with the goals of object oriented design. That said, I admit to
doing this myself, out of a sense of "why not" and "others say this is a
good thing to do."

I know this doesn''t help... I''m just agreeing with you :-) I''m looking
forward to other responses.

--- Nick

"Gary Morris" <gm*******@carolina.rr.com> wrote in message
news:ew**************@tk2msftngp13.phx.gbl...
大家好,

好的,首先我知道自VB6以来的属性,我已经广泛使用过。看来属性得到和设置在C#和VB.NET中的概念基本相同,所以
没有问题。在使用VB6进行编程时,我从未编写任何使用过属性的程序,到目前为止,我还没有在C#中使用它们。我真正想知道的是,我什么时候应该使用它们?我的意思是,我们有变量(现在称为
字段,我认为),可以公开或私有,但所有的时间我看到代码:

- 首先声明一个字段为私有
- 然后使用公共财产来读取或写入私有字段

现在,我可以理解它们如何真正方便当你/你需要一个只读字段,但对于我的生活,我无法看到创建另一个成员的另一个成员除了为私有字段分配/读取值之外什么都没有。
可能刚开始公开。我还想声明,如果你想在阅读或写完之后做些什么,那么我可以看到它对他们很有用,那就是
只使用我能看到的属性。毕竟,公共领域是从代码之外的代码访问的,它包含类就像
属性一样,所以为什么还要烦恼:

private int MyInt;

public int MyIntP

{
返回MyInt;
}
设置
{
MyInt = value;
}
}
现在,如果设置部分是这样的:
.....
设置
{
MyInt = value * 2;
}
.....
我明白没问题。

它似乎对我而言,它与正常的公共领域完全相同。我希望听到一个解释,为什么这个
经常被使用,当财产对私人领域什么都不做时
除了读/写它的价值。如果我错过了一些非常重要的东西,我想知道它,但如果没有,我也想知道为什么这么多人使用它们(不断)如果没有<合法需要。

谢谢。
Hello all,

OK, first of all I have known about properties since VB6, which
I have and have used extensively. It seems that property get
and set are basically the same concept in C# and VB.NET, so
no problem there. In programming with VB6, I never wrote any
program that used properties, and to date I still haven''t ever
used them in C#. What I really want to know is, when should I
use them at all? I mean, we have variables (now referred to as
fields, I think), which can be made public or private, but all the
time I see code which:

- first declares a field as private
- then uses a public property to read or write to the private field

Now, I can understand how they would be really handy when
you need a read-only field, but for the life of me I cannot see the
point in creating yet another member for a class that does
nothing except assign/read a value to/from a private field that
could have just been made public to begin with. I also want to
state that I can see a good use for them if you wanted to DO
something to the field after it''s read or written, and that''s the
only use I can see for properties. After all, a public field is
accessed from code outside it''s containing class just like a
property is, so why bother with something like:

private int MyInt;

public int MyIntP
{
get
{
return MyInt;
}
set
{
MyInt = value;
}
}

Now, if the set part went like this:
.....
set
{
MyInt = value * 2;
}
.....
I could understand that with no problem.

It just seems to me that it does exactly the same thing as a normal
public field does. I would like to hear an explanation as to why this
is used so often when the property does nothing to the private field
except to read/write its value. If I''m missing something really
important here, I want to know about it, but if not, I also want to
know why so many people use them (constantly) if there is not a
legitimate need.

Thanks.



这篇关于关于属性及其用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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