Lesson 3rd: Modern Visual Object Programming

Source: Internet
Author: User
Principle of encapsulating objects:

1. What is encapsulation? Advantages of encapsulation?

Hide the class
Good encapsulation can reduce coupling;
The internal implementation of the class can be freely modified;
Class has clear external interface

II. Implementation:

1. Access limiters
Public; protected (class interior and its subclass); internal (same assembly); private (class interior)
Default Value: private
Protected internal = protected + internal (for the same project, subclass can access the base class)

2.

Attributes and indexers are also used to encapsulate class details and provide public interfaces to users of this class. Inheritance
1. derived class: base class
2. C # Only single inheritance is supported. Multi-inheritance means that a subclass has multiple parent classes. To implement multi-inheritance, you must use interfaces.
3. prevent inheritance: public SealedClass XX4. the derived class can call the method of the base class: BaseBase Class

1 public class Account
2 {
3 public double balance;
4 public bool Withdraw (double amt)
5 {
6 balance-= amt;
7 return true;
8}
9} derived class

1 public class CheckAccount: Account
2 {
3 public bool Withdraw (double amt)
4 {
5 if (amt <= base. balance)
6 return base. Withdraw (amt );
7 else
8 return false;
9}
10}

When to use inheritance
1. code reuse
2. design reuse (put public fields and methods in the parent class, and private in the subclass)
3. Experience (a is inherited by B, so a is a B)

Polymorphism 

The same operation acts on different objects and can have different interpretations to produce different execution results. This is polymorphism.


Polymorphism during compilation (overload) and polymorphism during runtime (override overwrite)

Overload

Location: exists in an inherited class
Method Name: must be the same
Parameter List: Must be different
Return Value: can be different

Override

Location: exists in the same class
Method Name: must be the same
Parameter List: must be the same
Return Value: must be the same

Virtual

If you want to override the methods in the parent class in the subclass, the method of the parent class must be a virtual method.

Abstract

In the parent class, only the declaration of abstract methods is implemented in the subclass.
If an abstract method exists in the class, the class must be declared as an abstract class (abstract classes are not commonly used, and interfaces are used)

Interface
1. Interfaces provide blueprints for classes (the purpose of interfaces is to build a framework and wait for implementation)
2. interfaces only provide definitions (always public, and only method definitions are not implemented. [This is the same as abstract methods ])
3. The data type of the implemented interface must be implemented by interface members.
4. The interface itself can be derived from multiple basic interfaces
Comparison between interfaces and abstract classes:
Similarities:
1. All methods are defined and not implemented.
2. Just as the subclass inherits the parent class, the implementation of the abstract method of the parent class must be provided. If a class inherits the interface, the method body must be provided for implementation.
For example, public class admin_user: Iadmin_user indicates that the class admin_user provides the implementation of methods in Iadmin_user.

Differences:
1. the method in the interface will always be public
2. The interface only has the definition of methods, while the abstract class can have the definition of general methods and other class members, such as attributes and fields.
3. Classes only support single inheritance, while interfaces support multi-inheritance.

Interface

Public interface IAccount
{
Void Method1 ();
String Method2 (int x, int y );
...
} Implementation class

Public class InsuranceAccount: IAccount
{
Void Method1 ()
{
...
}
String Method2 (int x, int y)
{
...
}
}


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.