C # inheritance

Source: Internet
Author: User


C # class inheritance
(1) struct cannot be inherited
(2) All members of the base class will be inherited by the derived class, but cannot be accessed after private inheritance, and Other types can be accessed.
(3) When a class is inherited, there can be only one base class, and it must be placed behind the ":" (next to each other), followed by multiple interfaces.
(4) When the base class method is declared as virtual, the derived class can use override to override this method.

public class Xt{int xx;public override string ToString(){return "what a fine day it is";}}


(5) When the base class method is abstract, if the derived class is no longer abstract class, this method must be implemented.
(6) modify a class or class member through sealed
This class is sealed when sealed modifies the class, so it cannot be used as the base class of other classes.
public sealed Tc{//...}


When sealed is used to modify attributes or methods, it must be used with override, indicating that the method cannot be changed after it is overwritten.


The override method must be based on virtual, abstract, or override.
Override indicates that when a derived class calls a method of the same name, it will not call the base class, but will be overwritten.
  public sealed override string ToString()        {            //return base.ToString();            return "what a fine day iterator is";        }


(7) use new to hide fields, attributes, and methods in the base class.
In this case, the method with the same signature and field of the same name in the base class will be hidden rather than overwritten. new is not required, but an alarm will be triggered if new is not added.


(8) differences between new and override
New is similar to the redefinition in a C ++ derived class. override is similar to a virtual function in a C ++ class. It is called through the pointer or reference of the base class, and the specific function to be called is determined during runtime.


Example:
namespace ConsoleApplication5{    class Program1    {        public sealed override string ToString()        {            //return base.ToString();            return "what a fine day iterator is";        }        public void print(int x)        {            Console.WriteLine("this is int in Program1:{0}",x);        }        public virtual void print(string str)        {            Console.WriteLine("this is string in Program1:{0}",str);        }        }    class Program2 : Program1    {        //public override string ToString() //error        //{        //    return "funck";        //}        public void print(int x)        {            Console.WriteLine("this is int in Program2:{0}",x);        }        public override void print(string str)        {            Console.WriteLine("this is string in Program2:{0}",str);        }    }    class Program    {        static void Main(string[] args)        {            Program1 A1 = new Program1();            Program1 A2 = new Program2();            A1.print(1);            A1.print("A");            A2.print(1);            A2.print("A");            Program2 B = new Program2();            B.print(1);            B.print("A");                                }    }}


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.