Java Programming Ideas (I. Introduction to Objects)

Source: Internet
Author: User
Tags export class

Record the thinking in learning. Mainly to help oneself consolidate the knowledge, but also hope that through their own summary, for Java beginners to provide some inspiration. Pain and happiness, insist on good.

At the beginning of a variety of Daniel's online blog to see the recommendation of "Java programming thought" bought, found that really is a very good book, recommend a certain programming basis for people to learn.

Learn about Java. First, start with the JDK. is also the most basic thing.

JRE (Java Runtime Environment) Java Runtime Environment. Includes both the Java Virtual machine and the Java class Library. A Java program can be run by a computer with a JRE installed. The JRE is the software environment required to run a program written in the Java language and is provided to the user who wants to run the Java program. (In general, the JRE is used to support Java programs running)

JDK (Java Development Kit) Java Development Kit. The JDK contains the JRE, as well as compiler Javac, which compiles Java source code, and includes many tools for debugging and analyzing Java programs: JCONSOLE,JVISUALVM and other tools. is provided to the programmer for use. (in general, JDK is used to support the development of Java programs)

1, object-oriented programming (Object-oriented Progarmming,oop).

2, abstract. The complexity of the problems that people can solve directly depends on the type and quality of the abstraction.

3, the Pure object-oriented programming method has so 5 characteristics. 1) All things are object 2) programs are collections of objects that tell each other what they want to do by sending messages. 3) Each object has its own storage composed of other objects. (for example, a car object that contains a tyre object.) )

4) Each object has its type. That is, each object is meant to be an instance of a class. 5) All objects of a particular type can receive the same message. It's easy to understand that everyone can eat.

4. Each object has an interface. The interface determines the requests that can be made to a particular object. However, there must be code in the program that satisfies these requests, which together with the hidden data constitute the implementation.

New Light (); It.on ();
Light (type name)

On () interface

Off () interface

Brighten () interface

Dim () interface

When you "send a message" to an It object (generating a request), the method associated with it is called. The above code creates a light object by defining a "reference" to the object, and then invoking the new method to create an object of that type. In order to send a message to an object, you need to declare the name of the object and connect a message request with the origin symbol.

5. Service is provided for each object. When trying to develop or understand a program design, one of the best ways is to think of the object as a "service provider", the program itself providing services to the user, which will do this by invoking the services provided by other objects. Seeing an object as a service provider also has an accompanying benefit: it helps to improve the cohesion of the object. High cohesion is one of the basic quality requirements of software design: This means that a software artifact (such as an object, and of course it may also refer to a method or an object library) is "well-composed" in all aspects. In a good object-oriented design, each object can do a good job, but it doesn't try to do more. Using an object as a service provider is a great simplification, not only in the design process, but also when others try to understand your code or reuse an object, and if they see the value of the service the object can provide, it makes it much easier to adapt the object to its design.

6, the specific implementation is hidden. There are two types of programmers: one is the creator of the class (the programmer who creates the new data type), and the other is the client programmer (the class consumer who uses the data type in its application). Most programmers belong to the second, but all should try to change. Should not only be satisfied with the writing on the business.

The goal of the client programmer is to collect a variety of classes for rapid application development. The goal of the class creator is to build the class, which exposes only the necessary parts to the client programmer and hides the other parts. The hidden part is usually the fragile part of the object, but the modification is wrong and the interface will not be used directly. Therefore, to prevent client programmers from accidentally modifying the destruction, the hidden implementation can reduce the program bugs, but also convenient for the client programmer to use. Then there is access control, one is to let the client programmer can not touch the parts they should not touch, and the other is to allow the library designer can change the way inside the class without fear of affecting the client programmer.

In Java, three keywords are used to set boundaries within a class: public, private, protected. These access-specific words determine who can be used by what is defined immediately after. Public indicates that the element immediately followed is available to anyone. Private means that the element immediately following it cannot be accessed by anyone other than the type creator and the internal method of the type. Private is like a wall between a class creator and a client programmer, and if someone tries to access a private member, it gets an error message at compile time. The protected keyword is equivalent to private, except that the inherited class can access the protected member, but not the private member. Java also has a default access permission that will work if you do not use any of the previously mentioned access to the specified word. This kind of permission is often referred to as package access rights. Under this permission, a class can access members of other classes in the same package, but outside of the package, these members are specified as private.

7, Reuse concrete implementation. Once a class is created and tested, it should represent a useful unit of code. The simplest way to reuse a class is to use an object of that class directly. You can also place an object of that class in a new class. We'll call it "create a member object". The new class can consist of any number, any type of other object, in any way that can implement the functionality desired in the new class. A new class is synthesized in an existing class, and this concept is called a combination. If the combination occurs dynamically, then it is often called aggregation. A combination is often considered a "has-a" (owning) relationship. Members of the new class are usually declared private, making it impossible for client programmers using the new classes to access them. This also allows you to modify these members without interfering with existing client code, or you can modify these member objects at run time to implement dynamic modification of the program's behavior.

