Java programming ideology Study Notes (1), programming ideology Study Notes

Source: Internet
Author: User
Tags export class

Java programming ideology Study Notes (1), programming ideology Study Notes
Object Introduction

1. Abstract all programming languages provide abstract mechanisms. For example, assembly is a slight abstraction of the machine, and C language is an abstraction of assembly. In comparison, these languages have strong limitations in solving problems. Because they force you to think from the machine perspective. Programmers must establish associations based on machine Models and actual problem models. This remains unchanged for development. Because of this, the idea of object-oriented came into being.

In the object-oriented world, all problems can be viewed as objects. Programmers no longer have to be constrained by specific types of issues to be addressed. We can call the elements in the Problem Space and the representations in the space as objects. This sentence is very easy to read, but we only need to remember to regard any problem as an object. It is more suitable for describing our real world and closer to our human thinking.

Pure object-oriented programming:

  • All things are objects. The real world, the problems to be solved, and other conceptual components can be abstracted as objects.
  • Programs are a collection of objects. They send messages to tell each other what to do. This sentence may be ambiguous for beginners. Remember that the Code defines the type and the active objects in the program are always objects. A method in the called object is equivalent to "sending a message" to the object, and the object determines what to do based on the message.
  • Each object has its own storage composed of other objects. Generally, an object can contain other objects. It is with this feature that we can build a complex architecture based on object-oriented architecture that hides complexity from the simplicity of objects.
  • Each object has its type. This is easy to understand. An object is an active element in a program, and each object is a special instance of its type. For example, a person is a type, a person is an object of this type, a person is another object of this type, and a person are different entities, however, they belong to the "person" type.
  • A specific type of object can accept the same message. People can go, and birds can fly. A more profound understanding is that an object of the circle type must be a object of the "" type. Therefore, an object of the circular type must receive messages sent to a geometric object. Therefore, when writing code, try to write code that interacts with the "more basic type. Because the child type also applies to these codes. This kind of "alternative" is the embodiment of the upward transformation idea in java.

Summary: objects are stateful (represented by data members in java), behavior (represented by method members in java), and identifiers (each object can be completely distinguished from other objects ).

2. interfaces and services.

Each object must expose an interface and declare the services it can provide. The interface here and the keyword interface in java are not a concept. The interface here is equivalent to an external channel, specifically the method declared in the class.

  

 1 public class Light { 2  3     private int brightness; 4  5     public void on(){ 6         System.out.println("the light is on"); 7     } 8  9     public void off(){10         System.out.println("the light is off");11     }12 13     public static void main(String[] args){14         Light light = new Light();15         light.on();16         light.off();17     }18 }

This is a simple example. Light defines a lamp type, so all objects of this type should have this property, "brightness". All objects of this type should have these two interfaces: "on ", "off", an object of the Light type is created in the main function (program entry). The object name is light,

You can send a message to this object and enable or disable it by calling the "on" and "off" methods. Here I call the system class print string to simulate opening and closing. Properties are not used here, just to demonstrate the meaning of "object interface.

This example shows us the idea that while writing code, we should always think about what problems can be abstracted by the Types I have created, what kind of services (through interfaces) can my object provide to the outside world, and what classes should I call to provide such services (combination or inheritance of classes ), you can solve the problem if you want to understand it.

3. Access Control Mechanism

In some cases, it is necessary to control access to Members in the class. Developers can be divided into two types: the class creator, who creates the class, and the client programmer, who uses the class. Generally, the class creator only needs to expose the interfaces that users want to use, while hiding other parts to be protected, first, this mechanism can protect programs from being abused and destroyed by careless programmers. Second, it allows class designers to change the internal implementation of classes without affecting other programmers who use this class. This is the root cause for using the access control mechanism.

Java uses three keywords: public, private, and protected to set the boundary. Remember that the public modifier can be used anywhere. The private modifier can only be used within the class, the members modified by protected can be accessed within the class and inherit from the class, and protected has the default package access permission. Members without any keyword modification also have the default package access permission.

4. inheritance or combination

Code reuse is one of the biggest advantages of object-oriented design languages. The simplest thing is to create a type object and place it in another class, which is called "Creating a member object". This reuse method has great flexibility. For example, for the "ship" type, you need to have an object of the "Pulp" type as its data member. You can create such an object and put it into the "ship" type.

Applicable scenarios of inheritance: If you can use an existing class as the basis, you can create the required types by adding or modifying the class interface. The inherited class contains all the members of the base class, both public and private. Besides, it also copies all interfaces of the base class. That is to say, all messages sent to the base class can also be sent to the export class.

  

  

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.