Java作业八(2017-10-30)

public class TAutoPerson {
public static void main(String args[]) {
new Person();
new Person();
new Person();
new Person();
new Person(); }
}
class Person{
private String name;
private static int count;
public Person() {
count++;
System.out.println("产生了" + count +"个实例化对象。 ");
}
public String getInfo() {
return "姓名: " + this.name;
} }

Java作业八(2017-10-30)

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

Java作业八(2017-10-30)

public class TAutoPerson {
public static void main(String args[]) {
Single s = null;
s = Single.getInstance();
s.print();
} }
class Single{
private static Single instance = new Single();
private Single() { }
public static Single getInstance() {
return instance;
}
public void print() {
System.out.println("hello world!!!");
}
}

  Java作业八(2017-10-30)

public class TAutoPerson {
public static void main(String[] args) {
Persona per1 = new Persona("张三",30);
Persona per2 = new Persona("李四",31);
Persona per3 = new Persona("张五",32);
System.out.println("---------------------信息修改之前-----------------------------");
System.out.println(per1.getInfo());
System.out.println(per1.getInfo());
System.out.println(per1.getInfo());
System.out.println("---------------------信息修改之后-----------------------------");
Persona.city = "B城";
System.out.println(per1.getInfo());
System.out.println(per1.getInfo());
System.out.println(per1.getInfo());
} }
class Persona{
private String name;
private int age;
static String city = "A城";
public Persona(String name,int age) {
this.name = name;
this.age = age;
}
public String getInfo() {
return " 姓名:"+ this.name + ",年龄:" + this.age + ", 城市: "+city;
}
}

  Java作业八(2017-10-30)

public class TAutoPerson {
public static void main(String[] args) {
TAutoPerson s=TAutoPerson.getSinglePerson();
System.out.println(s.toString());//得到唯一的对象
} private String name="The God";
private int age=10000;
private static TAutoPerson person=new TAutoPerson();//生成对象放在java静态池中,否则如果非静态在堆中,生命周期比类短
private TAutoPerson() {};//构造方法私有化可以避免大量实例化对象而消耗内存
public static TAutoPerson getSinglePerson()
return person;
}
@Override
public String toString() {
return "SinglePerson [name=" + name + ", age=" + age + "]";
}
}

  Java作业八(2017-10-30)

上一篇:SERVICE_USE_PID


下一篇:VB的判断语句和循环语句