什么是房产? [英] What is a property?

查看:68
本文介绍了什么是房产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


有人可以告诉我究竟什么是财产是在C#类?据我所知,
可以看出它是两种方法。 - 即一个实例的getter和setter

变量。


这些之间有什么区别:


public string InstanceStringVariable()

{

get {return _instanceStringVariable; }

set {_instanceStringVariable = value; }

}





公共字符串getInstanceStringVariable

{

return _instanceStringVariable;

}


public void setInstanceStringVariable(string stringVariable)

{

_instanceStringVariable = stringVariable;

}


谢谢,

彼得

Hi there,

can someone tell me what exactly a "property" is in a C# class? As far as I
can see it is "two methods" - ie a getter and a setter for an instance
variable.

What is the difference between these:

public string InstanceStringVariable()
{
get { return _instanceStringVariable; }
set { _instanceStringVariable = value; }
}

and

public string getInstanceStringVariable
{
return _instanceStringVariable;
}

public void setInstanceStringVariable(string stringVariable)
{
_instanceStringVariable = stringVariable;
}

Thanks,
Peter

推荐答案

你完全正确。存在属性,以便可以遵守类中的封装规则

。将数据公开为

公共字段是非常糟糕的做法,因为任何外部类都可以在没有该类的

知识的情况下更改该数据。如果数据成员假设有一个

范围的有效值,这可能是灾难性的,因为在更改值之前没有值

检查。 br />

一个属性确实是类所拥有的一小块代码,用于强制执行该类需要控制其数据的规则。


属性还可以合成其他数据中的值。例如你

可以用开尔文存储温度,但是提供返回数据的属性

华氏度或摄氏度在吸气剂中进行计算

实施。


-

鲍勃鲍威尔[MVP]

Visual C#,System.Drawing

Ramuseco Limited .NET咨询
http://www.ramuseco.com


在Windows窗体中查找优秀的Windows窗体文章提示和技巧
http://www.bobpowell.net/tipstricks.htm

用GDI + FAQ回答那些GDI +问题
http://www.bobpowell.net/faqmain.htm


所有新文章都提供C#和VB.NET中的代码。

订阅所提供的RSS源,绝不会错过任何新文章。


" Peter Kirk" < pk@alpha-solutions.dk>在消息中写道

新闻:uy ************** @ TK2MSFTNGP14.phx.gbl ...
You are exactly correct. Properties exist so that the rules of encapsulation
within a class can be obeyed. It is very bad practice to expose data as a
public field because any external class can alter that data without the
knowledge of the class. In the case of a data member that assumes say, a
range of valid values, this can be disastrous because there is no value
checking before the value is changed.

A property is indeed a little chunk of code owned by the class to enforce
whatever rules the class needs to over the control of it''s data.

Properties can also synthesize a value from some other data. For example you
may store a temperature in Kelvin but provide properties to return that data
in Fahrenheit or Centigrade doing the calculation in the getter
implementation.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Peter Kirk" <pk@alpha-solutions.dk> wrote in message
news:uy**************@TK2MSFTNGP14.phx.gbl...
你好,我可以看到它是两种方法。 - 即一个实例的getter和setter变量。

这些之间有什么区别:

