字段或属性 [英] fields or properties

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

问题描述

您好,

我不知道何时使用字段以及何时使用属性。它看起来好像使用属性总是更好。但是我想在某些情况下字段必须是
更好,否则它们就不存在了!


谢谢

Julien

Hello,
I don''t know when to use fields and when to used properties. It looks to
me that using properties is always better. But I guess fields must be
better in some cases, otherwise they wouldn''t exist!

Thank you
Julien

推荐答案

好吧,让我们来看看。我想也许你对它们之间的区别感到困惑,因为它们是非常不同的东西。回答这个问题最简单的方法

就是我想的一个例子。


当你编写一个类时,你需要多次而不需要变量来保存数据

以使类正常运行。在类中定义的这些变量称为b $ b,称为字段。它们实际上只不过是在类中定义的变量来保存数据以使类能够工作

它的魔力。


但是,将这些变量(字段)暴露给该类用户的
存在一个主要问题。只是因为你正在写这个课并且

理解它的假设是如何工作的,并不意味着明年的那个人

创建你班级的实例会理解所有

的内部运作。


所以,当你布置你的班级和其中一个名为CustPhone的字段时需要

总是被格式化为:111-111-1111,你不能确定你的班级使用

的人,甚至你以后会记得。


来物业。属性实际上根本不是变量,而是

而是用于处理内部

变量或字段的写入和读取的函数。通过使用属性的Set和Get功能,您现在可以控制写入和读取内部

字段的内容,从而防止类的用户被能够在课堂上输入腐败的

数据,可能会导致以后失败。


希望这有助于您了解其中的差异。字段和属性

总是一起使用,并且应该在大多数类设计中使用,除非

您没有真正的理由提供访问字段的属性。但

字段总是在那里,无论什么属性都没用,如果它有

无需写入。


祝你好运,我希望我理解你的问题。


glenn

" julien" <菊**** @ sobrier.net>在消息中写道

news:42 ********************** @ news.free.fr ...
Well, lets see. I think maybe you are confused about the difference between
them as they are very different things. The easiest way to answer this
would be with an example I guess.

When you write a class you more times than not need variables to hold data
in order for the class to operate correctly. These variables that are
defined in the class are known as fields. They are really nothing more than
variables defined in the class to hold data in order for the class to work
its magic.

However, there is a major problem with exposing these variables(fields) to
the user of the class. Just because you are writing the class and
understand how its suppose to work, doesn''t mean the guy next year that
creates an instance of your class will understand all the inner workings of
it.

So when you lay out your class and one of the fields named "CustPhone" needs
to always be formatted like: 111-111-1111, you can''t be sure the guy using
your class or even you will remember that later on.

In comes properties. Properties are really not variables at all, but are
rather functions that handle writing and reading from your internal
variables or fields. By using the Set and Get features of properties you
are now able to control what gets written to and read from your internal
fields, keeping the users of your classes from being able to enter corrupt
data into the class that might cause failure later down the road.

Hopefully that helps you understand the difference. Fields and Properties
are always used together and should really be in most class designs, unless
you have no real reason to provide a property to access a field. But the
fields are always there no matter what as a property is useless if it has
nothing to write to.

Good luck and I hope I understood your question.

glenn
"julien" <ju****@sobrier.net> wrote in message
news:42**********************@news.free.fr...
您好,
我不知道何时使用字段以及何时使用属性。我认为使用属性总是更好。但是我想在某些情况下字段必须更好,否则它们就不存在了!

谢谢
Julien
Hello,
I don''t know when to use fields and when to used properties. It looks to
me that using properties is always better. But I guess fields must be
better in some cases, otherwise they wouldn''t exist!

Thank you
Julien



那么,字段应该是私有的还是属性公共的?
So, fields should be private only and properties public?


julien< ju **** @ sobrier.net>写道:
julien <ju****@sobrier.net> wrote:
那么,字段应该是私有的还是属性公共的?
So, fields should be private only and properties public?




是的 - 虽然属性不必公开,无论是。对于内部,受保护甚至是私人财产来说,它通常是有用的。


-

Jon Skeet - < sk***@pobox.com>
http://www.pobox。 com / ~siget

如果回复小组,请不要给我发邮件



Yes - although properties don''t have to be public, either. It can often
be useful to have internal, protected or even private properties.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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

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