Object-oriented basis (IV.)

Source: Internet
Author: User

Abstract classes and abstract methods

1 . By adding the abstract keyword in front of a class, this class becomes an abstract class .

(1) When a class becomes an abstract class, it cannot be used as an object , that is, a new object cannot be created, and cannot be initialized.

(2) Abstract method is only a method, cannot write method .

(3) When a class inherits an abstract class, it is necessary to rewrite The abstract method of the abstract class to implement the method.

(4) Abstract methods must be placed in the abstract class to be used, the abstract method is implemented in the abstract subclass, that is, the abstract subclass implementation of the abstract parent class all abstract methods.

(5) Abstract methods must be abstract classes, abstract classes, but not necessarily abstract methods.

(6) The abstract method of the parent class must be obtained by abstracting the sub-class to output the value of the method;

Conclusion: Abstract classes can only be used as parent classes.

2. Abstract methods

  Note: Abstract methods cannot have implementation methods, followed by a semicolon directly after the function name; To implement an abstract method, you must have an abstract subclass to implement the method.

Define the method in the abstract class (parent Class):

Implemented in an abstract subclass:

3. Abstract attributes (encapsulation)

Second, the interface

Definition: The keyword interface is used to define an interface.

"Precautions"

1. A subclass can inherit multiple interfaces, resolving a subclass that inherits only one parent class's properties and methods.

2, in the interface, if the subclass inherits the method, it must implement this method.

Within an interface, you cannot define a property, a method, or a field.

Iii. Commission (delegate)

Understanding: A delegate can be seen as a container for a function that can refer to any function that satisfies its requirements.

Example: The delegate is equivalent to the screwdriver "base", to use what type of screwdriver (flat, plum, cross-type) is predetermined, as long as the conditions will be placed on the base.

Definition: A delegate can be considered a data type and can be used to define a variable, but he is a special type of data, and the variable that it defines can receive a value that can only be a function.

How to use the usage method:

Object-oriented basis (IV.)

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.