Three characteristics of polygon relativity in C #: encapsulation, inheritance, polymorphism, and the difference between hidden (new) and method override (override) and overloading (overload) in C #

Source: Internet
Author: User

Overloads, overrides, and hidden definitions:

Overloading: Occurs within the same scope (for example, inside a class) and defines a series of methods with the same name, but the method has a different argument list. This allows you to decide which one to call by passing different parameters. A different return value type does not make an overload.

Override: Occurs when inheriting, redefining a method in the parent class in a subclass, the method in the subclass is the same as the method of the parent class
For example: The base class method is declared as virtual (virtual method), and the override in the derived class is used to declare this method.

Hidden: The base class method does not make a declaration (default is a non-virtual method), and in a derived class uses new to declare the hiding of this method.

When overloaded, the calling method is selected according to the parameters;
When overridden, access to the parent class subclass invokes the subclass's overriding method;
When hidden, access to the parent class invokes the method of the parent class, the subclass of the child class.

Hide (New) Example:

 using System;
class A
   {  
Public void F ()
         {  
Console.WriteLine ("A.F");
         }  
   }  
class B:a
   {  
new public void F ()
         {    
Console.WriteLine ("B.f");
         }  
   }  
class Test
   {  
static void Main (string[] args)
         {  
b b = new B ();
B.f ();
a = b;
A.F ();
         }  
   }  
output to
B.f
A.F 

overriding virtual (virtual method) Example
using System;
class A
   {  
Public virtual void F ()
         {  
Console.WriteLine ("A.F");
         }  
   }  
class B:a
   {  
Public override void F ()
         {    
Console.WriteLine ("B.f");
         }  
   }  
class Test
   {  
static void Main ()
         {  
b b = new B ();
B.f ();
a = b;
A.F ();
         }  
   }  
output to
B.f
B.f

Add: Override overrides are generally used for interface implementation and method rewriting of inheriting classes, note that

1, the mark of the method of covering must match with the mark of the method that is covered completely, can reach the effect of coverage;
2. The return value of the overridden method must be the same as the return of the overridden method;
3. The exception that is thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;
4. The overridden method cannot be private, otherwise only a new method is defined in its subclass, and it is not overwritten.

Polymorphic

1) Inheritance embodies polymorphism: A parent type variable can refer to various instances of a subtype, or it can receive a subclass object.

2) Individual polymorphism: The subtype instances of the parent type are varied.

Polymorphism of behavior: Parent type definition Method The quilt class overrides to a variety of overloads, which are also polymorphic methods.

Do not appear to turn the parent class object into a subclass type, it will look like an exception!

3) polymorphic premise: Must be a relationship between classes and classes. Either inherit, or implement. There is usually a precondition: there is coverage.

4) The advantages of polymorphism: the appearance of polymorphism greatly increases the expansibility of the program.

The disadvantage of polymorphism: Although extensibility is improved, members in the parent class can only be accessed using a reference to the parent class.

5) Characteristics of member functions in polymorphic states:

① at compile time: see if there is a method called in the class to which the referenced variable belongs. If there is, compile through, if no compilation fails.

② at run time: see if there are methods called in the class to which the object belongs.

③ Simple Summary is: Member method in polymorphic call, compile look to the left, run look to the right.

6) in polymorphism, the characteristics of member variables: both compile and run, refer to the left (the class to which the reference variable belongs).

7) in polymorphic, static member methods and attributes: both compiled and run, refer to left.

8) The parent class reference points to the subclass object, which is transformed downward when the parent class wants to use properties and methods that are specific to the subclass.

Three characteristics of polygon relativity in C #: encapsulation, inheritance, polymorphism, and the difference between hidden (new) and method override (override) and overloading (overload) in C #

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.