学校简单工厂
/**
* 父类
* @author Administrator
*
*/
public abstract class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person() {
super();
}
public Person(String name) {
super();
this.name = name;
}
public abstract void introduce();
}
/**
* 子类学生
* @author Administrator
*
*/
public class Student extends Person {
private float score=100;
private Student self;
public Student(float score, Student self,String name) {
super(name);
this.score = score;
this.self = self;
}
public Student() {
super();
}
public void introduce()
{
System.out.println("我叫:"+getName()+",得了"+this.score+"分。");
}
public Student getInstance()
{
Student stu=new Student();
return stu;
}
}
/**
* 子类老师
* @author Administrator
*
*/
public class Teacher extends Person {
private int yearOfTeach=2;
public Teacher(int yearOfTeach,String name) {
super(name);
this.yearOfTeach = yearOfTeach;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
public void introduce()
{
System.out.println("我叫:"+getName()+",工龄:"+yearOfTeach);
}
}
/**
* 工厂类
*
* @author Administrator
*
*/
public class PersonFactory {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
Student stu;
Teacher te;
Scanner input = new Scanner(System.in);
public Person createPerson(String type) {
if (type == "学生") {
stu = new Student(99, null, "yinjei");
stu.introduce();
return stu;
} else if (type == "老师") {
te = new Teacher(2, "陈红");
te.introduce();
return te;
} else
return null;
}
}
/**
* 测试类
* @author Administrator
*
*/
public class Test {
public static void main(String[]args)
{
PersonFactory per=new PersonFactory();
Person Student=per.createPerson("学生");
Student.introduce();
Person Teacher=per.createPerson("老师");
Teacher.introduce();
}
}
|