Other OOP techniques in C #

Source: Internet
Author: User

Object-oriented programming has many characteristics, such as inheritance, encapsulation, polymorphism and so on. The knowledge about inheritance has already been introduced, and it is now a concrete introduction to the encapsulation and polymorphism.

A Packaging

A class is a collection of properties and methods, so that when a particular method or attribute is defined, the developer knows how to use the methods and attributes without knowing the specific implementation of the method or attribute being invoked. As if the brakes were in the process of driving, the driver knew that it was OK to brake at some point, but do not pay attention to how the braking process was achieved, this was the work of the car designer.

Person mans = New Person ("Lixiao", "Woman", 11, 88); Man. Eat (12); In this small example, we created a man class object man, which invokes the method of the Person class Eat ()--man. Eat (12), but the specific implementation of this method is not visible here. This is known as encapsulation.

Two Polymorphic

Polymorphism is an important feature of object-oriented programming, in the C # environment, the polymorphism is overload and overwrite. The concept of overloading has been said before: In a class, more than two methods have the same name, different parameter types, but the return value may not be the same as the following example.

public void Hi ()

{

Console.WriteLine ("Hi,c Sharp");

}

public void Hi (string name)

{

Console.WriteLine ("Hi,{0}", name);

}

Overwrite is a subclass of a method that repeats a parent class in order to implement a feature. Overriding methods are stricter than overloaded methods: only virtual methods and abstract methods can be overwritten, while overriding several conditions must be met: The same method name, parameter list, and return value type are indispensable.

For example, the following example: A method in person of the time base class on the left, and the process of Getweight () method after the student class inherits the person class after the right.

 

A result of polymorphism even if a class that derives from the base class has a certain overlap in methods and properties, you can use the same syntax to handle objects instantiated from the same class, such as class student and worker, inherited from the person class, Then objects of two subclasses can call the Eat () method of the person class.

Class Student:person

Student Luxiaofeng = new Student ();

Luxiaofeng.eat (15);

Class Work:person

Work sub_work = new Work ();

Sub_work. Eat (15);

Three The relationship between objects

There are two more important relationships between objects in addition to inheriting this simple relationship: inclusion and collection.

1. Include relationship: A class contains another class, which, in contrast to inheritance, controls access to members of the contained class, and even other processing before using the members of the included class.

2. Set Relationship: A class is used as a container for multiple instances of a class, with functions such as indexing, sorting, and redesigning sizes.

The collection relationships of objects are similar to those used by arrays, such as defining a set: student[] Mystu = new STUDENT[20]; But the main difference between collections and arrays: collections can usually perform additional functions of adding and deleting items in a collection. And the collection has an item property that can return the object based on its index.

Four Event

objects can activate events, often in C # 's window programming, such as by pressing a button or clicking on some icons, C # to programmatically handle the events that are raised. In the window design process, the event is essential. Let's look at a simple window application.

int count = 0;

int coun1= 0;

private void Button1_Click (object sender, EventArgs e)

{//Click Button1 event Handling

count++;

TextBox1.Text = count. ToString ();

}

private void Button2_Click (object sender, EventArgs e)

{//Click Button2 Event Handling

count--;

TextBox1.Text = count. ToString ();

}

Add Textbox,label,button controls to each of the two Form1, Coun and coun1 respectively to count the number of clicks on the two button controls and then display them on the textbox.

Five Reference type and numeric type

In C #, the data exists in one variable in two ways depending on the type of the variable, which is a reference type and a value type, respectively. The difference between the two is mainly:

1. Value types store themselves and their contents in one place in memory;

2. The reference type stores a reference in one place in memory, and another place stores the content.

So far, the string variable (reference type) and other simple variables (int) have been used in exactly the same way, except that the value of the reference type can be null, and the value type always contains a value. String Name=null; int id=0; If you define name and do not assign a value or make it equal to NULL, the system prompts: name is not initialized.

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.