class C1
{
    private int num;
    public C1(int num)
    {
        this.num=num;
    }
}
public class Text_001
{
    public static void main(String[] args)
    {
        C1 t1=new C1(100);
        C1 t2=new C1(100);
        System.out.println(t1==t2);
        C1 t3=t1;
        System.out.println(t1==t3);
        t2=t1;
        System.out.println(t1==t2);
    }
}

结果:

false
true
true

对象在定义的时候调用了没有参数的构造函数,但是该类并不含有对应的构造函数

public class InitializeBlockDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        InitializeBlockClass obj=new InitializeBlockClass();
        System.out.println(obj.field);
        
        obj=new InitializeBlockClass(300);
        System.out.println(obj.field);
    }

}

class InitializeBlockClass{
    //下面这句在初始化块之前与之后,会影响到field字段的初始值
    //public int field=100;
    
    {
        field=200;
    }
    public int field=100;
    public InitializeBlockClass(int value){
        this.field=value;
    }
    public InitializeBlockClass(){
        
    }
}

结果:

100

300

在同时有初始化块和构造方法的情况下,构造方法优先。

public class Text_002
{

    public static void main(String[] args)
    {
        int i=100;
        C1 t1=new C1();
        t1.setNum(i);//静态方法访问没有附加static的变量
    }

}
class C1
{
    private int num;
    public void setNum(int num)
    {
        this.num=num;
    }
}
public class HowManyClassYouHaveCreated
{

    public static void main(String[] args)
    {
        int f=1;
        Hello h1=new Hello();
        Hello h2=new Hello();
        Hello h3=new Hello();
        Hello h4=new Hello();
        Hello h5=new Hello();
        Hello h6=new Hello();
        Hello h7=new Hello();
        System.out.println("HowManyClassYouHaveCreated?");
        System.out.println(h7.getNum());
        
    }

}
class Hello
{
    static private int num=0;
    public Hello()
    {
        System.out.println("Hello");
        num++;
    }
    public int getNum()
    {
        return num;
    }
}

 

 posted on 2018-10-21 10:08  Aurinko  阅读(166)  评论(0编辑  收藏  举报