My Thinking In Java Learning Notes (6) (ZT)

xiaoxiao2021-03-06  95

??? I personally understand the initialization and cleaning relationship is to eat and go to the toilet. It 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? :) Does our procedure have to generate objects as eating as eating, not to clean up the object of use? Retapped by our objects such as mountains, occupying system resources? ??? In the constructor, ensure that the initialization is initialized ?????? What is the constructor? The constructor is a special function. When the object is generated, he will be called automatically, and this function name is the same as the Class name. ??????

Class Constructortest ?????? {??????? constructortest () // The constructor is not returned, notice that it is not a 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 the quotes to generate a particular object you want. If constructortest (String S) is the only constructor, the compiler does not allow you to generate an object in any other way ???????} ??????}

?????? Some people see that this is to ask, why do I have not defined constructor at all, but why can I still use him 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 quotes ???????} ????? ?

Although the system will help you define a simpler, there is no core constructor, but you must remember: If you define the constructor, then the system will not automatically help you generate one Default constructor, you also want to call this default constructor, you can only call your own defined constructor! ?????? Overloading ?????????? I actually I personally not engage in the name of overloading and overwritten, not I don't understand the difference between them, and Yes, I think their name is too similar, easy to confuse, huh, we use the shortest words to describe overload ------ the same name, different quotes. ????????? to give a small example, we all like to play CS very much, we will say: Everyone rushes a door, kill them. What does this "kill" mean? Of course not kill in real life, but the definition in the game. At this time, you can see the 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 the method of 2 in 'kill', one is a killing real person, one is a person who killed CS, we shout 'kill in the game, I estimate that no one will Thinking of you want to kill the real person, everyone knows that you are to kill the characters. 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 ????????? So, the heavy-duty constructor is unaffiring ????????? Class Person ????? ???? {?????????? string sex = "man"; ????????????? {???? ??????? system.out.println ("this is a" sex); ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????? {??????????? SEX = S; ??????????? system.out.println ("this is a" sex); ??????????} ?????????? public? static void main (string args []) ????????? {??????? ???? new persons (); ??????????? New Person ("Woman"); ??????????} ?????????}? ???????? Here we define 2 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) ??????? Match the basic type to overload ????????? Since the basic type can be automatically converted from a smaller type to a large type, So when using the overload mechanism, it is easy to confuse, that is, when you have several functions, Void Test (INT i) Void Test (Byte B), if you call Test (3) When you see, 3 is considered int, and the function that can accept the Int quotes will be evoked, and if you only have a Void Test (Double D) such a function, you will find that INT 3 is automatically improved. Change the value of Double, and when you pass the value of the function, you must use the 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 number of quotes is less than the value passed, forced conversion, output D ??????????} ?????????}

??????? Taking the return value as a benchmark ????????? Note that the return value of the function in Java is one of the return values ​​of the overload standard ?????? ????????? DEFAULT constructor is a constructor without any quotes, and he is automatically generated by the system when you do not define any constructor (whether there is a quoter), if You have defined the constructor yourself, then the system will not help you generate a constructor, you can't call them ???????

Keyword this ??????? THIS mainly has ???????? 1, in the function, what he represents is the handle of this function (Object Reference) ????? ????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 object handle to handle this this, 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 regard this.i here as Test.i (just an example, if you write, you can't compile it) so you can understand me. The above mentioned "You can take the same way to handle the object handle" means this sentence! ??????????? There is also a situation that needs to be clearly pointed out of the keyword, that is, when you have to clearly point out who is currently the current object handler ?????????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

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 (return to the current object via the keyword this, we can easily perform multiple operations multiple operations with an object ?????????? 2, in the constructor Medium adjustment constructor ???????????? Maybe you have written, there are multiple constructors, I am intended to have a lot of places in the middle, then we can construct in order to avoid writing duplicate code. ???????????? class person ???????????? {????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????? {?????????????? system.out.Println ("speak Chinese"); ?????????????} ??? ?????????? person (String s) ????????????? {?????????????? this (); // where The call is the persons () constructor ?????????????? system.out.println ("speak english"); ?????????????} ?? ??????????? person (int i) ????????????? {?????????????? // this (); This is a mistake! / Call here is Per SON (STRING S) Constructor ?????????????? System.out.Println ("We Have" i "Persons !!!"); ????????? ????} ????????????? void show () ????????????? {?????????????????????????? // this (); the compiler does not allow you to call constructor within any function other than constructor ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? Public static void main (string args []) ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???????????} ????????????}

??????????

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

New Post(0)