[Idea] How to embody encapsulation, inheritance, and multi-state programmers !!!

Source: Internet
Author: User

Auto: http://www.cnblogs.com/cestcnsoft/archive/2011/03/24/1994096.html

What is encapsulation?

1. Protect data members fromProgramYou can access or modify data directly only through the provided public interface ==> Data encapsulation.

2. The details of the method are hidden to users. As long as the interface remains unchanged, modifications to the content will not affect external callers ==> method encapsulation.

3. An object contains complete attributes and corresponding methods is called encapsulation.

4. The attributes of an object cannot be directly accessed from outside the object, but can only be accessed through the method corresponding to the attribute.

5. The object method can receive messages outside the object.

For example:

Class

{

Private int prop;

Puplic int getprop ()
{
Return prop;
}

Public void setprop (INT prop)
{
This. Prop = prop;
}

}

The property prop is private and cannot be directly accessed from outside, but the outside can call the getprop () and setprop () methods,
Send a message to the object to complete a function.

What is polymorphism?

the concept of polymorphism is often referred to as "one interface, multiple methods ". This means that you can design a common set of related actions
. Polymorphism allows the same interface to be used by multiple actions of the same class, thus reducing the complexity of the program. Let's look at the dog as a metaphor.
A dog's sense of smell is polymorphism. If a dog smells like a cat, it will bark and chase it. If the dog smells food, it will be divided into
execute saliva and go to the bowl where food is filled. In both cases, the same smell organ is working. The difference is that two smells are asked.
different types of data apply to the dog's nose! In Java, two or more methods in the same class can have the same name, as long as
the parameter declaration is different. In this case, this method is called overload. This process is called method
overloading ). Method Overloading is a method for Java to implement polymorphism.

There are two ways to achieve polymorphism :*
1. Inheritance (subclass inherits the parent class (including abstract class, interface ect ))
2. Overload (in the same class)

For object-oriented programming, another important concept in Object-Oriented Programming is polymorphism. At runtime
Base Class pointer to call methods in the derived class. You can put a group of objects in an array and call their methods.
In combination, polymorphism is embodied, and these objects do not have to be of the same type. Of course, they all inherit from a class. You can
All the derived classes are placed in an array. If these objects have methods of the same name, you can call the methods of the same name for each object. The same operation does not act
The same object can have different interpretations and different execution results. This is polymorphism. Polymorphism uses a derived class to overload virtual functions in the base class
Method.

In object-oriented systems, polymorphism is a very important concept. It allows customers to operate on an object.
The system is responsible for explaining the specific actions of the series.

The word "polymorphism" was first used in biology, indicating that organisms of the same race share the same characteristics. In C #, polymorphism is defined as: the same
Operations Act on instances of different classes. Different classes are interpreted differently and different execution results are generated. C # support multiple types
Status:

> Polymorphism during compilation
Polymorphism is implemented by overloading during compilation. For non-Virtual members, the type returned by the system based on the passed parameters during compilation
And other information to determine the operation.
> Runtime polymorphism
Running polymorphism refers to the operation that is determined based on the actual situation until the system is running. In C #, The Runtime polymorphism is passed through the Virtual Member
.

The polymorphism during compilation provides us with a fast running speed, while the polymorphism during runtime brings us a high degree of flexibility and abstraction.

Object-Oriented programming methods have four basic features:
1. Abstraction:
Abstraction is to ignore those aspects irrelevant to the current target in a topic, so that you can pay more attention to the aspects related to the current target. Abstraction
It is not intended to understand all the problems, but to select some of them and ignore the need for some details at the moment. For example, we need to design a Student Score
Management System: When examining a student, we only care about his class, student ID, and score, instead of concerning his height and weight.
Abstract involves two aspects: Process abstraction and number abstraction. Process abstraction means that any operation with clearly defined functions can be viewed by users.
Although a single object may actually be completed by a series of lower-level operations, data abstraction defines the data type and applies to this class.
Type object operation, and limits the object value can only be modified and observed by using these operations.

2. inheritance:
inheritance is a hierarchical model of the concatenation class, in addition, it allows reuse with encouragement. It provides a clear way to express commonalities.
A new class can be derived from an existing class, this process is the inheritance of the class. The new class inherits the features of the original class. The new class is called the derived class (subclass) of the original class.
the original class is called the base class of the new class (parent class ). The derived class can inherit methods and instance variables from its base class, and the class can be modified or added
method to make it more suitable for special needs. This also reflects the general and special relationship between nature. Inheritance solves the reusability of software.
for example, all Windows applications have a window, they can be viewed as derived from a window. But some applications
for text processing, and some applications for drawing, this is because different subclasses are derived and different features are added to each subclass.

3. encapsulation:
encapsulation is one of the object-oriented features, it is the main feature of object and class concepts. The encapsulation process is surrounded by data, and data access is only allowed
use the defined interface. Object-oriented computing begins with the basic concept that the real world can be depicted as a series of completely autonomous and encapsulated objects.
some objects access other objects through a protected interface. Once an object is defined, it is necessary to determine the Observability of these features, that is, which
some features are visible to the external world and used to indicate internal states. Define the interface of the object at this stage. Generally, direct access to one
actual representation of objects, you should access the object through the operation interface, which is called Information Hiding. In fact, Information Hiding is the recognition of user encapsulation. encapsulation
This provides support for information hiding. Encapsulation ensures that the module has good independence, making program maintenance and modification easier. Only
internal to the class, therefore, the impact of application modifications can be minimized.

4. Polymorphism:
Polymorphism allows different types of objects to respond to the same message. For example, in the same addition, add two time points together and add two integers
They must be completely different. For example, the Text Processing Program and the drawing processing program have different effects by selecting the edit-paste operation. Polymorphism
Including parameterized polymorphism and inclusion polymorphism. Polymorphism languages are flexible, abstract, and behavior-sharing,CodeThe advantages of sharing can effectively solve the application process
Sequential naming.

When an object is used, it is called an object-based object. The inheritance is added based on the object and thus becomes an object-oriented object.
Object-based encapsulation is the biggest feature. Private, protected, and public are not mandatory Based on the object language. Note that
Encapsulation does not provide much help for the application itself.

Related Article

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.