• Java基础之:OOP——多态练习题


    Java基础之:OOP——多态练习题

    应用案例1:

    编写教师类

    要求有属性“姓名name”,“年龄age”,“职称post”,“基本工资salary”

    编写业务方法, introduce(),实现输出一个教师的信息。

    编写教师类的三个子类:教授类(professor)、副教授类(vice professor)、讲师类(lecturer)。

    工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。在三个子类里面都重写父类的introduce()方法。

    定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印。

    
    

     

    package polymorphic_HomeWork;
    
    public class WorkTest_01 {
    
    	public static void main(String[] args) {
    		Teacher professor = new Professor("小范",20,"professor",4000.0);
    		Teacher viceProfessor = new ViceProfessor("小黄",18,"ViceProfessor",3000.0);
    		Teacher lecturer = new Lecturer("小雨",19,"lecturer",2000.0);
    		
    		System.out.println(professor.introduce());
    		System.out.println(viceProfessor.introduce());
    		System.out.println(lecturer.introduce());
    	}
    
    }
    
    //要求有属性“姓名name”,“年龄age”,“职称post”,“基本工资salary”,
    //编写业务方法, introduce(),实现输出一个教师的信息。
    class Teacher{
    	
    	private String name;
    	private int age;
    	private String post;
    	private double salary;
    	public Teacher(String name, int age, String post, double salary) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.post = post;
    		this.salary = salary;
    	}
    	public Teacher() {
    		super();
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public String getPost() {
    		return post;
    	}
    	public void setPost(String post) {
    		this.post = post;
    	}
    	public double getSalary() {
    		return salary;
    	}
    	public void setSalary(double salary) {
    		this.salary = salary;
    	}
    	
    	//introduce(),实现输出一个教师的信息。
    	public String introduce() {
    		return "Teacher [name=" + name + ", age=" + age + ", post=" + post ;
    	}
    	
    }
    
    //教授类(professor)
    class Professor extends Teacher{
    	
    	private double level;
    
    	public Professor(String name, int age, String post, double salary) {
    		super(name, age, post, salary);
    		this.level = 1.3;
    	}
    
    	public double getLevel() {
    		return level;
    	}
    
    	public void setLevel(double level) {
    		this.level = level;
    	}
    	
    	@Override
    	public String introduce() {
    		// TODO Auto-generated method stub
    		return "Professor  " + super.introduce() + ", salary=" + getSalary()*level + "]";
    	}
    }
    
    //副教授类(vice professor)
    class ViceProfessor extends Teacher{
    	
    	private double level;
    
    	public ViceProfessor(String name, int age, String post, double salary) {
    		super(name, age, post, salary);
    		this.level = 1.2;
    	}
    
    	public double getLevel() {
    		return level;
    	}
    
    	public void setLevel(double level) {
    		this.level = level;
    	}
    	
    	@Override
    	public String introduce() {
    		// TODO Auto-generated method stub
    		return "ViceProfessor  " + super.introduce()  + ", salary=" + getSalary()*level + "]";
    	}
    }
    
    //讲师类(lecturer)
    class Lecturer extends Teacher{
    	
    	private double level;
    
    	public Lecturer(String name, int age, String post, double salary) {
    		super(name, age, post, salary);
    		this.level = 1.1;
    	}
    
    	public double getLevel() {
    		return level;
    	}
    
    	public void setLevel(double level) {
    		this.level = level;
    	}
    	
    	@Override
    	public String introduce() {
    		// TODO Auto-generated method stub
    		return "Lecturer  " + super.introduce()  + ", salary=" + getSalary()*level + "]";
    	}
    }
    

      

    应用案例2:

    设计父类—员工类。子类:工人类(Worker),农民类(Peasant),教师类(Teacher),科学家类(Scientist),服务生类(Waiter)。

    (1) 其中工人,农民,服务生只有基本工资。

    (2) 教师除基本工资外,还有课酬(元/天)。

    3) 科学家除基本工资外,还有年终奖(bonus)。

    (4) 编写一个测试类,将各种类型的员工的全年工资打印出来。

    package polymorphic_HomeWork;
    public class WorkTest_02 {
    
    	public static void main(String[] args) {
    
    		Employee worker = new Worker("工人 小范",20,2000);
    		Employee peasant = new Peasant("农民 小范",40,1000);
    		Employee waiter = new Waiter("服务生 小范",25,3000);
    		Employee teacher = new Teacher("老师 小范",30,4000,50,200);
    		Employee Scientist = new Scientist("科学家 小范",35,3500,250000);
    		//这里创建的对象的编译类型都是Employee
    		
    //		System.out.println(worker.toString()); 
    //		System.out.println(peasant.toString()); 
    //		System.out.println(waiter.toString()); 
    //		System.out.println(teacher.toString()); 
    //		System.out.println(Scientist.toString()); 
    		
    		WorkTest_02 wt = new WorkTest_02();
    		
    		wt.showAll(Scientist);
    		wt.showAll(teacher);
    		wt.showAll(waiter);
    		wt.showAll(peasant);
    		wt.showAll(worker);
    	}
    	
    	
    	public void showAll(Employee e) {	//当有对象传入时,它的编译类型就变为了Employee。
    		//由于子类重写了父类的show方法,而这里e的运行类型是动态绑定了的。
    		//所以会自动的寻找到传入类型所对应的show方法
    		//父类的show方法,相当于让编译器可以通过编译。如果父类中没有show方法会报语法错误。
    		//因为e的编译类型为Employee.
    		e.show();
    	}
    
    }
    
    class Employee{
    	private String name;
    	private int age;
    	private double salary;
    	
    	public Employee(String name, int age, double salary) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.salary = salary;
    	}
    	public Employee() {
    		super();
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public double getSalary() {
    		return salary;
    	}
    	public void setSalary(double salary) {
    		this.salary = salary;
    	}
    
    	public void show() {	
    		System.out.println(toString());
    	}
    	
    	
    	
    	@Override
    	public String toString() {
    		return "name=" + name + ", age=" + age;
    	}
    	//提供一个方法返回基本年薪,子类可以在此基础上进行修改
    	public double yearSalary() {
    		
    		return salary * 12;
    	}
    	
    }
    
    //工人类(Worker)
    class Worker extends Employee{
    	
    	public Worker(String name, int age, double salary){
    		super(name,age,salary);
    	}
    	
    	@Override
    	public void show() {
    		System.out.println("Worker : " + super.toString() + ", salary: " + yearSalary()); 
    	}
    }
    
    //农民类(peasant)
    class Peasant extends Employee{
    	
    	public Peasant(String name, int age, double salary){
    		super(name,age,salary);
    	}
    	
    	@Override
    	public void show() {
    		System.out.println("Peasant : " + super.toString() + ", salary: " + yearSalary());
    	}
    }
    
    //服务生类(waiter)
    class Waiter extends Employee{
    	
    	public Waiter(String name, int age, double salary){
    		super(name,age,salary);
    	}
    	
    	@Override
    	public void show() {
    		System.out.println("Waiter : " + super.toString() + ", salary: " + yearSalary());
    	}
    }
    
    //教师类(teacher),教师除基本工资外,还有课酬(元/天)。
    class Teacher extends Employee{
    	
    	private double dollars;	//课酬
    	private int workDays;	//工作天数
    	
    
    	public Teacher(String name, int age, double salary, double dollars, int workDays) {
    		super(name, age, salary);
    		this.dollars = dollars;
    		this.workDays = workDays;
    	}
    	
    	
    	public int getWorkDays() {
    		return workDays;
    	}
    
    
    	public void setWorkDays(int workDays) {
    		this.workDays = workDays;
    	}
    
    
    	public double getDollars() {
    		return dollars;
    	}
    
    	public void setDollars(double dollars) {
    		this.dollars = dollars;
    	}
    
    	@Override
    	public void show() {	
    		System.out.println( "Teacher : " + super.toString() + 
    				", salary: " + (yearSalary()+dollars*workDays));
    	} 
    	
    }
    
    //科学家类(scientist),	科学家除基本工资外,还有年终奖(bonus)。
    class Scientist extends Employee{
    	
    	private double bonus;
    
    	public Scientist(String name, int age, double salary, double bonus) {
    		super(name, age, salary);
    		this.bonus = bonus;
    	}
    
    	public double getBonus() {
    		return bonus;
    	}
    
    	public void setBonus(double bonus) {
    		this.bonus = bonus;
    	}
    	
    	@Override
    	public void show() {
    		
    		System.out.println("Scientist : " + super.toString() + 
    				",salary:" + (yearSalary()+bonus)); 
    	}
    }
    

      

     

    应用案例3:

    定义一个图形类Picture,再定义Picture的两个子类:圆形类Circle、矩形类Rect。 要求: 图像类:有求周长和求面积和显示图形信息的功能。 圆形类:包含圆心和半径,重写求周长和求面积的方法。 矩形类:包含长和宽,重写求周长和求面积的方法。

    package polymorphic_HomeWork;
    
    public class WorkTest_03 {
    
    	public static void main(String[] args) {
    		Picture circle = new Circle("2,2",3.1);
    		Picture rect = new Rect(6.4,5.3);
    		
    		circle.show();
    		rect.show();
    	}
    
    }
    
    //图像类:有求周长和求面积和显示图形信息的功能。
    class Picture{
    	
    	private double sideLength;	//边长
    	private int sideCount;	//边数
    	public Picture(double sideLength, int sideCount) {
    		super();
    		this.sideLength = sideLength;
    		this.sideCount = sideCount;
    	}
    	public Picture() {
    		super();
    	}
    	public double getSideLength() {
    		return sideLength;
    	}
    	public void setSideLength(double sideLength) {
    		this.sideLength = sideLength;
    	}
    	public int getSideCount() {
    		return sideCount;
    	}
    	public void setSideCount(int sideCount) {
    		this.sideCount = sideCount;
    	}
    	
    	public double perimeter(){	//求周长
    		return sideCount * sideLength;
    	}
    	
    	public double area() {	//求面积
    		
    		return 0.0;
    	}
    	
    	public void show() {
    		
    		System.out.printf("周长(perimeter):%.2f	面积(area):%.2f
    ",perimeter(),area());
    	}
    }
    
    //圆形类Circle:包含圆心和半径,重写求周长和求面积的方法。
    class Circle extends Picture{
    	private String centerCircle; //圆心:"X,Y" 
    	private double radius;	//半径
    	
    	public Circle(String centerCircle, double radius) {
    		super();
    		this.centerCircle = centerCircle;
    		this.radius = radius;
    	}
    
    	public String getCenterCircle() {
    		return centerCircle;
    	}
    
    	public void setCenterCircle(String centerCircle) {
    		this.centerCircle = centerCircle;
    	}
    
    	public double getRadius() {
    		return radius;
    	}
    
    	public void setRadius(double radius) {
    		this.radius = radius;
    	}
    	
    	@Override
    	public double perimeter(){	//求周长
    		return 2 * radius * Math.PI;
    	}
    	
    	@Override
    	public double area() {	//求面积
    		return radius * radius * Math.PI;
    	}
    	
    	@Override
    	public void show() {
    		System.out.printf("Circle:圆心为 :%S  ",centerCircle);
    		super.show();
    	}
    }
    
    //矩形类Rect:包含长和宽,重写求周长和求面积的方法。
    class Rect extends Picture{
    	private double length;//长
    	private double wide;//宽
    	public Rect( double length, double wide) {
    		super();
    		this.length = length;
    		this.wide = wide;
    	}
    	public double getLength() {
    		return length;
    	}
    	public void setLength(double length) {
    		this.length = length;
    	}
    	public double getWide() {
    		return wide;
    	}
    	public void setWide(double wide) {
    		this.wide = wide;
    	}
    	
    	@Override
    	public double perimeter(){	//求周长
    		return 2 * length + 2 * wide;
    	}
    	
    	@Override
    	public double area() {	//求面积
    		return length * wide;
    	}
    	
    	@Override
    	public void show() {
    		System.out.print("Rect:  ");
    		super.show();
    	}
    }
    

      

      

  • 相关阅读:
    Docker之OVS网络
    Docker Weave网络部署
    Docker Macvlan网络部署
    eclipse下启动tomcat项目,访问tomcat默认端口显示404错误
    eclipse tomcat add时提示The Tomcat server configuration at ServersTomcat v6.0 Server at localhost-
    ubuntu eclipse 建立server 提示coud not load the tomcat server configuration at /opt/apache ...的解决方法
    关于TAR ZXVF命令解释
    Linux(UBUNTU) 下安装Eclipse
    spring4 之 helloworld
    Java中Bean是什么
  • 原文地址:https://www.cnblogs.com/SongHai/p/14110271.html
Copyright © 2020-2023  润新知