(构造方法私有化、this)10.29练习题

package cn.wax.www;

class Person
{
    private String name;
    private static int count;
    public Person()
    {
        count++;
        this.name="NONAME -"+count;
    }
    public Person(String name)
    {
        this.name=name;
    }
    public String getInfo()
    {
        return "姓名:"+this.name;
    }
    
}

public class PracticeA {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        System.out.println(new Person().getInfo());
        System.out.println(new Person("A").getInfo());
        System.out.println(new Person("B").getInfo());
        System.out.println(new Person().getInfo());

    }

}

package cn.wax.www;
class Perso
{
    private String name;
    private static int count;
    public Perso()
    {
        count++;
        System.out.println("产生了"+count+"个实例化对象");
    }
    public String getInfo()
    {
        return "姓名:"+this.name;
    }
}
public class PracticeB {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        new Perso();
        new Perso();
        new Perso();
        new Perso();
        new Perso();
    }

}

 

package cn.wax.www;
class Persoa
{
    private String name;
    private int age;
    static String city="A城";

public Persoa(String name,int age)
{
    this.name=name;
    this.age=age;
}

public String getInfo()
{
    return "姓名:"+this.name+",年龄:"+this.age+",城市:"+ city;
}
public static  class PracticeC {

    public static void main(String args[]) {
        // TODO 自动生成的方法存根
        Persoa pre1=new Persoa("张三",30);
        Persoa pre2=new Persoa("李四",30);
        Persoa pre3=new Persoa("王二麻子",30);
        System.out.println("---------------信息修改前---------------");
        System.out.println(pre1.getInfo());
        System.out.println(pre2.getInfo());
        System.out.println(pre3.getInfo());
        System.out.println("---------------信息修改后---------------");
        Persoa.city="B城";
        System.out.println(pre1.getInfo());
        System.out.println(pre2.getInfo());
        System.out.println(pre3.getInfo());
        

    }
}
}

 

package cn.wax.www;
class Single
{
    private static Single instance=new Single();
    private Single()
    {
    }
    public static Single getInstance()
    {
        return instance;
    }
    public void print()
    {
        System.out.println("hellow world!");
    }
}
public class PracticeD {

    public static void main(String args[]) {
        // TODO 自动生成的方法存根
        Single s=null;
        s=Single.getInstance();
        s.print();
    }

}

 

 

package cn.wax.www;

public class PracticeE {

    public static void main(String args[]) {
        // TODO 自动生成的方法存根
        for (int x=0;x<args.length;x++)
        {
            System.out.println(args[x]);
        }
    }

}

posted on 2017-11-06 13:45  老张的小迷妹儿  阅读(117)  评论(0编辑  收藏  举报

导航