C # 's understanding of inheritance

Source: Internet
Author: User

Inheritance: The main implementation of inheritance code reuse, save development time
Inheritance in C # has the following characteristics
1. A derived class can overwrite an inherited member if it defines a new member with the same name as the inherited member.
2. transitivity. If a inherits B,b and inherits C, then a can inherit the members declared in B as well as the members of the Declarations in C (properties, methods ...).
3. Constructors and destructors cannot be inherited, others can be inherited. The ability to access the members of the base class is defined by the access modifier: Private: Only the class itself can be accessed. Protected: Classes and derived classes can be accessed. Internal: Only classes in the same project can be accessed. Protected Internal: A combination of Protected and Internal. Public: Full access.
4. Derived classes are extensions of the base class, and derived classes can define their own members in extra.
5.base can call a member method of the parent class, in addition to constructors and destructors
6.new: The expression on the variable and the method is relative to the inheritance, the parent child (subclass) 2 classes have a variable with the same name param if the param variable of the parent class and the Param variable of the subclass are not the same then the subclass needs to be preceded by a new one when declaring Indicates that the variable is not a parent class, such as public new string id= "2";

public class Person {
Protected string id = "1";
Protected string name = "Wzm";
Public person () {
Console.Write ("Person class \ n");
}
public virtual void GetInfo ()
{
Console.WriteLine ("Id:{0},name:{1}", Id, name);
}
}
public class Employee:person
{
Public Employee (): Base ()//Call the constructor of the parent class * *
{
}
private string id = "2";
public override void GetInfo ()
{
Base. GetInfo (); Call the GetInfo () method of the parent class
Console.WriteLine ("Employee ID: {0}", Id);
}
}
Class Program
{
static void Main (string[] args)
{
Employee E = new Employee ();
E.getinfo ();
}
}

C # 's understanding of inheritance

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.