是面向对象具有的 set and get 它的用途: 一般是对类里面的变量进行操作. 而不是直接对类的变量进行操作. 有一个很大的作用就是: 便于维护.因为: 如果一个类的一个变量int a ,在其它包或命名空间类中使用了1000次,但是过了许久,你想把a改为b, 如果直接对变量a操作的话,就得需求修改整个程序的1000处. 如果用属性了,就不会了,只需改这个方法即可 public int A { set { a = value; } get { return a; } } 放为: public int B { set { b = value; } get { return b; } } 除去这个属性之外的地方根本不需要改变OK?
我看的是Charles Petzold 的 programming in the key of C#,后面附录里get,set不是关键字 如果说我这本书是垃圾绝对不可能 除非你是和他一样的高手.
我仔细看了一下书,P223上说,属性并不是CLS(Common Language Specification )的组成部分,它们在C#所使用的中间语言(intermediate language)也没有得到实现,如果在自己创建的C#类中声明了一个名字,比如所是Year的属性,C#编译器将自动声称两个分别为set_Year, get_Year的方法,并且与Year属性相关联的代码放在这个方法里,如果使用的语言不是C#,比如C++,就需要通过这两个方法去访问Year属性,同时,自己声明的方法不恩哪个以set_Year, get_Year命名,也就是说,声明了一个Year 属性,就不允许再去声明一个set_Year或get_Year方法.详细参见:C# Language Specification 10.2.7.1 小节看来大家基本功还要加强啊,谢谢大家讨论~
在C#中不是关键字~~~ 可是在VB.NET是关键字 别混起来哦 下表列出了 Visual Basic .NET 语言的关键字。所有语言关键字均保留。设备项目不支持带有星号 (*) 的项。AddHandler AddressOf Alias And AndAlso Ansi* As Assembly Auto* Boolean ByRef Byte ByVal Call Case Catch CBool CByte CChar CDate CDec CDbl Char CInt Class CLng CObj Const CShort CSng CStr CType Date Decimal Declare Default Delegate Dim DirectCast Do Double Each Else ElseIf End* Enum Erase Error Event Exit #ExternalSource False Finally For Friend Function Get GetType GoTo Handles If Implements Imports In Inherits Integer Interface Is Let* Lib Like Long Loop Me Mod Module MustInherit MustOverride MyBase MyClass Namespace New Next Not Nothing NotInheritable NotOverridable Object On Option Optional Or OrElse Overloads Overridable Overrides ParamArray Preserve Private Property Protected Public RaiseEvent ReadOnly ReDim #Region REM RemoveHandler Resume Return Select Set Shadows Shared Short Single Static Step Stop String Structure Sub SyncLock Then Throw To True Try TypeOf Unicode* Until Variant* When While With WithEvents WriteOnly Xor #Const #ExternalSource #If...Then...#Else 指令 #Region - & &= * *= / /= \ \= ^ ^= + += = -= 下面是C#的关键字 关键字是对编译器具有特殊意义的预定义保留标识符。它们不能在程序中用作标识符,除非它们有一个 @ 前缀。例如,@if 是一个合法的标识符,而 if 不是合法的标识符,因为它是关键字。abstract event new struct as explicit null switch base extern object this bool false operator throw break finally out true byte fixed override try case float params typeof catch for private uint char foreach protected ulong checked goto public unchecked class if readonly unsafe const implicit ref ushort continue in return using decimal int sbyte virtual default interface sealed volatile delegate internal short void do is sizeof while double lock stackalloc else long static enum namespace string 可以发现C#没有GET 和SET 不要搞混了 ^_^
这种问题应该自己查 MSDN 或者看看书。
set and get
它的用途: 一般是对类里面的变量进行操作. 而不是直接对类的变量进行操作.
有一个很大的作用就是: 便于维护.因为:
如果一个类的一个变量int a ,在其它包或命名空间类中使用了1000次,但是过了许久,你想把a改为b,
如果直接对变量a操作的话,就得需求修改整个程序的1000处. 如果用属性了,就不会了,只需改这个方法即可
public int A
{
set
{
a = value;
}
get
{
return a;
}
}
放为:
public int B
{
set
{
b = value;
}
get
{
return b;
}
}
除去这个属性之外的地方根本不需要改变OK?
关键字是一种高级计算机程序设语言中属于语言成分的特殊的标识符,通常,这些标识符是系统保留的,不允许用户在程序中使用这些标识符定义各种名称。
c#的关键字中并不包含get set。
楼上说是关键字,请问你在那里看到的?
如果说我这本书是垃圾绝对不可能
除非你是和他一样的高手.
可是在VB.NET是关键字 别混起来哦
下表列出了 Visual Basic .NET 语言的关键字。所有语言关键字均保留。设备项目不支持带有星号 (*) 的项。AddHandler AddressOf Alias And
AndAlso Ansi* As Assembly
Auto* Boolean ByRef Byte
ByVal Call Case Catch
CBool CByte CChar CDate
CDec CDbl Char CInt
Class CLng CObj Const
CShort CSng CStr CType
Date Decimal Declare Default
Delegate Dim DirectCast Do
Double Each Else ElseIf
End* Enum Erase Error
Event Exit #ExternalSource False
Finally For Friend Function
Get GetType GoTo Handles
If Implements Imports In
Inherits Integer Interface Is
Let* Lib Like Long
Loop Me Mod Module
MustInherit MustOverride MyBase MyClass
Namespace New Next Not
Nothing NotInheritable NotOverridable Object
On Option Optional Or
OrElse Overloads Overridable Overrides
ParamArray Preserve Private Property
Protected Public RaiseEvent ReadOnly
ReDim #Region REM RemoveHandler
Resume Return Select Set
Shadows Shared Short Single
Static Step Stop String
Structure Sub SyncLock Then
Throw To True Try
TypeOf Unicode* Until Variant*
When While With WithEvents
WriteOnly Xor #Const #ExternalSource
#If...Then...#Else 指令 #Region - &
&= * *= /
/= \ \= ^
^= + += =
-=
下面是C#的关键字
关键字是对编译器具有特殊意义的预定义保留标识符。它们不能在程序中用作标识符,除非它们有一个 @ 前缀。例如,@if 是一个合法的标识符,而 if 不是合法的标识符,因为它是关键字。abstract event new struct
as explicit null switch
base extern object this
bool false operator throw
break finally out true
byte fixed override try
case float params typeof
catch for private uint
char foreach protected ulong
checked goto public unchecked
class if readonly unsafe
const implicit ref ushort
continue in return using
decimal int sbyte virtual
default interface sealed volatile
delegate internal short void
do is sizeof while
double lock stackalloc
else long static
enum namespace string 可以发现C#没有GET 和SET 不要搞混了 ^_^