Chapter 4 initialization and cleanup
I personally understand that the relationship between initialization and cleanup is eating and going to the toilet. It is the object with initialization that we can use cleanup. But why? Imagine what a person just eats and does not pull? :) Does our program need to generate objects just like eating, instead of clearing the used objects? Let our objects pile up to occupy system resources?
Ensure initialization by using Constructors
What constructor? A constructor is a special function. when an object is generated, it is automatically called by the system and the function name is the same as the class name.
Class constructortest
{
Constructortest () // constructor does not return values. Note 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 an object is generated, the system automatically calls its constructor.
New constructortest ("hello"); // The constructor can also accept the number of arguments to generate the desired specific object. If constructortest (string S) is the only constructor, the compiler does not allow you to generate objects in any other way.
}
}
Some people will ask why my program has never defined constructor, but why can I still use it when creating objects?
For example
Class Test
{
Public static void main (string ARGs [])
{
New Test (); // No constructor is defined, but can be used because, when you do not define any constructor, the system automatically defines a default constructor that does not accept arguments.
}
}
Although the system will help you define a simplest constructor without arguments, you must remember that if you define a constructor, the system will not automatically generate a default constructor for you. You can only call your own constructor!
Overloading)
In fact, I personally cannot understand the names of the reload and overwrite statements. I don't understand the differences between them, but I think their names are too similar and easy to confuse ~ We use the shortest sentence to describe the overload-functions with the same name and different arguments.
For example, we all like playing CS. We will say: Everyone rushed to door A and killed them. What does this "kill" mean? Of course, it is not a real-life killer, but a definition in the game. In this case, you can use 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 'ky' method in 2. One is to kill real people, the other is to kill people in CS, And we shout 'ky' in the game ', I guess no one will think of you killing real people. Everyone knows that you are a character in the game. If you say, I will use the method of killing the characters in Cs to kill the bandits. It is estimated that people will think of you as a mental illness. It is because it doesn't matter if people say less than a few words, because our AI will help us analyze, but the computer won't. You must assign a specific function to the compiler, so we use the same name, but for functions with different numbers, you only need to pass in the corresponding object when calling the function, instead of writing a bunch of functions with different names to call them separately, such a well-selected name can help you write, read, and analyze programs for yourself and others, make the same function have different meanings
In Java, constructor must be overloaded (that is, the reason why the system automatically generates the default constructor). If you want to generate different objects as needed
Then, the overload constructor is closed.
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 we have defined two constructors. Through these two overloaded constructors, we can generate different person objects, a man and a woman.
Distinguish between overloaded functions
If the overloaded function has the same name, how can the compiler know which one you call? In fact, it is very simple. Wise people have already seen that every overloaded function has a unique number of arguments, even if the order of arguments is different, it is also sufficient to distinguish several overload functions with the same name (but not recommended, because this will make your program difficult to read and maintain)
Reload with basic types
Because basic types can be automatically converted from smaller types to larger types, obfuscation may occur when the overload mechanism is used, that is to say, when you have several functions void test (int I) void test (byte B) void test (short S), if you call test (3, you will find that 3 is regarded as an int, and the function that can accept the int argument is aroused. If you only have void test (double D, you will find that INT 3 is automatically upgraded to the value of double, and when the value of the passed function is greater than the value of the function, you must use a 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); // The number of arguments is greater than the passed value. The value is automatically converted and 100.0 is output.
P. Test (char) I); // The number of arguments is less than the passed value, which is forced conversion and Output D.
}
}
Use the return value as the benchmark for overloading
Note: in Java, the return value of a function cannot be used as one of the reload criteria.
Default constructor
A default constructor is a constructor that does not contain any arguments and does not define any Constructor (whether or not there are arguments, automatically generated by the system. If you have defined your own constructor, the system will not help you generate constructor, and you cannot call them.
Keyword: This
The main functions of this are:
1. When used in a function, it indicates the handle (Object Reference) that calls this function)
For example, 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. In fact, there is no need to use this. Just call it directly.
This. method2 (); // The Compiler secretly imports
System. Out. println ("method3 ");
}
Public static void main (string ARGs [])
{
Test T = new test ();
T. method3 ();
}
}
We all know that in object-oriented languages, most function calls must be called with objects. So what is this? In fact, this is the handle of the current object. You can handle this in the same way as the object handle, for example
Class Test
{
Int I = 10;
Void Method1 ()
{
Int I = 20;
System. Out. println (I); // The local variable 20 is printed here.
System. Out. println (this. I); // global variable 10 printed here
This. I = I; // the value of the local variable is assigned to the global variable.
System. Out. println (this. I); // print the changed global variable 20.
}
Public static void main (string ARGs [])
{
Test T = new test ();
T. Method1 ();
}
}
You can set this. I think it is test. I (for example, if you write it like this, it cannot be compiled ), in this way, you can understand the meaning of this sentence, "you can handle this object in the same way as the object handle!
There is also a case where you need to explicitly specify the this keyword, that is, when you must explicitly specify who the current object handle is
Class Test
{
Int I = 0;
Public test go ()
{
I ++;
Return this;
}
Public void show ()
{
System. Out. println (I );
}
Public static void main (string ARGs [])
{
New Test (). Go (). Show ();
}
}
Since go (returns the current object through the keyword this, we can easily perform multiple operations on an object.
2. Call the constructor In the constructor.
Maybe there are multiple constructors in the class you write, maybe there are many similarities among them. To avoid repeated code writing, we can construct
Class person
{
Person ()
{
System. Out. println ("Speak Chinese ");
}
Person (string S)
{
This (); // The person () constructor is called here.
System. Out. println ("Speak English ");
}
Person (int I)
{
// This (); this is an error! Because only one constructor can be called by this! In addition, the statements that call the constructor must be placed in first statement in constructor.
This ("man"); // The person (string s) constructor is called here.
System. Out. println ("we have" + I + "persons !!! ");
}
Void show ()
{
// This (); the compiler does not allow you to call constructors in any function other than the constructor.
}
Public static void main (string ARGs [])
{
Person P = new person (10 );
}
}
Significance of static
After learning this in our system, we should be able to fully understand what static functions are. He means: for this function that has been declared as static, you cannot directly call a non-static function in the static function, because it does not have the this, only objects can be used to call non-static functions. However, we can also use the class itself to call its own static function without passing through any object. With static, you can imagine that the static function is a global function in Java, which can provide access to other static functions and static data members. But please do not use a lot of static functions and variables, because it makes people feel like they are not OOP languages (because OOP languages use objects to call functions)
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.