A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Angel♥儱唲 中级黑马   /  2013-7-24 21:43  /  1054 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

谁能给点多态方面的练习题呀, 最好是带答案的

1 个回复

倒序浏览
     学校简单工厂
/**
* 父类
* @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();
       
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马