Third java classes and hands-on brain Homework

1. Homework

problem:

 

 Original code:

Test.java:

 

package test3;

public class Test {

public static void main(String[] args) {
Ch c[]=new Ch[10];
for(int i=0;i<10;i++)
{
c[i]=new Ch();
System.out.println(c[i].gettime());
}
}

}

 

Ch.java

 

package test3;

public class Ch {
private static int time=0;
public Ch()
{
time++;
}
public int gettime()
{
return time;
}
}

 

Screenshot:

 

 Description:

Declared as static fields of the class can not guarantee time with the declaration of an object is changed, the constructor allow time + 1, represents the creation of an object.

 

2.

Hands-on brain:

1) Question:

 

 

When the class Foo write a constructor, the system will no longer generate a default constructor with parameters have been due Foo int initValue, but the main function is called there is no reference, so the error will occur.

 

2)

 

 

 

 

package text2;

public class test {

public static void main(String[] args) {
ob ob1=new ob();
System.out.println(ob1.value);

}

}

package text2;

public class ob {
public int value=100;
{
value=200;
}
public ob()
{
value=300;
}
}

 

Test Results:

 

 Description: constructor highest priority, followed by the initialization block, is finally defined.

 

3)

 

Original code:

package text2;

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 test
{
public static void main(String[] args)
{
new Leaf();

}
}

 

 

operation result:

 

 Explanation: The first starts to perform static initialization block from the parent class, then normal initialization block and begin from the parent class constructor.

 

4)

 

 Original code:

package text2;

public class test
{
public static void main(String[] args)
{
ob c=new ob();
c.shuchu();

}
}

 

package text2;

public class ob {
private int a=1;
private static int b=2;
public static void shuchu()
{
System.out.println("实例a="+new ob().a);
System.out.println("静态b="+b);
}
}

 

Output:

 

Guess you like

Origin www.cnblogs.com/sicilya/p/11692505.html