黑马程序员技术交流社区

标题: 谁能给点练习题做做多态方面的 [打印本页]

作者: Angel♥儱唲    时间: 2013-7-24 21:43
标题: 谁能给点练习题做做多态方面的
谁能给点多态方面的练习题呀, 最好是带答案的
作者: yinjiek    时间: 2013-7-25 12:56
     学校简单工厂
/**
* 父类
* @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();
       
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2