C # Object-oriented-inheritance

Source: Internet
Author: User

1. Inheritance

Inheritance: A class can have an inheritance relationship with a class, and a class inherits only one parent class, but can be inherited by many classes

The two classes that make up an inheritance relationship: The parent class or the base class;

The basic properties of the parent and child classes:

is a (is_a) relationship: A subclass is a special case of a parent class.

Extended (Extends) Relationship: Subclasses have functionality that the parent class does not have.

2. Access privileges for class members
Public Public access is not restricted
Private private Only members of the class can access
Protection protected Subclasses can access, other classes cannot access

(1), Public and private
Primarily used to define member access permissions for a single class

(2), protected
When a class member is defined as protected, all outside classes cannot access it, but its subclasses can access it.

When a subclass object is created, the outside world can access all members of the subclass and the public members of the parent class.


Class member access rights under inheritance conditions:

Everything that doesn't have to be known to outsiders is private.
All services that need to be delivered to the outside are public.
All the "ancestral trick", "secret" is protected.

(3), internal

Confined within the same assembly
Internal is the default accessibility of C #, which means that if a class does not have any accessibility keywords in front of it
, then it is internal.

3. Mutual assignment of parent class variables of subclass

* The difference between a reference type and a value type

Summarize:

There are three parts: the subclass inherits the parent class; The subclass is converted to the parent class type; the parent class is converted to a subclass

Inheritance: The relationship between classes and classes: parent-child relationships

Subclass inherits Parent class, a class can inherit only one parent class

When a subclass inherits the parent class, it has the public properties and methods of the parent class

A subclass object can be converted directly to a parent class type, but the parent object cannot be converted directly to a subclass type, and when a parent class object is converted through a subclass object, the properties and methods of the subclass cannot be ordered through the parent class type.

Only when the parent class object is converted from a subclass object can it be converted back

An equality assignment for an object that is actually assigned a pointer to the
Class1 C = new Class1 ();
Class1 d = c; Then D and C are a thing.

Subclass objects can be used as base class objects

If you are sure that the object referenced in the parent class variable is indeed a subclass type, you can assign a value by type casting:

Sub-class object variable = (subclass name) base class object variable;

or using the AS operator

Subclass object variable = base class object variable as subclass name

4. Method overloading, hiding and virtual method invocation

Extension: Subclass method, parent class not;

Overloading: A subclass has a function with the same name as the parent class, but the parameter type or number is different;

Exactly the same: The subclass method is exactly the same as the parent class method from the method name to the parameter type.

(1) Heavy Duty (Overlord)

function with the same name, different parameter types

Parent class sub-class

Same name function with same parameter type

Type Conversions

Summary :

When a subclass inherits a parent class, a method in the parent class with a different parameter with the same name as the child class can be overloaded with the subclass method
The parent class can only read its own methods and cannot invoke subclasses.
When a parent class has a method that has a parameter with the same name as the child, the parent class object is modulated by the parent class's method
A method of a subclass is called by a subclass type

(2), hide

(3), overriding and virtual method invocation


The parent class is virtual and the subclass is overridden.

The object-oriented language has a "virtual method invocation" attribute that allows us to use the same statement to perform different operations depending on the object type.

C # Object-oriented-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.