Introduction to C # Object-oriented (OOP)-Next day-polymorphism and inheritance (inheritance)

Source: Internet
Author: User

Introduced:

The first day's content is mainly the method overloads under different scenarios. This section focuses on the concept of object-oriented inheritance. Start by defining the inheritance with a single point graph.

Inheritance

A simple example:

ClassA:  class CLASSA:CLASSB    {public        void Display1 ()        {            Console.WriteLine ("ClassA Display1");            Base. Display1 ();        }    } Classb:class ClassB    {public        int x = +;        public void Display1 ()        {            Console.WriteLine ("ClassB Display1");        }        public void Display2 ()        {            Console.WriteLine ("ClassB Display2");        }    } Class program    {        static void Main (string[] args)        {            ClassA a = new ClassA ();            A.display1 ();            Console.readkey ();        }    } /* Output Result: ClassA Display1 ClassB display1*/

In the example above, CLASSB is the parent class, ClassA is a subclass, ClassA inherits from ClassB (or CLASSB derived classa).

This example also gets a warning: "Classa.display1 ()" Hides the inherited member "Classb.display1 ()".  If it is intentionally hidden, use the keyword new. This means that the members in the ClassA are like members of the CLASSB, so the base class is hidden. If this is intentional, you can replace it with "public new void Display1 ()".

Knowledge Points: We can define a member in a subclass with the same name as the base class, and the methods in the subclass always take precedence and then the base class (when the case with the same name is encountered). Of course, you can also use the base keyword to call a member of a base class in a subclass. In addition, inheritance cannot be backwards compatible (meaning that an instance of a base class cannot invoke members of a derived class, which is well understood). In addition to constructors and destructors, we can inherit any member from a base class.

For members of a class, there are two types:

    • Static Member: Class with the static keyword, which belongs to this class and can be accessed directly
    • Ordinary member: accessed by an instance of this class, he only belongs to the instance created by this class.
public class ClassB {} public class Classa:classb {}//compiled is actually: public class Classb:object{}public class classa:classb{ }

In C #, if a class does not inherit any class, it is also inherited from the object class by default, which is the common base class for all classes. Other special classes such as: System.ValueType, System.Delegate, etc. cannot be inherited.

Let's look at an example:

public class CLASSW  {  } public class  CLASSX  {  } public  class CLASSY:CLASSW, classx  {  }

When you define Classsy, you will get an error. Knowledge points: In C #, a class can inherit from only one class and does not support multiple inheritance. (In fact, C # Multiple inheritance can be done through the interface)

Let's look at an example:

public class Classw:classy {} public class CLASSX:CLASSW {} public class classy:  classx {}

This also gives an error: the cyclic base class dependencies that involve "classy" and "CLASSW" .... (3 errors)

Knowledge Points: Cyclic dependencies (Circular dependency) are not allowed in C #. is to inherit from each other

Let's look at an example:

public class ClassB {public     int b = +;} public class Classa:classb {public     int a = +;}//<summary> Program:used to execute the method. Contains Main method. </summary> public class Program {     private static void Main (string[] args)     {         ClassB ClassB = new Clas SB ();         ClassA ClassA = new ClassA ();         ClassA = ClassB;         ClassB = ClassA;     } }

There is an error in this example: the type "CONSOLEAPPLICATION3.CLASSB" cannot be implicitly converted to "Consoleapplication3.classa". An explicit conversion exists (is there a missing cast?)

Knowledge Points: We can assign a derived class object to the base class, but not the other way round. Both (base class object = Derived class object, yes, derived class object = base class object, NO)

Summary: This paper mainly discusses the basic concept of inheritance.

  1. We can declare a function of the same name in a derived class that has already been declared in the base class. < Span id= "nohighlight_0.8657674915297932" > < Span id= "nohighlight_0.6380952076853446" >
  2. The base class always executes first when the method is called by the
  3. derived class object. < Span id= "Ouhighlight__47_47to6_6" > < /span>
  4. reserved keywords "base" derived for call base class method
  5. Inheritance cannot be backwards compatible.
  6. In addition to constructors and destructors, a class can inherit everything from its base class .
  7. In C# InOfInherited,CustomClassNoDerivedSelf -Special Jian in c# command , system.enum system.delegate system.array et
  8. The parent class can have only one . C # does not support multiple inheritance through classes .
  9. In C#In theInheritedInNoAllowCycleDependent items< Span id= "ouhighlight__132_136to45_45" > < Span id= "ouhighlight__157_157to56_56" > < Span id= "ouhighlight__172_179to62_63" > < Span id= "ouhighlight__218_226to70_72" >
  10. we can no
  11. we can't will int implicit convert char,< Span id= "ouhighlight__45_47to18_18" > but char can convert

Annotated: Original address: https://codeteddy.com/2014/05/16/diving-in-oop-part-2-polymorphism-and-inheritance-inheritance/ I just made a translation and summary on the basis of the author, and added a bit of my own understanding. Hope to be of help to everyone. Please correct me if there is any mistake.

Introduction to C # Object-oriented (OOP)-Next day-polymorphism and inheritance (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.