8, inheritance. When an existing type is inherited, a new type is created. This new type not only includes all members of the current type, including private members, but it also replicates the interface of the base class. In other words, all messages that can be sent to the base class object can also be sent to the exported class object. The export class has the same type as the base class. The base class has the same underlying interface as the derived class, so there must be some specific implementations of these interfaces. Simply inheriting a class without doing anything else the methods in the base class interface will inherit directly from the exported class. This means that the object of the exported class not only has the same type as the base class, but also has the same behavior, which makes little sense. There are two ways to make the base class differ from the derived class. The first: Add a new method directly in the export class. This means that the base class method does not meet all of your requirements, so you must add more methods. The second is to directly change the behavior of the method of the existing base class, overriding the base class method.

9, "is a" and "like is a" relationship. If the export class only overrides the base class method. Then the export class and base class are exactly the same type. This is a purely alternative. This situation scares the relationship between the base class and the derived class as a is-a relationship. Sometimes if you have to add a new interface element to the export type, it also expands the interface. Although this new type can still replace the base class, the substitution is not perfect, and the base class cannot access the newly added method. This situation is the is-like-a relationship.

10. Interchangeable objects that accompany polymorphism. Sometimes, you want to treat an object as a base class object rather than as the specific type it belongs to. This allows people to write code that does not depend on a particular type. The compiler is unlikely to produce a function call in the traditional sense. A non-object-oriented compiler produces a function call that causes so-called pre-binding, which means that the compiler generates a call to a specific function name, and the runtime resolves the call to the absolute address of the code that will be executed. In OOP, however, the program does not determine the address of the code until it is run, so other mechanisms must be used when a message is sent to a generalization object. In the object-oriented programming language, late binding is used, and when a message is sent to an object, the called code is not determined until run time. The compiler ensures that the called method exists and performs type checking on the invocation parameters and return values (the language that cannot provide such assurances is called a weak type), but does not know the exact code that will be executed. In performing late binding, Java uses a small piece of special code to replace the call of an absolute address. This code uses the information stored in the object to calculate the address of the method body. Thus, each object can behave differently depending on the content of this small piece of code. The JVM runtime determines the correct type of object and then calls the correct method.

11, a single inheritance structure. In Java, all classes eventually inherit from a single base class (Object). This ensures that all objects have some functionality (the interface of the object base class). This makes the garbage collector's implementation much easier. All objects are guaranteed to have their type information. Therefore, there is no deadlock in the inability to determine the type of the object.

12, containers. Used to hold objects. There are various types of containers in Java that can meet different needs. List (for storing sequences), MAP (associative array, used to establish an association between objects), set (one for each object type), queue, tree, stack, etc. From a design point of view, really just need a sequence that can be manipulated to solve the problem. But in fact, you have to choose the container. First: Different containers provide different types of interfaces and external behavior. such as stacks and queues. These two are not the same. Second: Different containers have different efficiencies for some operations. such as the difference between ArrayList and LinkedList. In ArrayList, a random access element is an operation that takes a fixed amount of time. For LinkedList, however, the randomly selected element needs to be moved in the list. The price is high, and the closer to the end of the column, the longer the search takes.

13, parameterized type. The familiar paradigm.

New Arraylist<shape> ();//the compiler will automatically check if the incoming parameters meet the requirements.

14, the creation of objects and the life cycle. C + + thinks efficiency control is the most important issue, so it gives programmers the power to choose. In pursuit of maximum execution speed. The storage space and life cycle of an object can be determined at the time the program is written, which can be achieved by placing the object in a stack or restricted domain variable or in a static storage area. This approach places storage allocation and deallocation at a high priority, and in some cases such control is valuable. However, this also sacrifices flexibility. Because the exact number, life cycle, and type of the object must be known at the time of writing. The second approach is to create objects dynamically in the heap's memory pool. In this way, it is not until the runtime knows how many objects are needed, their life cycles, and their specific types. That way, because storage space is managed dynamically at runtime, it takes a lot of time to allocate storage space in the heap, which may be much larger than the time it takes to create storage space on the stack. Creating and freeing storage space in the stack requires only one assembly instruction, which moves down or up the top of the stack, respectively. The time to create the heap storage space depends on the design of the storage mechanism.

Java uses dynamic memory allocation in its full form. Whenever you want to create a new object, use the New keyword to construct a dynamic instance of this object.

The life cycle of the object. For languages that allow objects to be created on the stack, the compiler can determine when an object survives and can automatically destroy it. However, if you create an object on a heap, the compiler knows nothing about its life cycle. Java provides a garbage collector. It can automatically discover when an object is no longer in use and then destroy it, unlike C + +, which must manage memory manually.

15, exception handling. For programmers to dbug more conveniently. Keep the program from crashing, and you can continue running.

16, concurrent programming. Handle multiple tasks at the same time. In general, only a different processor is assigned, which is really parallel execution. In the language, multithreading allows programmers to not care about whether the machine is multiple processors or only one processor.

Java Programming Ideas (I. Introduction to Objects)

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.