C # inheritance

Source: Internet
Author: User

This article from: http://hi.baidu.com/kaixinpengpeng/blog/item/9f6911d834b8b93633fa1c88.html
Chapter C # inheritance

1. Inheritance

What is inheritance?: Inheritance allows reuse of existing classes to create new classes. The classification principle is that a subclass derived from a class has all non-private attributes of this class.
1, InheritanceC #Class:C # does not support multiple inheritance. C # class always inherits from one base class (if a base class is not specified in the declaration, it inherits from system. object ).
The syntax of the derived class to inherit the base class is as follows:
Using system;
Public class person
{
// This is the base class
}
Public class student: person
{
// This is a derived class
}
Note: When a derived class inherits the base class, use the ":" symbol to connect. The derived class inherits all non-private member variables and methods of the base class.
2, Call the constructor of the base class:
If no explicit constructor is defined for the class, the compiler provides the default constructor to initialize the member fields of these classes. However, if the constructor is explicitly added, the entire constructor can be effectively controlled.
Example:
Using system;
Namespace Jicheng
{
// Base class
Public class person
{
Public String _ name;
Public uint _ age;
// Constructor of the base class
Public Person (string name, uint age)
{
This. _ name = Name;
This. _ age = age;
// Print the output
Console. writeline (_ name );
Console. writeline (_ age );
}
}
// Derived class
Public class student: person
{
Private uint _ id;
// The constructor of the derived class and call the base class constructor to use ": Base"
Public student (string name, uint age uint ID): Base (name, age)
{
This. _ id = ID;

// Print the output
Console. writeline (_ id );
}
}
Public class exercise
{
[Stathread]
Static void main (string [] ARGs)
{
// Construct student
Student objstudent = new student ("xyz", 45,001 );
}
}
}
Note: By using the base () syntax, a derived class can explicitly call the constructors of the base class. You can use it to initialize fields if necessary.

II,C #Method rewriting in
Keywords:Override
The override keyword modifier is a new implementation of methods with the same name in the base class. methods with the same name in the base class must be declared as virtual or abstract. Adding the virtual keyword to the method in the base class indicates that the implementation of the virtual keyword can be rewritten in the derived class.
1. The default C # method is not virtual, so it cannot be rewritten.
2. The accessible level of the base class method does not change because of the method to be rewritten. Their access modifiers are the same.
3. The new, static, and virtual keywords cannot be used with the override access modifier.
Keywords:Virtual
The virtual keyword is provided in C #, which defines a method as too many and is used to declare the methods that can be modified in a class. This method is called a virtual method, word classes can use the override keyword to freely implement their respective Virtual Methods
Syntax:
[Access modifier] virtual [Return Value Type] Method Name ([parameter list])
{
// Virtual method implementation
}
1. virtual access modifiers cannot be used together with access modifiers such as static and override.
2. Call a virtual method. The system will automatically check the method at runtime to determine which implementation method is used for the call.
Keywords:New
The new access modifier is used to explicitly hide the members that inherit from the base class. If the names of the derived class members are the same as those of the base class members, new recognizes the members of the derived class as a new member.
1. An error occurs when both new and override are used in a method.
2. the real purpose of the new access modifier is to hide the base class method.
3. If the life of a method is new, it does not actually hide the base class method, and the compiler also generates a warning. In this case, delete new.

Iii. abstract classes and abstract methods
What is an abstract class:Classes that cannot be instantiated are called abstract classes. abstract classes are the base classes of derived classes.
Keywords: Abstract
Syntax:
Abstract class name
{
............
}
1. An abstract class can contain both abstract and non-abstract methods.
2. the abstract method is only implemented in the derived class. This indicates that the abstract method only stores the function prototype and does not involve the subject code,
3. Classes derived from abstract classes must implement abstract methods of their base classes to instantiate objects.
4. The override key sub-can implement the abstract method in the derived class. The method declared by override is called the rewrite base class method, and its signature must be the same as the signature of the override method.
Example:
Using system;
Namespace example_5
{
// Abstract class
Abstract Class ABC
{
// Abstract Method
Public abstract void afunc ();
}
// Derived class
Class derv: ABC
{
// Implement abstract methods in abstract classes
Public override void afunc ()
{
Console. writeline ("Implementing abstract methods ");
}
}
Public class test
{
Static void main (string [] ARGs)
{
Derv OBJ = new derv ();
OBJ. afunc ();
}
}
}
5. If the base class implements the abstract class, the derived class does not need to be implemented again.
6. abstract classes are not only an implementation technique, but also an abstract concept, thus establishing an agreement for all derived classes.

Iv. Interfaces
Keyword: Interface
Syntax:
Modifier interface Interface Name
{
// Interface subject
}
1. An interface is equivalent to an abstract class, but it cannot call any implementation method.
2. Each method of the interface must be implemented in the derived class.
3. The interface can sometimes be seen as a class mold, which specifies what content a class should provide.
4. The interface subject is limited to methods, indexers, and attribute declarations.
5. The interface cannot contain fields, constructors, and constants.
6. Interface members are implicitly disclosed. If you explicitly specify the access level for them, a compiler error will occur.
7. You cannot implement any method, attribute, or indexer in the interface.
8. When specifying a method, you only need to provide the return type, name, and parameter list, and end with a semicolon.
9. The syntax for implementing interfaces is the same as that for implementing inheritance. The colon ":" is used.
Example
Interface icustomer
{
..................
}
Public class myclass: icustomer
{
..................
}
10. Methods in interfaces cannot be rewritten and can only be implemented.
11,
Encoding standard:
The interface name must always contain uppercase letters (I ).

V. Implementation of multiple interfaces
The implementation of multiple interfaces in C # makes up that C # can only be a single inheritance and cannot be a multi-inheritance weakness.
Syntax example:
Public intetface INAME
{
Void name ();
}
Public interface iage
{
Void age ();
}
Public class consoleshow: iage, INAME
{
Public void name ()
{
// Specific implementation
}
Public void age ()
{
// Specific implementation
}
}

Vi. Implementation of explicit Interfaces
If the two interfaces have identical signatures, you can use the "interface name. Method Name" method to explicitly implement the interface.
Example:
Public interface INAME
{
Void test ();
}
Public interface iage
{
Void test ();
}
Public test: INAME, iage
{
Void INAME test ()
{
// Implement the INAME excuse
}
Void iage test ()
{
// Implementation of iage excuses
}

}

VII. interface inheritance
You can also create an interface by merging other interfaces. The syntax used to merge interfaces is similar to the inherited syntax, except that the former can merge multiple interfaces into one. To implement an interface for a class, you must write code for the base interface and all members of the derived excuse.

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.