Data interaction between different java objects (common) and java Interaction

Source: Internet
Author: User

Data interaction between different java objects (common) and java Interaction

Everything in java is an object. Whether you are a thread, asynchronous task, or... You can use the following three common methods to exchange data between objects. Of course, android has its own unique data interaction method compared with java. These will be discussed in later articles. This article aims to summarize the data interaction between common java objects.

1. pass through Constructor

The struct constructor is very convenient for parameter passing, so that some attributes of the object are initialized when the object is created. We often need to reload the constructor of classes according to our own needs. When an object is created, we can pass the required data to it to realize data interaction between different objects.

2. getXXX and setXXX Methods

Based on the encapsulation and security considerations in java object-oriented programming, we often encapsulate the member variables in the class (that is, modify them using private ), this improves data security. What should we do when we want to operate these member variables? We can use two methods. The first method is the public Constructor (or constructor), that is, the content described in method 1. when an object is instantiated, the variable is assigned a value. The second method is through the set and get methods.

When writing a pojo class, we usually set the get and set methods for the member variables.

In fact, the application scenarios of get and set are not limited, so our thinking cannot be limited. In android, if the activity wants to get handler from the sub-thread, we can write a getHandler Method to the sub-thread, and then call the getHander method with the object of the sub-thread in the activity, you can get the handler of the sub-thread.

Callback callback. When a checkbox is encountered in listview, we will create a map in the adapter to store the status of the checkbox. In the activity, in the listener method of all selected and canceled buttons, we need to get the map of the adapter and then update the map. Solution: Write A getMap method in the adapter.

Iii. Interface callback

This part of the previous blog has been described in detail: http://blog.csdn.net/a910626/article/details/45920893

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.