10.30

package joker;

class Single{
    private static Single instance = new Single();
    private Single() {
    }
    public static Single getInstance() {
        return instance;
    }
    public void print() {
        System.out.println("Hello World!");
    }
}
class Human {
    static String name;
    private static int count;
    public Human() {
        count++;
        this.name = "nobody-" + count;
        System.out.println("产生了" + count + "个实例!");
    }
    public Human(String name) {
        this.name = name;
    }
    public String getInfo() {
        return "姓名:" + this.name;
    }
}

public class HumanDemo{
    public static void main(String[] args){
        System.out.println(new Human().getInfo());
        System.out.println(new Human("K").getInfo());
        System.out.println(new Human("Q").getInfo());
        System.out.println(new Human().getInfo());
        new Human();
        new Human();
        new Human();
        new Human();
        Human h1 = new Human("张三");
        Human h2 = new Human("李四");
        Human h3 = new Human("王五");
        System.out.println("修改之前:" + h1.getInfo() + " " + h2.getInfo() + " " + h3.getInfo());
        Human.name = "赵六";
        System.out.println("修改之后:" + h1.getInfo() + " " + h2.getInfo() + " " + h3.getInfo());
        Single a = null;
        a = Single.getInstance();
        a.print();
    }
}
posted @ 2017-11-12 20:32  ELF_CH  阅读(158)  评论(0编辑  收藏  举报