Inheritance in C # (inheritance, abstract classes, and abstract methods, interfaces)

Source: Internet
Author: User
Tags modifiers

Recently in the study of refactoring, so for the class, interface these studies are more, and then look at various materials, as long as I feel able to learn something, on the record, feeling is a meaningful thing.

First, inheritance

What is inheritance :Inheritance is the process of allowing existing classes to be reused to create new classes. The principle of classification is that a subclass derived from a class has all the non-private properties of the class.
1. Inherit classes from C # :C # does not support multiple inheritance, and C # classes always inherit from a base class (inherited from System.Object if a base class is not specified in the declaration).
Derived classes inherit the syntax of the base class as follows:
using System;
Public class person
{
//This is a base class
}
Public class Student:person
{
//This is a derived class
}
Note: When a derived class inherits a base class with a ":" Symbolic connection, the derived class inherits all non-private member variables and methods of the base class.
2 . Call the constructor of the base class:
If you do not define an explicit constructor for a class, the compiler provides a default constructor to initialize the member fields of those classes. However, if you explicitly add a constructor, you can effectively control the entire construction process.
Example:
using System;
namespace Jicheng
{
//base class
Public class person
{
public string _name;
public uint _age;
//constructor for base class
Public person (String name,uint age)
{
this._name = name;
this._age = age;
//Print out
Console.WriteLine (_name);
Console.WriteLine (_age);
}
}
//derived class
Public class Student:person
{
private uint _id;
//derived class constructor and call base class constructor with ": Base"
Public Student (string name,uint age UINT ID): Base (name,age)
{
this._id = ID;//Print out
Console.WriteLine (_id);
}
}
Public class Exercise
{
[STAThread]
static void Main (string[] args)
{
//Construction Student
Student objstudent=new Student ("XYZ", 45,001);
}
}
}
Note: Derived classes can explicitly call the constructor of a base class by using the: base () syntax. You can use it to initialize a field, if necessary. Second, method overrides in C #
keyword: override
The override keyword modifier method, with the override keyword modifier, is a new implementation of a method of the same name in the base class, which must be declared as a virtual or abstract type. Adding the virtual keyword to a method in a base class means that its implementation can be overridden in a derived class.
1. The default C # method is not virtual and therefore cannot be overridden.
2. The accessibility level of the base class method is not changed by overriding its methods, and their access modifiers are the same.
3, new, static, and virtual keywords cannot be used with the override access modifier
Keywords: virtual
The virtual keyword is provided in C #, and the term defines the method as a way to support the declaration of a method that can be modified in a class, which is called a dummy method, and the word class can use the override keyword to freely implement their own version of the virtual method
Grammar:
[access modifier] virtual [return value type] Method name ([parameter list])
{
//implementation of virtual methods
}
1. Virtual access modifier cannot be used with access modifiers such as static and override
2, call the virtual method, at run time the system will automatically check to determine which implementation method of the tune.
keyword: new
The new access modifier is used to explicitly hide members that inherit from the base class, and if the derived class member has the same name as the base class member, new recognizes the derived class member as a completely new member.
1. Using both new and override in one method will make an error.
2. The real purpose of the new access modifier is to hide the base class method.
3, if a method life is new, it does not actually hide the base class method, and the compiler also generates a warning, you should delete new. Iii. Abstract classes and abstract methods
what is abstract class:Classes that cannot be instantiated are called abstract classes, and abstract classes are the base classes of derived classes.
Keywords: abstract
Grammar:
Abstract class name
{
... ....
}
1. An abstract class can contain both abstract and non-abstract methods.
2. Abstract methods are only really implemented in derived classes, which indicates that abstract methods only store function prototypes, do not involve the principal code,
3. A class derived from an abstract class needs to implement an abstract method of its base class to instantiate the object.
4. Using the override key to implement an abstract method in a derived class is called overriding a 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. The base class implements an abstract class, and the derived class does not need to re-implement the abstract class.
6. Abstract classes are not just an implementation technique, they represent an abstract concept, thus establishing a convention for all derived classes. Iv. Interface
Keyword: interface
Grammar:
modifier Interface Interface name
{
//Interface Body
}
1, an interface is equivalent to an abstract class, but it can not be half-shouted any implementation method.
2. Each method of an interface must be implemented in a derived class.
3, the interface can sometimes be regarded as the mold of the class, it indicates what a class should provide.
4, the interface body is limited to methods, indexers, attributes of the declaration.
5. The interface cannot contain fields, constructors, constants, and so on.
6. The interface member is implicitly exposed, and if you explicitly specify an access level for it, a compiler error occurs.
7, in the interface can not implement any method, property or indexer.
8. When specifying a method, simply give the return type, name, and argument list, and end with a semicolon.
9, the implementation of the interface syntax and implementation of inheritance, all with the colon ":"
Example
Interface Icustomer
{
... .........
}
Public class Myclass:icustomer
{
... .........
}
10, the method in the interface can not be rewritten, can only be implemented.
11.
Coding Standard:
The name of the interface must always be a capital letter I five, multi-interface implementation
The implementation of multiple interfaces in C # makes up for C # only single inheritance and cannot be multiple inheritance weaknesses.
Syntax Examples:
Public intetface Iname
{
void Name ();
}
Public Interface Iage
{
void Age ();
}
Public class Consoleshow:iage, Iname
{
Public void Name ()
{
//Concrete implementation
}
Public void Age ()
{
//Concrete implementation
}
} six, explicit interface implementation
If you have exactly the same signature in two interfaces, you can implement the interface explicitly by using the "interface name. Method Name" method.
Example:
Public Interface Iname
{
void Test ();
}
Public Interface Iage
{
void Test ();
}
Public Test:iname, Iage
{
void Iname Test ()
{
//The implementation of Iname excuses
}
void Iage Test ()
{
//The implementation of Iage excuses
}

}

Vii. Interface Inheritance
You can also create a new interface by merging other interfaces. The syntax for merging interfaces is very similar to inherited syntax, except that the former can combine multiple interfaces into one. To implement an interface for a class, you must write code for the base interface and for all members that derive an excuse.

Inheritance in C # (inheritance, abstract classes, and abstract methods, interfaces)

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.