C # getting started with language (2)

Source: Internet
Author: User

The following example shows how to create and use a user-defined class and how to create a dynamic link library. Use a text editor to create two files. The first one is Apple. cs. The content is as follows:
Public class Apple {

Private string variety = "";

Public Apple (string appleVariety ){
This. variety = appleVariety;
}

Public void outputVariety (){
System. Console. WriteLine (variety );
}

}


The second file is Example2.cs. The content is as follows:


Class Example2 {

Static void Main (){
Apple mac = new Apple ("Macintosh ");
Apple gra = new Apple ("Granny Smith ");
Apple cor = new Apple ("Cortland ");
Mac. outputVariety ();
Gra. outputVariety ();
Cor. outputVariety ();
}
}


First, we define a new user-defined class named Apple. Although Apple classes do not have to be placed in independent files, it is a good Object-Oriented Programming habit to put each class in its own independent files, which helps simplify organization and management. We add the public modifier (public class Apple) to the Apple class declaration so that other classes can create Apple class instances.

The next line of code defines the instance variable variety. After the modifier private is used, the variety variable can be directly accessed only within the Apple class. This is a common Object-Oriented Programming habit, called encapsulation. After encapsulation, the work details of the object are hidden and invisible to the users of the object. The keyboard you are using is a good example encapsulated in the real world. We do not fully understand how key-hits are sent to the Controller (most of us do not know), but we only need to understand how its interfaces work. For example, we know that when you open the text editor and press the & key on the keyboard, the & character will appear on the screen. If everyone must understand the work details of the keyboard rather than the interface, many of us will not use it.

The following three lines of code are:


Public Apple (string appleVariety ){
This. variety = variety;
}


The three lines of code define the Apple class constructor. Class constructor is similar to a blueprint describing how to create class instances. We can easily distinguish between constructors and other methods in the class, because constructors always have the same name as classes. In this example, an Apple-like constructor has a string parameter, which is then saved to the instance variable variety.

The last method of the Apple class is outputVariety (). This Method provides an interface for accessing instance variables, so it is called an Accessor Method ).

Let's take a look at Example2. The difference between this example and the previous example is that you need to create and use a user-defined Apple-like instance. We use the new operator to create three Apple class instances. When creating an instance of a class, we do not need to explicitly call the class constructor. The new operator will automatically call the class constructor for us. After three Apple class objects are created, the outputVariety method of these three objects is called in sequence, and the outputVariety method outputs the variety values of these three objects.

Next we will compile and run this example. First, we need to compile the Apple category into a dynamic link library. The command is as follows:


Csc/target: library Apple. cs


/Target: the library indicates not to create an execution file, but to create a. dll file (that is, a dynamic link library ). Therefore, the above command will generate an Apple. dll file.

Next we compile Example2.cs. The compilation command is as follows:


Csc/reference: Apple. dll Example2.cs


Now we have to go to the execution file example2.exe. Execute this file and you can see the following output on the console:


Macintosh
Granny Smith
Cortland


Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.