C # Object-Oriented Design

Source: Internet
Author: User

1. Principle of encapsulating objects

Encapsulation is to hide the internal class. Advantages: 1. Good encapsulation can reduce coupling, 2 internal implementations can be freely modified, and 3 have clear external interfaces.

2 Inheritance

How to access base class members

The derived class can call the method of the base class. Through the base keyword, the derived class has some restrictions when accessing the base class. Private Members cannot be accessed. The base class members of interal can only be accessed by the derived classes in the same assembly. If the class child inherits the class parent, the child object contains a parent object.

3 polymorphism

Polymorphism requires a derived class to rewrite virtual functions in the base class. virtual functions are also called virtual functions. The virtual keyword Public Virtual bool withdraw (...) is used (....) {..}, when a virtual method is called, The runtime determines the class instance when the object is called, and calls an appropriate rewrite method.

Rewrite method: 3 Requirements: Same method name, same parameter list, same return value

Overload method 3 Requirements: the same method name, different parameter lists (type and quantity), can return different values

Abstract Method: A derived class must implement all abstract methods in the base class (the abstract method does not have a method body). If the class contains abstract methods, the class must be declared as an abstract class.

When to use inheritance

Code reuse to reduce coding workload

Design reuse: common fields and methods can be placed in the parent class, and then a new subclass is derived from it. The subclass has its own fields and methods.

Class Structure in. net

4 Interface

The interface provides a blueprint for the class

The interface only provides definitions

The data type of the implemented interface must be implemented by interface members.

The interface itself can be derived from multiple basic interfaces

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.