10.14 hands-on brain

1. Why is the following code not compile? Where is wrong?

 

 

Le Foo species own definition of a constructor can not reference system comes constructor;

 

 

2. Run TestStaticInitializeBlock.java example, look at the output, concludes that "static initialization block execution order."

package dongshou;

Root class
{
static {
System.out.println ( "Root static initialization block");
}
{
System.out.println ( "normal initialization block the Root");
}
public Root ()
{
System.out.println ( " Root no argument constructor ");
}
}
class Mid the extends Root
{
static {
System.out.println (" Mid static initialization block ");
}
{
System.out.println (" Mid general initialization block ") ;
}
public Mid ()
{
System.out.println ( "Mid no argument constructor");
}
public Mid (String MSG)
{
// call the same class constructor overloads by the this
the this ();
the System .out.println ( "Mid constructor parameters with which the parameter values:" + MSG);
}
}
Leaf the extends Mid class
{
static {
System.out.println ( "Leaf static initialization block");
}
{
System.out.println ( "Leaf normal initialization block");
}
public Leaf ()
{
// call the super parent class has a constructor parameter string of
super ( "Java initialization sequence demo");
System.out.println ( "Leaf performed constructor");
}

}

public class TestStaticInitializeBlock
{
public static void main(String[] args)
{
new Leaf();

}
}

 

 

operation result

Root static initialization block
static initialization block Mid the
static initialization block Leaf of
ordinary initialization block of Root
Root no argument constructor
normal initialization block Mid the
Mid no argument constructor
Mid with constructor arguments, which parameter values: Java initialization sequence demonstrates
normal initialization block Leaf of
performing constructor Leaf

 

Order: parent class> subclass

Static initialization block> Common initialization block> no argument constructor

 

3. Use the class page on a slide defined in the following code output is what?

 

 

 

.

 

 

 Please basis of the output code, summed up the law Java field initialized itself.

The output 100

               300  

Overloads, based on the data will look to meet the requirements for operation.

 

Static fields and constructors 4. class, a class can track the number of objects created. Please write a class at any time to query it. "You have created a number of objects?."

class chunjian

{

public static cishu =1;

chuanjian ()

{

cishu ++;

}

void show ()

{

System.out.println(cishu);

}

}

 

Guess you like

Origin www.cnblogs.com/2940500426yingxin/p/11696268.html
Recommended