Introduction to Java 4android Learning Objects

Source: Internet
Author: User

Introduction to Java 4android Learning Objects

This week, we will complement the Basic java knowledge and study again.


1,Programmers must establish associations between machine Models and models that are designed to solve problems.. Establishing such associations is laborious and does not belong to the functions inherent in programming languages, making programming difficult to write and costly to maintain. At the same time, it also produces the entire "Programming Method" industry as a by-product.


2,The object has the status, behavior, and identifier.. State: each object can have internal data (it gives the State of the object); method: the behavior they produce; and each object can be distinguished from other objects, specifically, each object has a unique address in the memory.


3,Each object has an interface.. Creating abstract data types (classes) is one of the basic concepts of object-oriented programming. Class describes a set of objects with the same features (data elements) and actions (functions. The interface identifies the requests that can be sent to a specific object. In the program, you must have code that meets these requests. This is the implementation. To send a message to an object is actually to generate a request, and the request is associated with the method, it is natural to call the method.


4,Each object provides services. We need to build a rational object to solve our problems. Therefore, we need to think about how to break down the problem into objects. High cohesion is one of the basic requirements of software design, which means that all aspects of a software component are "combined.


5,Specific implementation of hiding. It is of great benefit to divide program developers into "creators" (those who create new data types) and "client programmers" (those who use data types in applications) based on their roles. The hidden part of a class means that the creator can modify it without worrying about the impact on others. The hidden part is also the fragile part of the object. It is easy to be destroyed by careless or unknown client programmers. Therefore, hiding it can reduce program bugs.

The first reason for access control is that client programmers cannot touch what they should not. The second reason is that the library designer can change the internal working mode of the class without worrying about affecting the client programmer.


6,Reuse specific implementation. Code reuse is one of the greatest advantages provided by object-oriented programming languages. Composition and aggregation ). A combination is often considered as a "has-a" (having) relationship. The difference between combination and aggregation is a headache for most people. Aggregation is more loose. It means "if a combination occurs dynamically, It is aggregation ". It may be very abstract, so I have to give an example in "Dahua. The combination is like the relationship between the wings of the geese and those of the geese. If the geese are gone, the wings of the geese will surely be gone. Aggregation is the relationship between geese and geese. A geese can be added to a group at any time and can leave. The relationship is much loose.

In addition, if you can use composite relationships, try to use composite relationships instead of inheritance.


7,Inheritance. Why do we need inheritance? Because we want to be lazy, when we implement a class and another class similar to it, we still need to redefine it, how eager we can define new classes by copying existing classes and modifying them. So there is "inheritance ". It inherits a parent class (base class) and then a subclass (export class ). Subclass inherits the parent class. Modifications to the parent class are also reflected in the Child class.

When the existing type is inherited, a new type is created. This new type not only contains all members of the existing type (although the private member is hidden and cannot be accessed), but also copies the interface of the base class. That is to say, all messages that can be sent to base class objects can be sent to export class objects. Different implementations of the export class and the base class on the interface, that is, Overriding ). If a new interface is added to the export class, the base class cannot be accessed. Although this relationship is inherited, it is not a pure replacement. It is called "is-like-a" instead of "is-".


8,Interchangeable objects with adjoint Polymorphism. When processing type hierarchies, an object is often treated as a specific type to which it belongs, but as a base class object. This allows us to write code that does not depend on a specific type. Generic ). To add a new class, you do not need to change the place where it is called. The base class is used to send messages, but different implementations can be executed based on the type of the passed object.

How can this mechanism be implemented? This leads to "pre-binding" and "post-binding". Later binding can determine the code it calls only when the program is running. (Using the virtual keyword in C ++, Which is enabled by default in Java ). The process of viewing the export class as its base class is called the upward Transformation (Upcasting)


9,Single inheritance. One of the most controversial issues of C ++ and Java is single inheritance and multi-inheritance. This problem has its own advantages, but it is recommended that if your company's code has a lot of C code before, then C ++ is better; if your project is from the beginning, we recommend that you use Java.


10,Container. In C ++, containers are part of the Standard C ++ class Library, which is often called the Standard Template Library (STL ). In Java, there are various containers that meet different needs. Before Java SE5, the container Object only had the common type: Object in Java. after entering the container, only the Object type is known, and the type disappears, so you need to explicitly perform a downward transformation on your own, that is, convert the Object to your type, but there is a problem, unless you know exactly what to put in, otherwise, it is easy to put the apple into it. In the result, you have to say that the family is pineapple and then use pineapple to transfer the family. This will lead to exceptions ..

After Java SE5, a parameterized type is added to the Java container, which is called a pattern in Java. Eg. ArrayList Shapes = new ArrayList (); In this way, the container is limited to the Shape type (of course, you know its export class is ).


11,Object creation and lifecycle. C ++ regards efficiency control as the most important topic, so it gives programmers the right to choose. In order to achieve the maximum execution speed, the object's storage space and life cycle can be determined during programming. Java uses dynamic memory allocation to dynamically create objects in Heap memory pools. Java has a garbage collection mechanism. When no one references this object, the object will be released.


12,Exception Handling: handling errors. When an exception occurs, it cannot be ignored. You must handle it accordingly. You can select further correction in it so that the program returns to the regular state again, or you can output debugging information. In C and C ++, error codes are often returned to indicate errors. However, the caller can ignore your error code... Therefore, exceptions are mandatory.


13,Concurrent Programming. Multithreading.


14,Java and Internet.


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.