20150429 C # Object-oriented inheritance and polymorphism

Source: Internet
Author: User
Tags what inheritance

Inherited
First, what inheritance?
1. The associated member of the parent class is automatically derived to the subclass. Public protected
2. Subclasses can extend the associated members of a parent class.
3. Subclasses can be used instead of the parent class, but the parent class cannot be used instead of subclasses.
Second, the inheritance of the syntax:
Class Subclass Name: Parent class name
{

}
Iii. Rules of Succession:
(i) Inheritance of member variables
1.public protected
2. If the same name is hidden. Subclasses and parent classes each use their own members.
(ii) Inheritance of attributes
1. Properties generally only public
2. If the same name is hidden. Subclasses and parent classes each use their own members.
(iii) Inheritance of methods
1.public protected
2. If the same name is hidden. Subclasses and parent classes each use their own members.
3. Rewrite:
A. Set the method of the parent class to a virtual method, virtual. --The parent class says: This method can be rewritten.
B. In the subclass, override the method in the parent class using override. --Subclass Confirm I rewrite this method.

Note: 1. If virtual is not declared in the parent class method, there is an override in the subclass to override it. Will error.
2. If virtual is declared in the parent class, there is no override in the subclass to rewrite it, no error, but it will be hidden.
3. Virtual method of a virtual end. Subclasses this method is still a virtual method, even if the virtual method of the parent class is override.

(iv) Inheritance of constructors
1. If the parent class does not have a write constructor, or the parent constructor does not have parameters. There are no special requirements for the constructors of subclasses.
2. If the constructor for the parent class has parameters, the subclass constructor must pass the value to the parent class constructor. Base ();

Note: When you call a method of a parent class in a subclass, you can use Base.xxxx to invoke it. Methods that are generally overridden by calling the parent class.

base--the parent class object.
this--the current object.


Polymorphic: Not a new thing. Implemented on the basis of inheritance.
A polymorphic implementation is a reference to a parent class that points to an instance of a subclass. When referring to different subclasses, the parent class refers to a different pattern when calling a subclass method.

20150429 C # Object-oriented inheritance and polymorphism

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.