Java sixth week after-school report

1, run the following code, the results

public class ceshi {
public static void main(String[]args) {
    Foo obj1=new Foo();
    Foo obj2=new Foo();
    System.out.println(obj1==obj2);
}
}
class Foo{
    int value=100;
}

 

 

 These numbers are not comparable values ​​of two variables, but rather address two variables, so you have no right results.

2 run the following code, ask the wrong reasons

 

 

We are easy to get, the class has been initialized, and then initialize the main function it will be an error.

3, class initialization rules

Initialization occurs when the definition, initialization module, class initialization, all subject to the order, i.e., who last initialized, who call the shots,

That coverage criteria.

4

package teacher;

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

}

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

    }
}

Results are as follows

 

Guess you like

Origin www.cnblogs.com/520520520zl/p/11695026.html