C # Language Primer (2)

Source: Internet
Author: User
Tags constructor modifier
The following example demonstrates how to create and use a user-defined class and how to create a dynamic-link library. Create two files with a text editor. The first one is Apple.cs, which reads 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, which reads 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 the Apple class does not have to be placed in separate files, it is a good object-oriented programming practice to place each class in its own separate file, helping to simplify organization and management. We have added the public modifier (public class apple) to the Apple class declaration so that other classes can create an instance of the Apple class.

The next line of code defines the instance variable variety. With the modifier private, the variety variable can be accessed directly from within the Apple class. This is a common object-oriented programming idiom, called encapsulation. After encapsulation, the details of the object's work are hidden from the user of the object. The keyboard you are using now is a good example of encapsulation in the real world. We don't fully understand how keystrokes are sent to the controller (most of us don't know), but just understand how its interface works. For example, we know that when you open a text editor and press the "&" button on your keyboard, the "&" character appears on the screen. If everyone must understand the work details of the keyboard rather than just understand its interface, not many of us will use it.

The next three lines of code are:


Public Apple (String applevariety) {
this.variety = variety;
}




These three lines of code define the constructor of the Apple class. A constructor for a class resembles a blueprint that describes how to create an instance of a class. We can easily distinguish between constructors and other methods within a class, because constructors always have the same name as the class. In this case, the class Apple 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 the instance variable, so it is called the accessor method (accessor).

Here we look at the Example2 class. The difference between this example and the previous example is to create and use an instance of the user-defined class Apple. We created an instance of three Apple classes with the new operator. When creating an instance of a class, we do not need to explicitly invoke the constructor of the class, and the new operator will automatically invoke the constructor of the class for us. After creating an object for three Apple classes, we call the Outputvariety method of the three objects in turn, and the Outputvariety method outputs the variety values in the three objects.

Let's compile and run the example below. First we want to compile the Apple class into a dynamic link library, which commands the following:


Csc/target:library Apple.cs




/target:library means that you do not create an execution file, but instead create a. dll file (that is, a dynamic link library). Therefore, the above command will generate a Apple.dll file.

Next we compile Example2.cs, and the compile command looks like this:


Csc/reference:apple.dll Example2.cs




Now we get the execution file Example2.exe. Execute this file to see the following output on the console:


Macintosh
Granny Smith
Cortland






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.