Thinking question 1
In the early days we often defined variables like this
int value=100;
The preceding example defines the variable
Myclass.obj=new MyClass ();
Are the variables defined in these two ways the same?
Not the same. The former defines the original data type variable, which opens up space for the variable when the original data variable is defined, which is the variable that defines the object and uses the construction method to open up the memory space for it.
Think question 2:
For raw data types, you can use "= =" directly to determine whether the values of two variables are equal.
Object variables can also use "= =" to determine whether the values of two variables are equal?
No.
When "= =" is applied to two primitive data type variables, the values of the two variables are compared for equality. When applied to a reference type variable, compares whether the two variables refer to the same object. That is, compare the two reference type variables to save the same object address.
In Java, to compare the field values of two objects, you can override the Equals () method of the base class, and only the Equals method that overrides object if the parameter type is object. , the Equals method is overload when the parameter type is another class. The code is as follows:
public class ObjectEquals {
public static void Main (string[] args) {
Mytestclass obj1=new Mytestclass (100);
Mytestclass obj2=new Mytestclass (100);
System.out.println (OBJ1==OBJ2);
System.out.println (Obj1.equals (OBJ2));
}
}
Class Mytestclass
{
public int Value;
Note: Only the parameter type object is the Equals method that overrides the object
The parameter type is Mytestclass, only the Equals method is overload.
@Override
public boolean equals (Object obj)
// {
Return ((mytestclass) obj). Value==this. Value;
// }
public boolean equals (Mytestclass obj)
{
return obj. Value==this. Value;
}
Public mytestclass (int initvalue)
{
Value=initvalue;
}
}
Think question 3:
Please summarize, what is the difference between this method? Can you list a few?
1) The method name is the same as the class name;
2) The first first letter of the method name is capitalized;
Think question 4:
Why is the above code not compiled? Where did it go wrong?
A parameter was missing when referencing the construction method new Foo ().
If a custom construction method is provided in a class, the default construction method is no longer provided by the system.
Think question 5:
If a class has both an initialization block and a constructor, and it also sets the initial value of the field, who decides?
If there are static member assignments or static initialization blocks in the parent class, perform static member assignments or static initialization blocks. If there are static member assignments or static initialization blocks in the class, perform static member assignments or static initialization blocks to assign the members of the class to the initial values. The initialization blocks of the class do not receive any arguments, and they are executed whenever an object of the class is created. Executes the default value specified by the class member definition or the initialization block of the class, exactly which one you want to see in front of. If there is a construction method, the construction method will prevail.
Instance
Executes the example and outputs the result as
Summarize the rules of Java initialization output field according to the output result
1. Execute the default value specified when the class member is defined or the initialization module of the class, exactly which one you want to see "in front", and execute the following.
2, if there is a construction method, then the construction method shall prevail.
3, class initialization block does not receive any parameters, and as long as the creation of the object of the class, they will be executed.
Think question 6:
Run the sample, observe the results, and summarize the execution order of the static initialization blocks:
Class Root
{
static{
SYSTEM.OUT.PRINTLN ("Static initialization block of Root");
}
{
System.out.println ("Root's normal initialization block");
}
Public Root ()
{
SYSTEM.OUT.PRINTLN ("Root parameter-free constructor");
}
}
Class Mid extends Root
{
static{
SYSTEM.OUT.PRINTLN ("Static initialization block of mid");
}
{
SYSTEM.OUT.PRINTLN ("Normal initialization block for mid");
}
Public Mid ()
{
SYSTEM.OUT.PRINTLN ("Non-parametric constructor for mid");
}
Public Mid (String msg)
{
Calling overloaded constructors in the same class through this
This ();
System.out.println ("Mid with parametric constructor, its parameter value:" + msg);
}
}
Class Leaf extends Mid
{
static{
SYSTEM.OUT.PRINTLN ("Static initialization block of the Leaf");
}
{
SYSTEM.OUT.PRINTLN ("ordinary initialization block of the Leaf");
}
Public Leaf ()
{
A constructor that invokes a string argument in the parent class through Super
Super ("Java Initialization sequence demo");
System.out.println ("The constructor that executes the leaf");
}
}
public class Teststaticinitializeblock
{
public static void Main (string[] args)
{
New Leaf ();
}
}
Operation Result:
1. Static initialization blocks are executed only once
2. When creating objects of subtypes, it also causes static initialization blocks of the parent type to execute
3, super () can access the initialization method in the parent class. With static initialization blocks, the static initialization blocks are executed from the parent class, the static initialization blocks of the subclasses are executed, and then the normal initialization blocks of the parent class are executed, the original constructors, the overloaded constructors, and the subclasses of the same kind.
Think question 7:
Only static data is allowed to be accessed in a static method. So how do you access an instance member of a class in a static method (that is, a field or method that has no static keyword attached)?
The specific code for accessing instance members by parameters is as follows:
public class Test2 {
public static void Main (string[] args)
{
Test t=new test ();
int A=t.getyear ();
T.INPUT1 ();
Test.input (a);
T.input (a);
}
}
Class Test
{
int year = 100;
public int getyear ()
{
Return year;
}
public static void input (int a)
{
System.out.println ("Static" +a);
}
public void Input1 ()
{
SYSTEM.OUT.PRINTLN ("example");
}
}
Run results
Think question 8:
Two integers exactly the same, why an output
True, one output false?
Called the Java.lang.String class, contains the value of the string and the implementation is special, you can not apply the construction method, you can directly obtain a string object.
Title: You can track the number of objects created by a class by using the class's static fields and constructors. Please write a class that can be queried at any time "how many objects have you created?" ”
Package text;
class Number
{
Static int I= 0;
Public Number ()
{
i+ +;
}
Public void out ()
{
System. out. println ("You've created a" +I+ "Object!) ");
}
}
Public class Text {
Public Static void Main (string[] args) {
TODO auto-generated Method stub
Number n1=new number ();
N1.out ();
Number n2=new number ();
N2.out ();
Number n3=new number ();
N3.out ();
}
}
Java job 04 classes and objects