C # object-oriented design for theoretical learning

Source: Internet
Author: User

Principle of encapsulating objects

Inheritance

Polymorphism

Interface

 

    1. Encapsulation is to hide the internal members and implementations of the class.

Benefits

Good encapsulation can reduce Coupling

Class internal implementation can be freely modified

Class with clear external interface

Data Hiding

Is one of the most useful methods of encapsulation.

Implementation Method: access restriction Modifier

Public is unrestricted and anyone is allowed to access it.

Ø protected internal = protected + internal

Ø internal allows the project orProgramInternal class to access

Ø when protected is inherited, The subclass can have full access to the base class.

Ø private only allows access by members in the same class

L attributes and indexers are also used to encapsulate class details and provide public interfaces to users of this class.

    1. Inheritance)

A class can be directly obtained from another class.CodeAnd Data

L The derived class obtains all its members from the base class.

Lpublic class insuranceaccount: Account

LC # only supports single inheritance. Multi-inheritance can implement the same function through interfaces.

Prevent inheritance

Public sealed class xx

Example:

Form look and feel designed by GUI

From. Net form class

Inherited

L if the class child inherits the class parent

The child object contains a parent object.

How to access base class members

  1. The derived class can call the methods of the base class.
  2. Use the base keyword
  3. A derived class has certain limitations when accessing the base class and cannot access private members. The base class members of internal can only be accessed by the derived classes in the same assembly.
  • Polymorphism
                • Polymorphism during compilation: Overload: Used in the same class
                                                            • The method name must be the same
                                                            • The parameter list must be different.
                                                            • The return value types can be different.
                                                    • Running polymorphism: override (override), which can be overwritten.
                                                    • The method name must be the same
                                                    • The parameter list must be the same
                                                    • The return value type must be the same.
  • Interface
  • the interface provides a blueprint for the class. a pm-level person usually defines a series of interfaces during design.

    the client needs some interfaces. The server can implement these interfaces to meet the requirements of the client. functions similar to the Protocol

    the interface can be derived from multiple basic interfaces

    all methods in the interface must be public

    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.