Interface and inherited polymorphism in C #

Source: Internet
Author: User
Tags definition constructor inheritance static class

In the previous section we learned about constructors that initialize an instance of an object. Also in the. NET framework, destructors are provided to clean objects. Typically, you do not need a special destructor, and the system automatically provides a default destructor to perform actions to clean up objects that you no longer need.

One, static and instance class members

Properties, methods, and fields are unique to the object instance, in addition to static members. For example, static methods, properties, and fields. These static members can be shared among different instances of the class. So they can be viewed as global objects of the class. Static properties and fields can access data that is independent of any object instance. When you use static members, you do not need to instantiate the object.

In the application console program used a lot of input and output, in fact, they are static methods. You do not need to instantiate the console and convert to call directly. Attempting to instantiate a static method or class can cause the operation to fail.

Static class: Normally we all want the class to contain only static members, and cannot be used to instantiate objects, for this one simple method is to use static classes instead of setting the constructor of the class to private. Static classes have only static members and do not require the definition of constructors. Because the static class is not allowed to be instantiated.

Two Interface

An interface is a combination of implicit public methods and properties to encapsulate a set of specific features, one that defines an interface and implements it in a class, so that the class supports all the properties and members specified in the interface.

Also note that interfaces cannot exist alone and cannot instantiate an interface like an instantiated object, the interface cannot contain any code that implements its members, but only the members themselves, and the implementation must be implemented in the class that implements the interface. A class can support multiple interfaces, while an interface can also be implemented in multiple classes.

Second, the succession

Inheritance is one of the most important attributes of OOP, and any class can inherit from another class, but each class can inherit only a few basic classes. The inherited class is called the parent class, and the inherited class is called a subclass. A very important problem is that the accessibility of a member becomes an important issue when inheriting a base class. When a class inherits from a base class, it gets all its members and properties as well as fields. Now look at the situation after the Student class inherits the person class:class student : person  indicates that the Student class inherits the person class

Class person //person class

{

public string  name;

Public string sex;

Public int age;

Public double weight;

Public person ()//constructor

{

name =  "Lu xiaofeng";

sex =  "man";

Age = 22;

Weight = 99;

}

Public person (String name, string sex,  int age, double weight)

{

This.name = name;

This.sex = sex;

This.age = age;

This.weight = weight;

}//constructor

Public void eat (Double food)

{//method, weight gain after eating

this.weight +=  food;

}

}

Class Student:person

{

public string School_name;

public double score;

public int grade;

Public Student ()

{

School_name = "CSU";

Score = 99;

Grade = 1;

}

public void Study (double hours)

{

This.weight-= hours;

}

}

Class Student all the properties and methods of its parent class after it inherits the person

Now let's compare the difference between the next person class and a student class.

Person mans = New Person ("Lixiao", "Woman", 11, 88);

Console.WriteLine ("Person method and Attributes");

Console.WriteLine ("Name={0},sex={1},age={2},weight={3}",

Man.name, Man.sex, Man.age, man.weight); Eat (12);

Console.WriteLine ("Man.") Eat (a); ", man.weight);

Student Luxiaofeng = new Student ();

Console.WriteLine ("Student Methods and Attributes");

Console.WriteLine ("Luxiaofeng.name={0},luxiaofeng.sex={1},luxiaofeng.age={2},luxiaofeng.weight={3}", Luxiaofeng.name,luxiaofeng.sex,luxiaofeng.age,luxiaofeng.weight); Calling properties of the parent class

Console.WriteLine ("Luxiaofeng.school_name={0},luxiaofeng.score={1},luxiaofeng.grade={2}", Luxiaofeng.school_ Name,luxiaofeng.score,luxiaofeng.grade);//Own properties

Console.WriteLine ("Luxiaofeng.weight={0}", luxiaofeng.weight);

Luxiaofeng.eat (14);//The method of calling the parent class

Console.WriteLine ("Luxiaofeng.weight={0}", luxiaofeng.weight);

Luxiaofeng.study (18);//Call your own method

Console.WriteLine ("Luxiaofeng.weight={0}", luxiaofeng.weight);

The results of the output are as follows:

In summary:

Inheritance in C # conforms to the following rules:
1, inheritance is transitive. If c derives from B, and b derives from a, then C inherits not only the members declared in B, but also the members of a. The Object class acts as the base class for all classes.
2. The derived class should be an extension of the base class. A derived class can add new members, but you cannot drop the definition of a member that has already been inherited.
3 A derived class can overwrite an inherited member if it defines a new member with the same name as an inherited member. But that's not because the derived class deletes these members, but they are no longer accessible.

4 You can define a virtual method, a virtual property, and a virtual index indicator whose derived classes can overload these members, thereby enabling the class to exhibit polymorphism.

5 a derived class can inherit from only one class, and multiple inheritance is present through the interface.

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.