Thinking in Java Learn (6)

xiaoxiao2021-03-06  94

Chapter 4.Initative and Clean I Personally understand the initialization and cleanup relationship is to eat and go to the toilet, which is an object of initialization, we can use cleanup. But why should you clean up? Imagine a person who is not pulling, what is it? :) Do we have to generate an object like eating, not to clean up the object that has been used? Retapped by our objects such as mountains, occupying system resources?

??

????? Ensure initialization in the constructor

??????? What constructor? The constructor is a special function. When the object is generated, he will be called automatically by the system, and the name of this function is the same as the Class name.

??????

Class constructortest ?????? {??????? constructortest () ?? // The constructor is not returned, notice that it is not void! ??????? {???????? system.out.println ("nothing!"); ???????} ??????? casetructortest (string s) ?? ????? {???????? system.out.println (s); ???????} ??????? public static void main (String [] args) ?? ????? {??????? new constructortest (); // When the object is generated, the system will automatically call his constructor ???????? New Constructortest ("Hello" ); / / And constructor can also accept parameters to generate a specific object you want. If constructortest is a unique constructor, the compiler does not allow you to generate an object in any other way ???????} ??????} ????? It is to ask here, why do my programs do not define constructor at all, but why can I still use it when you create an object?

??????E.g

?????

Class Test ?????? {??????? public static void main (string args []) ???????? new test (); // Did not define any constructor, but can be used, the reason is that when you do not define any constructor, the system will automatically define a default constructor that does not accept the parameters ???????} ????? Although the system will help you define a simpler, there is no parameter constructor, but you must remember: If you define the constructor, then the system will not help you. Generate a default constructor, you also want to invoke this default constructor, you can only call your own defined constructor!

????????

Overloading ????????? In fact, I personally unclear and overwritten the name, not I don't understand the difference between them, but I think they The name is too similar, it is easy to confuse, huh, we use the shortest words to describe overload ------ the same name, different quotes.

????????? Mono, we all like to play CS, we will say: "Everyone rushes a door, kill them." What does this "kill" mean? Of course, it is not "kill" in real life, but the definition in the game. At this time, you can see "kill" as a function, for example

??????????

Class Person ????????? {??????????} ????????? Class CS ????????? {??????? ??} ????????? class man ????????? {?????????? public void kill (PERSON P) ????????? ? {??????????? system.out.println (p "was killed!"); ???????????? public void Kill (cs c) ?????????????? system.out.println (C "WAS Killed"); ?????????? } ?????????? public static void main (String [] args) ?????????????? Person P = New Person () ; ??????????? cs c = new cs (); ??????????? man m = new man (); ??????????? M.kill (p); ??????????? M.kill (c); ???????????} ????????} ???? ???? Here we define two "kill" methods, one is killing real person, one is a person who kills CS, we call 'kill in the game', I estimate that no one will think You have to kill the real person, everyone knows that you are to kill the characters in the game. If you say, I have to kill the way with the way to kill the characters in CS. It is estimated that people will think that you are neuropathy. It is because of the words, even if you say a few words, it doesn't matter, because our artificial intelligence will help us analyze, but the computer will not be like this, you must assign a specific function to the compiler, so we use the same name. However, the function of different quotes, you only need to pass the corresponding object when the function is called, not to write a function of a lot of names that are different, call, such a carefully selected name to help you and Some people write, read, analyze the program, so that the same function has a variety of different meaning ????????? In Java, the constructor must be overloaded (that is, the system automatically generates the default constructor) If you want to produce different objects according to your own wishes, the overload constructor is essential.

??????????

Class Person ????????? {?????????? String sex = "man"; ?????????? Person () ??????? ?? {??????????? system.out.println ("this is a" sex); ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Person (String S) ????????????? SEX = S; ??????????? system.out.println ("this Is A " sex); ????????????????plic? static void main (string args []) ?????????? { ??????????? new persons (); ??????????? New Person ("Woman"); ??????????} ???? ?????} ????????? Here we define two constructor, through these two overloaded constructor, we can produce different Person objects, a man, a woman

????????

Distinguish the overload function ????????? The overloaded function has the same name, then how can the compiler know that you call? In fact, it is very simple, smart people estimate that each of the heavy-duty functions has a unique quotes, even if the order of the quotes is different, it is enough to distinguish the same overload function as the same name (but not recommended, because This will make your program difficult to read and maintain). ???????

With the basic type to overload ???????? Since the basic type can be automatically converted from a smaller type to a large type, it is easy to confuse when using the overload mechanism, that is, when you have Several functions are Void Test (INT i) ?? Void Test (Short S), if you call Test (3), you will find that 3 is considered int, and A function that can accept the INT quoter will be evokeed, and if you only have a function of Void Test (Double D), you will find that INT 3 automatically gets improvement, changing the value of Double, and when you pass the value of the function is greater than When the number of functions, you must use forced conversion to call

??????????

Class myclass ????????? {?????????? static int i = 100; ?????????? Void Test (double d) ?????? ???? {??????????? system.out.println (d); ???????????? void test (char c) ?????????? {??????????? system.out.println (c); ??????????} ?????? ???? public? static void main (string [] args ????????????? aclass p = new myclass (); ????? ?????? p.Test (i); / / The quoter is greater than the transferred value, automatic conversion, output 100.0 ??????????? p.test ((char) i);? / / The quoter is less than the value passed, forced conversion, output D ??????????} ???????

Baseline with return value

??????? Note that the return value of the function in Java is one of the heavy-duty standards.

???????

DEFAULT constructor

??????? Default constructor is a constructor without any quotient, and he is automatically generated by the system when you do not define any constructor (whether there is a quoter), if you have already I define the constructor, then the system will not help you generate the constructor, you can't call them.

???????

Keyword this ??????? THIS mainly:

???????

1. Use in the function, what he represented is the handle (Object? Reference) that calls this function

??????????? E.g

Class test ?????????????????? Void method1 () ???????????? {???? ?????????? system.out.println ("method1"); ??????????????????? Void Method2 () ????????????? {?????????????? system.out.println ("Method2"); ????????? ????} ????????????? void method3 () ????????????? {?????????????? THIS.METHOD1 (); // Call another function in the same class in the function, actually there is no need to use this, directly call it ?????????????? THITHOD2 () ; // The compiler will sneak into this THIS handle ?????????????? system.out.println ("Method3"); ?????????? ???} ????????????? public static void main (String [] args) ????????????? {????????? ????? test t = new test (); ??????????????? t.method3 (); ?????????????}??? ??????????????????????? We all know that most of the function calls in the object-oriented language is to call the same object, then this this Is it dry? In fact, this this this is the handle of the current object. You can take the same way of handling the same way as the object handle, for example ?????????????

Class test ????????????? {?????????????? INT i = 10; ?????????????? VoID Method1 () ?????????????????????????????????????????????????????????????????????????????????? ??? system.out.println (i); // This is a local variable 20 ??????????????? system.out.println (this.i); // Print global variable 10 ???????????????? THIS.I = I; // Here the local variable value to the global variable ???????????? ??? system.out.println (this.i); // Sprinkling the global variable after changing the change 20 ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???? public static void main (string args [] ??????????????????????? Test TEST T = New Test ); ???????????????? t.method1 (); ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?} ??????????? you can think of this.i here as Test.i (just an example, if you write, you can't compile it!) So you will I can understand that I have the meaning of "You can take the same way of handling the same way as the object handle".

??????????? There is also a situation that needs to be clearly pointed out that this keyword is that when you have to clearly point out who is currently the current object handle:

?????????????

Class test ????????????? {????????????? INT I = 0; ?????????????? public TEST Go () ?????????????? {???????????????i i ; ?????????????? Return this; ??????????????} ?????????????? public void show () ???????????? ?? {??????????????? system.out.println (i); ??????????????} ???????? ?????? public static void main (string args []) ??????????????????????? new test () .go (). Go (). Go (). show (); ??????????????????????????????????????????????????????????????????????? ???????? Due to Go (returns the current object by keyword This, we can easily perform multiple operations multiple times in the same object. ????????

2, call constructor in the constructor

???????? Maybe you have written a class, there are multiple constructors, or there are many things in the middle, then we can construct a recurring code to avoid writing repeated code:

????????????

Class Person ???????????? {???????????????????? {????? ????????? system.out.println ("speak Chinese"); ???????????????????????? String s) ????????????? {??????????????????????? THIS (); // This is called the Person () constructor ???? ?????????? system.out.println ("speak english"); ???????????????????????? (INT i) ????????????? {?????????????????????????? (); this is an error! Because you can only call a constructor in THIS! In addition, the statement that calls the constructor must be placed in the first statement in constructor ?????????????????; // This is called the Person (String S) constructor ?? ???????????? system.out.println ("we have" i "Persons !!!"); ??????????????} ???? ????????? void show () ????????????? {?????????????? // this (); compiler does not Allow you to call constructor within any function other than constructor ???????????????????????????? Public static void main (String Args [] ) ????????????? {?????????????????????? Person P = new person (10); ???????????? ?} ????????????}

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

New Post(0)