package com.baidu.test;
public class Student {
public String name;
private Student(){
}
private static Student student;
public static Student getInstance(){
if (student==null) {
student=new Student();
}
return student;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public void printInof(){
System.out.println("the name is "+name);
}
}
package com.baidu.test;
public class Studenttest {
public static void main(String[] args) {
Student s1=Student.getInstance();
s1.setName("0532");
Student s2=Student.getInstance();
s2.setName("0536");
s1.printInof();
s2.printInof();
if (s1==s2) {
System.out.println("创建的只有一个实例");
}else {
System.out.println("创建的不是一个实例");
}
}
}
public class Person {
public Print creatPrint(){
Properties properties=new Properties();
String s="";
try {
properties.load(new FileInputStream("config.properties"));
s = properties.getProperty("print");
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
Print print=null;
if (s.equals("hpp")) {
print=new Hpprint();
}else if (s.equals("apple")) {
print=new ApplePrint();
}
return print;
}