.Net edge learns an edge to tell (two)
Tuesday, March 03, 2009 by rain
With its his object-oriented programming language differs,In.Net kind have four main members,Data Members(fields) , function Members(methods) , properties, events.Before other programming language has only two.Need to notice here,Data Members(fields) does not state forever into Public,Kind of can alter you data that need not know you because of the person that such meetings make use.
Public Class Test{
Private Int I;
}
I is a Data Members(fields) here,Need alludes one a little bit,The data visit that Properties provided convenient, security is enclosed.Will talk about Function Members(methods) below,He has kind of two kinds of existence,Instance and Static.The acceptance with implicit Instance the finger that points to the object that he is in,In C# you can use object name or This gets.Be like:SomeObject.Method() ,Or This.Method() .Of the static state (Static)Method cannot receive This guide.Accordingly they cannot visit any kind of example in to convert data directly.His calling way is SomeClassName.StaticMethod() .He need not exemplify.Function Members(methods) acquiesce is Private,Can be in only namely statement their kind in visit,We need to state so that can be in,they are Public any kind in visit.Function Members(methods) is OK by burden,That is to say you can establish the method that many have same name,E.g. Test(int I) , test(double I) , test() , test(string I, bool B) . . . . . ..Net is compiled implement the parameter that can deliver according to you decides you are in call which method.The got-up magic figures drawn by Taoist priests to invoke or expel spirits and bring good or ill fortune that still has a few other of course is like Extern,If you want to state in.Net Framework,his utility is yours method and in the method that you implement besides.Net Framework,State in C# for example your method and in the method that you implement in Dll of Windows this locality,You are about to state so in C# ,Public Extern Yourmethod(){}
Next we should talk about Constructors and Finalize,Every kind of method that has a Constructor at least,If you were not offerred,The Constructor that C# will generate automatically to do not have any parameter for you.Constructor is with you kind the name is same and those who do not have any return of value method.Every Constructor can call you base kind Constructor (if do not have dominance base kind,Call a boy or girl friend) ,This is called can call before your Constructor main body is carried out,Such,You know you base kind had correct initialization.Constructor is called when every object is founded only,The example that its action is initialization object,Call in order to facilitate.Destructors already needed no longer,Because C# can offer automatically to clear the method of the object (call Finalize) .Rubbish clears automatically,The distinction that this also is C# one of character at other programming language.Next,We should talk about attribute.Why should use property above all?Will see a case,If you have kind of Person,A Data Members(fields) calls Age is Int32 model into Public by statement,(in front we had been told cannot state into Public,Illustrate is here why cannot) :
Person Jim = New Person(); // Create Object; Pointed To By Jim
Jim.Age = 23;
Int32 JimsAge = Jim.Age;
Jim.Age = -5; // Invalid, but Unchecked If You Use A Field
This paragraph of code can create two problems
Your user knew 1. the detail of inside data,They may become a few operations that you do not hope to happen
Your user may change 2. data is worth illegimately for,If set Age,be zero or negative number
Everybody can know the dominant position of attribute now,Photograph correspondence grants above carry
1. user won't know your inside data structure
2. attribute method can protect data
What the exemple Cheng below showed attribute is advantageous.
Public Class Person
{
Int32 Age; // notices acquiesce is Private
Public Person(Int32 Age) {// Constructor
This.age = Age; // This eliminates different meanings!
}
Public Int32 Age {// Property
Get {
Return Age;
}
Set {// Validating Value
If (value%26gt;0 %26%26 Value%26lt;150) {
Age = Value;
}
Else {// Throw Exception If Invalid Value
Throw New ArgumentException("Age Must Be Between 1
And 150 ");
}
}
}
}
This paragraph of program has the place of the attention of a few values
1. is in Constructor,We defined a parameter to also call Age,This meeting and different meanings of Private Age generation.We eliminate different meanings with This.
2.get method is used at reading to take attribute Set method to be used at accessing attribute.Key word Value represents the value that attribute is set
3. is worth in what we checked Value in Set method,Cast when data is illegimate went out unusual
Hit a bit tired...:)
Next time we talk about Event