1.
Please enter and run the following code to get what results??
Run Result: false.
.
2. Please summarize, what are the "differences" in this approach, and how many can you list?
(1) The type of the method is not.
(2) Value does not have a type defined.
(3) No return value.
3. Why is the following code not compiled? Where did it go wrong?
If a class provides a custom construction method, the default construction method is no longer provided by the system.
4. Using the class defined on the previous page slide, what is the output of the following code?
Please summarize the rules of Java field initialization according to the output of the code.
Results: 100
300
(1) You can assign a value directly to a variable.
(2) A method can be called to assign a value to a variable
5.
Run the Teststaticinitializeblock.java sample, observe the output, and summarize the "Order of execution of static initialization blocks".
Package Dijia;
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 Qiuhe
{
public static void Main (string[] args)
{
New Leaf ();
}
}
Results:
Static initialization block of root
Static initialization block for mid
Static initialization block of the leaf
Common initialization block for root
The parameterless constructor of root
Normal initialization block for mid
Parameter-free constructors for mid
The parametric constructor for mid, with parameter values: Java Initialization sequence Demo
Ordinary initialization blocks of the leaf
The constructor that executes the leaf
Static initialization block execution order:static-> No->public of the defined type
Static initialization blocks are executed only once. When you create an object of a subtype, it also causes the execution of the static initialization block of the parent type.
6.
Static methods only allow access to static data, so how do you access the instance members of a class in a static method (that is, a field or method that does not have the static keyword attached)?
Use the dot "·" To access.
7. Using the class's static fields and constructors, we can track the number of objects created by a class. Write a class that can query it at any time "how many objects have you created?" ”。
Package Dijia;
public class D {
public static int i=0;
Public D (int a)
{
a=0;
i++;
}
public static int Y ()
{
return i;
}
public static void Main (string[] args) {
D s=new D (2);
D e=new D (4);
System.out.println (D.y ());
}
}
Results: 2
Java Classes and objects