Rereading Java Programming Ideas Chapter 12 I want to think

zhaozj2021-02-17  53

Rereading Java Programming Ideas Chapter 12 I want to think

I originally learned Java's textbooks is "Java Programming Thought". I feel that many of this book is very thin, very thorough, but I don't understand some knowledge points, some are not read, some feel that these knowledge points and main The content is not related. At that time, the project was tight, and the knowledge that did not understand, it was already programmed anyway. After doing 2 years of Java development, rereading this book, only the inner contact of these discovered knowledge, if the author's intention is treated, and there will be a lot less detours less.

When I re-read Chapter 12, I remembered that I have solved a lot of detours, and I have done a detailed discussion and explanation in this chapter. I will take this question. I will discuss every step. I hope that beginners will take less detours.

Question 1: When writing the program, I hope that a method has two return values, how to deal with?

Discussion 1: People who have used C language know that one of them can return by return value, and the other can return through a pointer parameter (incoming a pointer variable into the function, changing the point points to the content, achieving the purpose). Java does not have a pointer, how to achieve this? 12 chapters said very well: "In fact, the identifier of each object in Java (except for the basic number of drama types) is one of the pointers. But their use has been strictly restricted and specified ...". Can you use this to return multiple values? The answer is affirmative, the Read method of the Java.io.InputStream class is to use, see public int in (byte [] b).

Harvesting 1: The parameter transfer method of the Java method has two: 1. Press value: Java's basic data type, which uses this method; 2, according to the reference delivery, other types of objects, all pass references (handle ). There is a problem in the way to pass the reference, that is, two references points to the same object, modify an object in the function, and also modify the object outside the function, that is, the aliasing side effects are easy to cause.

Conclusion One: In some cases, you can use the side effects of the alias to reach the purpose of returning multiple returns values. Such as the Read method of the Java.io.InputStream class.

Question 2: According to the conclusions, some types of customs can be used as output parameters, the purpose of return values, but how to solve the basic data type?

Discussion 2: Since each of the basic types of data corresponds to a packaging class, can I use the corresponding packaging class to achieve my purpose? Practice has found that these packaging classes are not modified, and they are read-only. Objects of these classes, once created, can not be modified, can not be used to achieve the purpose of packaging classes. In this case, it is found that an array can achieve an array of basic type data, and as a parameter is passed to the method, the array content is changed in the method to achieve the purpose.

Harvest 2: I found that some categories are read-only, and once these objects are created, they cannot be modified; the basic data type packaging classes are read-only classes. These classes actually use the constant modes mentioned in "Java and Mode" books. Advantages of the constant mode: 1, the constant object is more easily maintained than the variable objects; 2. Thread security. Disadvantages: Once a constant object needs to be modified, you must create a new object. Some discussions about String and StringBuffer, nothing more than String is invariant, and StringBuffer does not use constant mode, essentially the discussion of constant mode. Conclusion II: For basic number of output parameters as a method of output parameters: an array of basic data types can achieve the purpose, and for the basic data type packaging class, it is not possible. (In order to create an array as an output parameter, it is only a way to achieve the purpose, it is not worth recommending.)

Question 3: Application Conclusion II. When the RMI is called, the output parameter changes when the method is called, but has not changed at the call. why?

Discussion 3: When I write an RMI service interface, I use the following method interface: Byte [] getfaxbyte (Fax Fax, int [] page), where Page is used as the output parameter, I hope to change the function when calling, the function changes Page [0] Value, after the function is called, the modified value is obtained by reference to Page. In practice, I found that RMI's service and calls on the same machine can get the correct result, and the RMI service and call are not on the same machine, the result is incorrect.

After a single step, the root cause of the problem was found. It turned out that it was not familiar with RMI, and did not understand the basis of RMI.

Harvest three: RMI is a remote method call, essentially to make a JVM can call another JVM method. The basic principle of RMI is to use Stub and Skel to disguise a remote object into a local object in its own machine. It is easy to ignore the point of the remote interface, the return parameter must implement the serializable interface; when the remote call is remotely transferred to the remote server, the remote return result serializable is transferred back to the called machine. In this case, since the parameter is not the same object, the method of the method call and the method is not the same object, and there is no side effects of the alias mentioned in one, in the RMI call, the use of output parameters will not succeed .

Conclusion 3: In RMI, the output parameters cannot be used, return return values.

Question 4: How do I return multiple parameters in RMI?

Discussion 4: I first thought that Collection, Collection can accommodate multiple objects, defined the return value as a Collection, can contain multiple objects in the Collection, which can return multiple parameters.

Conclusion 4: The purpose of returning multiple variables in the RMI call can be implemented using the Collection.

Problem 5: The object accommodated by Collection is just the object type, and the specific object information is lost. Moreover, function definitions and function calls must have a good description of the order of the objects of Collection content. That is, the coupling relationship between the function and the call is too strong. The reusability of this function is too low, and the user's user is very understanding of this function, it is easy to have errors.

Discussion 5: Conclusions The Thought of Writing Procedures is also an object-oriented idea, reading the books of design patterns and reconstruction, I feel that my idea is still some problems. A method If you need to return more than two return values, these return values ​​usually have certain correlations, there is a correlation, and a new class should be created, including the attribute that needs to return, and the newly created class As a return value. Harvest 4: In the process of solving the problem, there is a simple problem with the process-oriented ideas and object-oriented ideas. It has an experience in an aspect of OOP to help personal programming thoughts OOP transformation.

Conclusion 5: If the function needs to return multiple return values, try the new creation of Ä type to solve the problem.

Question 6: If the function requires multiple returns, each return value does not know each other, how to deal with?

Discussion 6: Does this situation happen? Reconstructing a book has a point of view, a method should only complete a function, when you encounter problems six, check whether there is a problem with the definition of the method? Then check if the definition is problematic? Check if you need to reconstruct the code.

I often encounter problems that need to return multiple return values ​​when I have just started using Java. After reading the relevant information on the reconstruction and design mode, basically use the conclusion five can be solved, and the possibility of issues six issues basically No. Although the problem, six can still be solved by conclusions, but I think it is reasonable to check if my own class is reasonable.

Harvest 5: I realized the benefits of refactivation to improve the code è, and I tried to use reconstruction in the future.

Conclusion Six: During the encoding process, some encoding problems with their own knowledge can be found. You can change a idea to check if the initial design idea is correct and use reconstruction improves code quality.

Reprint, please specify it from http://www.9cbs.net/develop/author/netauthor/xuyongshu/

Or http://www.9cbs.net/develop/my_Article.asp?author=xuyongshuo

转载请注明原文地址:https://www.9cbs.com/read-30194.html

New Post(0)