• Java 练习(多态性练习二)


    例子一:

    InstanceTest.java

    package com.klvchen.exer;
    
    public class InstanceTest {
    	
    	public static void main(String[] args) {
    		
    		InstanceTest test = new InstanceTest();
    		test.method(new Graduate());
    	}
    	
    	public void method(Person e) {
    		
    		String info = e.getInfo();
    		System.out.println(info);
    		
    		if(e instanceof Graduate) {
    			System.out.println("a graduated student");
    		}
    		
    		if(e instanceof Student) {
    			System.out.println("a student");
    		}
    		
    		if(e instanceof Person) {
    			System.out.println("a person");
    		}
    		
    	}
    
    }
    
    class Person{
    	protected String name = "person";
    	protected int age = 50;
    	
    	public String getInfo() {
    		return "Name: " + name + "
    " + "age: " + age;
    	}
    }
    
    class Student extends Person{
    	protected String school = "pku";
    	
    	public String getInfo() {
    		return "Name: " + name + "
    age: " + age + "
    school: " + school;
    	}
    }
    
    class Graduate extends Student {
    	public String major = "IT";
    	
    	public String getInfo() {
    		return "Name: " + name + "
    age: " + age + "
    school: " + school + "
    major" + major;
    	}
    }
    

    运行结果:

    例子二:

    定义一个测试类GeometricTest,
    编写equalsArea方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),
    编写displayGeometricobject方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)。

    GeometricObject.java

    package com.klvchen.exer1;
    
    public class GeometricObject {
    	
    	protected String color;
    	protected double weight;
    	
    	public String getColor() {
    		return color;
    	}
    	
    	public void setColor(String color) {
    		this.color = color;
    	}
    	
    	public double getWeight() {
    		return weight;
    	}
    	
    	public void setWeight(double weight) {
    		this.weight = weight;
    	}
    	
    	public GeometricObject(String color, double weight) {
    		super();
    		this.color = color;
    		this.weight = weight;
    	}
    	
    	public double findArea() {
    		return 0.0;
    	}
    }
    

    Circle.java

    package com.klvchen.exer1;
    
    public class Circle extends GeometricObject {
    	
    	private double radius;
    
    	public Circle(double radius, String color, double weight) {
    		super(color, weight);
    		this.radius = radius;
    	}
    
    	public double getRadius() {
    		return radius;
    	}
    
    	public void setRadius(double radius) {
    		this.radius = radius;
    	}
    	
    	@Override
    	public double findArea() {
    		return 3.14 * radius * radius;
    	}
    }
    
    

    MyRectangle.java

    package com.klvchen.exer1;
    
    public class MyRectangle extends GeometricObject {
    	
    	private double width;
    	private double height;
    	
    	public MyRectangle(double width, double height, String color, double weight) {
    		super(color, weight);
    		this.width = width;
    		this.height = height;
    	}
    
    	public double getWidth() {
    		return width;
    	}
    
    	public void setWidth(double width) {
    		this.width = width;
    	}
    
    	public double getHeight() {
    		return height;
    	}
    
    	public void setHeight(double height) {
    		this.height = height;
    	}
    	
    	@Override
    	public double findArea() {
    		return width * height;
    	}
    }
    

    GeometricTest.java

    package com.klvchen.exer1;
    
    public class GeometricTest {
    	
    	public static void main(String[] args) {
    		GeometricTest test = new GeometricTest();
    		
    		Circle c1 = new Circle(3.3, "white", 1.0);
    		test.displayGeometricObject(c1);
    		Circle c2 = new Circle(3.3, "white", 1.0);
    		test.displayGeometricObject(c2);
    		
    		boolean isEquals = test.equalsArea(c1, c2);
    		System.out.println("c1 和 c2 的面积是否相等: " + isEquals);
    		
    		MyRectangle rect = new MyRectangle(2.1, 3.4, "red", 2.0);
    		test.displayGeometricObject(rect);
    		
    	}
    
    	public void displayGeometricObject(GeometricObject o) {
    		System.out.println("面积为: " + o.findArea());
    	}
    	
    	//测试两个对象的面积是否相等
    	public boolean equalsArea(GeometricObject o1, GeometricObject o2) {
    		return o1.findArea() == o2.findArea();
    	}
    }
    

    运行结果:

  • 相关阅读:
    【BZOJ4198】[Noi2015]荷马史诗 贪心+堆
    【BZOJ4200】[Noi2015]小园丁与老司机 DP+最小流
    【BZOJ2839】集合计数 组合数+容斥
    【BZOJ2989】数列 kd-tree
    【BZOJ4240】有趣的家庭菜园 树状数组+贪心
    【BZOJ4238】电压 DFS树
    【BZOJ4237】稻草人 cdq分治+单调栈+二分
    Python Web学习笔记之WebSocket原理说明
    Python Web学习笔记之Cookie,Session,Token区别
    Python Web学习笔记之图解TCP/IP协议和浅析算法
  • 原文地址:https://www.cnblogs.com/klvchen/p/14441665.html
Copyright © 2020-2023  润新知