C # Abstract class,

Source: Internet
Author: User

C # Abstract class,

Reprinted from: http://blog.csdn.net/wokeyid/article/d

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, Inheritance C # 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 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.