java多态性练习

GeometricObject

package 多态性练习;

public class GeometricObject {
	protected String color;
	protected double weight;
	
	protected GeometricObject(String color,double weight) {
		this.color = color;
		this.weight = 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 double findArea() {
		return 0;
	}
}

Circle

package 多态性练习;

public class Circle extends GeometricObject{
	private double radius;

	protected 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;
	}
	
	public double findArea() {
		return Math.PI * radius * radius;
	}
}

MyRectangle

package 多态性练习;

public class MyRectangle extends GeometricObject{
	private double width;
	private double height;

	protected 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;
	}

	public double findArea() {
		return width * height;
	}
}

GeometricTest

package 多态性练习;

public class GeometricTest {
	public static void main(String[] args) {
		GeometricTest t = new GeometricTest();
		t.equalArea(new Circle(2,"blue",45), new MyRectangle(4, 5, "red", 96));
		t.displayGeometricObject(new Circle(2, "blue", 45));
		t.displayGeometricObject(new MyRectangle(8, 4, "black", 8));
	}
	
	public void equalArea(GeometricObject g,GeometricObject o) {
		if(g.findArea() == o.findArea())
			System.out.println("面积相等");
		System.out.println("面积不相等");
	}
	public void displayGeometricObject(GeometricObject g) {
		System.out.println("面积为:" + g.findArea());
	}
}

InstanceTest

package 多态性练习;

public class InstanceTest {
	
	public static void main(String[] args) {
		InstanceTest t = new InstanceTest();
		t.method(new Graduate());
		
	}
	public void method(Person e) {//在类中定义方法method(Person e)
		System.out.println(e.getInfo());//根据e的类型调用相应类的getInfo()方法
		if(e instanceof Graduate) {//如果e为Graduate类的对象,输出:“a graduated student”“a student”“a person” 
			System.out.println("a graduated student");
			System.out.println("a student");
			System.out.println("a person");
		}
		else if(e instanceof Student) {//如果e为Student类的对象,输出:“a student”“a person ” 
			System.out.println("a student");
			System.out.println("a person");
		}
		else//如果e为Person类的对象,输出:“a person”
			System.out.println("a person");	
	}
}
	

class Person {
	protected String name = "person";
	protected int age = 50;

	public String getInfo() {
		return "Name: " + name + "\n" + "age: " + age;
	}
}

class Student extends Person {
	protected String school = "pku";

	public String getInfo() {
		return "Name: " + name + "\nage: " + age + "\nschool: " + school;
	}
}

class Graduate extends Student {
	public String major = "IT";

	public String getInfo() {
		return "Name: " + name + "\nage: " + age + "\nschool: " + school + "\nmajor:" + major;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值