公共字符串InstanceStringVariable()
{
get {return _instanceStringVariable; }
设置{_instanceStringVariable = value;公共字符串getInstanceStringVariable
{
返回_instanceStringVariable;
}


> public void setInstanceStringVariable(string stringVariable)
{
_instanceStringVariable = stringVariable;
}
谢谢,
Peter
Hi there,

can someone tell me what exactly a "property" is in a C# class? As far as
I can see it is "two methods" - ie a getter and a setter for an instance
variable.

What is the difference between these:

public string InstanceStringVariable()
{
get { return _instanceStringVariable; }
set { _instanceStringVariable = value; }
}

and

public string getInstanceStringVariable
{
return _instanceStringVariable;
}

public void setInstanceStringVariable(string stringVariable)
{
_instanceStringVariable = stringVariable;
}

Thanks,
Peter



" Bob Powell [MVP]" < bob@_spamkiller_bobpowell.net> skrev i en meddelelse

新闻:uA ************** @ TK2MSFTNGP14.phx.gbl ...
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> skrev i en meddelelse
news:uA**************@TK2MSFTNGP14.phx.gbl...
你完全正确。存在属性,以便可以遵守类中的封装规则。将数据公开为公共字段是非常糟糕的做法,因为任何外部类都可以在不知道类的情况下更改数据。如果数据成员
假设说一系列有效值,这可能是灾难性的,因为
在值更改之前没有值检查。

A某些属性确实是类所拥有的一大块代码,用于强制执行类所需的任何规则来控制它的数据。

属性也可以合成一些值其他数据。例如,您可以以开尔文存储温度,但提供返回华氏度或摄氏度数据的属性,以便在getter
实现中进行计算。
You are exactly correct. Properties exist so that the rules of
encapsulation within a class can be obeyed. It is very bad practice to
expose data as a public field because any external class can alter that
data without the knowledge of the class. In the case of a data member that
assumes say, a range of valid values, this can be disastrous because there
is no value checking before the value is changed.

A property is indeed a little chunk of code owned by the class to enforce
whatever rules the class needs to over the control of it''s data.

Properties can also synthesize a value from some other data. For example
you may store a temperature in Kelvin but provide properties to return
that data in Fahrenheit or Centigrade doing the calculation in the getter
implementation.




好​​的,谢谢。那么实际的属性点是什么 - 为什么不写一个getter和一个setter方法来获得
?它只是被认为是减少打字吗?为什么

然后不要全力以赴并允许类似:


公共属性字符串InstanceStringVariable;


这样会然后生成一个实例变量,一个getter和一个setter。

当然没有身体到getter和setter,但是如果需要可以延长



公共属性字符串InstanceStringVariable

{

得到{...; }

}

彼得



OK, thanks. What then is the actual point of properties - why not just write
a getter and a setter method? Is it just the perceived "less typing"? Why
not then go whole hog and allow something like:

public property string InstanceStringVariable;

This would then generate an instance variable, a getter, and a setter. Of
course there is no "body" to the getter and setter, but then it could just
be extended if required:

public property string InstanceStringVariable
{
get { ...; }
}
Peter


嗨彼得,


我们可以告诉属性是C#中的智能字段(类成员)。公共字段

在类中可以完全访问其他类的字段。因此,无法提供对公共字段的只读或只写访问权限。

。这是

可以通过属性。


属性可以给予只读,只写或正确读取字段。

这个还允许我们为字段添加额外的业务逻辑。


class Class1

{

private string firstname;

private string lastname;

private int id;


//此属性仅允许设置(只写)

public string FirstName

{

set

{

firstname = value;

}

}


//此属性仅允许设置(只写)

公共字符串LastName

{

set

{

lastname = value;

}

}


//这是自动生成的,所以只得到(只读)

//这个属性有额外的逻辑来创建id

public int ID

{

get

{

随机rd =新的随机() ;

id = rd.Next( );

返回ID;

}

}


//此属性是只读的额外的商业逻辑

公共字符串名称

{

get

{

return firstname + lastname;

}

}

}


您可以将属性视为两种方法什么时候得到和设置,但是

属性类似于外部世界的类成员。我们可以使用属性

以相同的方式在类中具有公共字段并具有额外的可访问性。


问候

Prakash Prabhu K



" Peter Kirk"写道:
Hi Peter,

we can tell properties are smart fields (Class members) in C#. Public fields
in the class would give complete access to field from other classes. So it is
not possible to give read-only or write only access to public fields. This is
possible through properties.

Properties can give read only, write only or read right access to fields.
This also allow us to add extra business logic to fields.

class Class1
{
private string firstname;
private string lastname;
private int id;

//This property allows only to set (Write only)
public string FirstName
{
set
{
firstname = value;
}
}

//This property allows only to set (Write only)
public string LastName
{
set
{
lastname = value;
}
}

//This is auto generated so has only get (readonly)
//This property has extra logic to create id
public int ID
{
get
{
Random rd = new Random();
id = rd.Next();
return id;
}
}

//This property is readonly with extra business logic
public string Name
{
get
{
return firstname + lastname;
}
}
}

You can see properties as two methods when it has both get and set, but
Properties visible like class members to outside world. we can use properties
in the same way has public fields in the class with extra accessibility.

Regards
Prakash Prabhu K


"Peter Kirk" wrote:
你好,

有人可以告诉我究竟是什么属性是在C#类?据我所知,它是两种方法。 - 即一个实例的getter和setter变量。

这些之间有什么区别:

公共字符串InstanceStringVariable()
{
get {return _instanceStringVariable; }
设置{_instanceStringVariable = value;公共字符串getInstanceStringVariable
{
返回_instanceStringVariable;
}


> public void setInstanceStringVariable(string stringVariable)
{
_instanceStringVariable = stringVariable;
}
谢谢,
Peter
Hi there,

can someone tell me what exactly a "property" is in a C# class? As far as I
can see it is "two methods" - ie a getter and a setter for an instance
variable.

What is the difference between these:

public string InstanceStringVariable()
{
get { return _instanceStringVariable; }
set { _instanceStringVariable = value; }
}

and

public string getInstanceStringVariable
{
return _instanceStringVariable;
}

public void setInstanceStringVariable(string stringVariable)
{
_instanceStringVariable = stringVariable;
}

Thanks,
Peter



这篇关于什么是房产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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