My thinking in java learning notes (six)

xiaoxiao2021-03-06  105

Chapter 4 Initializing and Cleaning I Personally understanding the initialization and cleaning relationship is to eat and go to the toilet, which is an initialized object, 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? What is the constructor to ensure initialization? 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 () // Constructor is not returned, notice that it is not void! {System.out.println ("Nothing!");} Constructortest (string s) {system.out.println (s);} public static void main (string args []) {new constructortest (); // When the object When generated, the system will automatically call his constructor New Constructortest ("Hello"); / / and constructor can also accept the quotes to generate the specific object you want. If constructortest (String S) is the only constructor, the compiler does not allow you to generate objects in any other way}} Some people have to ask here, why is my program that is not defined at all defined constructor But why can I still use him when you create an object? For example, Class test {public static void main (string args [) {new test (); // does not define any constructor, but can be used, the reason is that the system will automatically be for you when you do not define any constructor Define a default constructor}}}, although the system will help you define the simplest, there is no core constructor, but you must remember: If you define the constructor, then the system will not Automatically help you generate a default constructor, you also want to invoke this default constructor, you can only call your own defined constructor! Overloading in the function, I personally make an old man unclear and overwritten, not I don't understand the difference between them, but, I think their name is too similar, it is easy to confuse, huh, huh ~ We use the shortest words to describe overload ------ The same name, the function of different quotes. For a small example, we all like to play CS very much, we will say: Everyone rushes a door, killing 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, such as 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);}} We define the method of 2 'kill', one is killing people, one is killing CS People, we call 'kill in the game', I estimate that no one will think 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 is necessary to be overloaded (that is, the system automatically generates the default constructor), if you want to come according to your own wishes Producing different objects, the heavy-duty constructor is a unpleasant 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);} public static void main (string args []) {new person (); new person ("Woman");}} Here us Two constructors are defined, through these two overloaded constructor, we can produce different Person objects, a man, a woman distinguishes the overload function overload function with the same name, then how can the compiler know What are you calling? 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 basic types to overload because the basic type can be automatically converted to a larger type, so it is easy to confuse when using the overload mechanism, that is, When you have several functions, Void Test (INT i) Void Test (Byte S), if you call Test (3), you will find that 3 is considered int, and The function that accepts the INT quotes 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 function of the function is greater than the function When you quote, 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 []) {myclass p = new myclass (); p.Test (i); / / quota greater than the transferred value, automatic conversion, output 100.0 p.Test ((char) i) / / The quoter is less than the passing value, forced conversion, output D}} is taken as a reference to the return value, and the Return value in Java cannot be used as one of the DEFAULT constructive function of the heavy-duty standard is a kind. Without any quotient constructor, and he is automatically generated by the system when you don't define any constructor (whether there is a quoter), if you have defined the constructor, then the system will not help you To generate a constructor, you can't call their keyword this this. The role of this this this is mainly 1. In the function, what he represents is the handle of this function (Object Reference), such as Class Test {Void Method1 () { System.out.println ("Method1");} void method2 () {system.out.println ("method2");} void method3 () {this.method1 (); // Call the same class in the function Another function, actually there is no need to use this, directly call this.method2 (); // The compiler will secretly pass the SYSTEM.Println ("Method 3") of this THIS ("Method 3");} public static void Main (String Args []) {Test T = New Test (); T.Method3 ();}} We all know that in object-oriented languages, most of the function calls are called with objects, then this this is doing? In fact, this this is the handle of the current object. You can take the same processing like the object handle to handle this this, such as Class Test {INT I = 10; void method1 () {INT i = 20; system.out.println i); // This is printed here that local variables 20 system.out.println (this.i); // That is printed here; // Here, the value of local variables is assigned to the global variable SYSTEM .out.println (this.i);

// Here, the changed global variable 20} public static void main (string args []) {test t = new test (); t.method1 ();} You can think of this. Test.i (just an example, if you write, you can't compile it), so you can understand that "you can take the same way to handle the same way as the object handle" this sentence. Meaning! There is also a situation that it is also necessary to clearly point out the keyword, that is, when you must clearly point out who is currently the current object handle, Class test {INT i = 0; public test go () {i ; returnid;} 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, so we can easily perform multiple operations with an object 2, call the constructor in the constructor Maybe you write, there are multiple constructors, intertwined them There are many things in the middle, then we can construct Class Person {Person ("Speak Chinese) in order to avoid writing repetitive code;" Speak Chinese ");} / Here, it is the persons () constructor system.out.println ("speak english");} Person (INT i) {// this (); this is an error! Because you can call a constructor by this! , Call the statement of the constructor You must put in First Statement in Constructor this ("man"); // This is called the Person (String S) constructor System.out.Println ("We Have" i "Persons !!!");} void show () {// THIS ();

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

New Post(0)