Let's go on to talk about class
Unlike other object-oriented programming languages,. NET class has four basic members, data member (fields), function members (methods), properties, events. Other programming languages have only the first two. It should be noted here that the data members (fields) should never be declared public, as this will allow the user to change your data without knowing your class.
public class test{
private int i;
}
Here I is a data members (fields), which needs to mention that properties provide a convenient and secure access package. Let's talk about the function members (methods), he has two ways of being, instance and static. Instance implicitly accepts a pointer to the object he is in, in C # you can get it with the object name or this. such as: Someobject.method (), or this. Method (). static (Static) method cannot receive the this pointer. Therefore, they cannot directly access any of the instantiated data in the class. His way of calling is Someclassname.staticmethod (). He did not need to be shown. Function members (methods) are private by default, that is, they can only be accessed in the class that declares them, and we need to declare them public so that they can be accessed in any class. The function members (methods) can be overloaded, which means you can create multiple methods with the same name, such as test (int i), test (double i), test (), test (string I, bool b) .... The net compiler determines which method you are calling based on the parameters you pass. Of course, there are other modifiers such as extern, which are used if you want to declare your methods in the. NET Framework and implement your approach outside of the. NET Framework, such as declaring your method in C # and implementing your method in a Windows local DLL. You're going to declare this in C #, public extern yourmethod () {}
Next we'll talk about constructors and finalize, each class has at least one constructor method, and if you don't, C # will automatically generate a constructor for you without any arguments. Constructor is a method that has the same name as your class and does not have any return value. Each constructor will invoke a constructor of your base class (if there is no dominant base class, call the object), and this call will be invoked before your constructor body executes, so that you know that your base class has not been properly initialized. constructor is invoked only once per object is created, and its role is to initialize an instance of an object so that it can be invoked. Destructors is no longer needed because C # automatically provides a way to clean objects (named Finalize). Automatic garbage cleanup, which is one of the features of C # that distinguishes it from other programming languages. Next, we should talk about attributes. Why do you use attributes first? To take a look at an example, if you have a class person, there is a data members (fields) that the age is Int32 to be declared public, (as we said before we cannot declare public, here is an example of why not):
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 code can cause two problems.
1. Your users know the details of the internal data, they may do something you do not want to happen
2. Your users may change the data to an illegal value, such as setting the age to zero or negative
Now you can know the advantages of attributes, relative to the above mentioned
1. The user will not know your internal data structure
2. The property method protects the data
The following routine shows the superiority of the property.
public class Person
{
Int32 age; Note that default is private
Public person (Int32 age) {//constructor
This.age = age; This eliminates ambiguity!
}
Public Int32 Age {//property
get {
return age;
}
set {//validating value
if (value > 0 && value < 150) {
Age = value;
}
else {//Throw exception if invalid value
throw new ArgumentException ("Age must is between 1
and 150 ");
}
}
}
}
This program has several values for the place of concern
1. In constructor, we define a parameter called age, which can be ambiguous with private age. We use this to eliminate ambiguity.
The 2.get method is used to read property set methods for accessing properties. The keyword value represents the value that the property is set
3. In the Set method, we examine the value of values and throw an exception when the data is not valid
I'm tired of playing ... :)
Next time we talk about the event,
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service