多态性的使用练习
一
package eightdemo;
//多态的练习5
public class TestInstance {
public static void main(String[] args) {
TestInstance t = new TestInstance();
t.method1(new Student());
System.out.println();
t.method1(new Graduate());
}
public void method1(Person p) {// Person p = new Student();
System.out.println(p.getInfo());
if (p instanceof Graduate) {
System.out.println("a graduate");
}
if (p instanceof Student) {
System.out.println("a student");
}
if (p instanceof 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 super.getInfo() + "\nschool: " + school;
}
}
class Graduate extends Student {
public String major = "IT";
public String getInfo() {
return super.getInfo() + "\nmajor:" + major;
}
}
二
代码一
package eightdemo1;
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.0;
}
}
代码二
package eightdemo1;
public class Circle extends GeometricObject{
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public Circle(double radius,String color,double weight){
super(color,weight);
this.radius =radius;
}
public double findArea(){
return Math.PI * radius * radius;
}
}
代码三
package eightdemo1;
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;
}
public double findArea(){
return width * height;
}
}
代码四
package eightdemo1;
public class TestGeometric {
public static void main(String[] args) {
TestGeometric t = new TestGeometric();
Circle c1 = new Circle(2.3, "Green", 1.0);
Circle c2 = new Circle(2.3, "Red", 1.0);
MyRectangle m1 = new MyRectangle(2.3, 3.0, "blue", 2.0);
t.displayGeometricObject(c2);
boolean b = t.equalsArea(m1, c2);
System.out.println(b);
}
//判断两个对象的面积是否相等
public boolean equalsArea(GeometricObject o1,GeometricObject o2){
// if(o1.findArea() == o2.findArea())
// return true;
// else
// return false;
return o1.findArea() == o2.findArea();
}
public void displayGeometricObject(GeometricObject o){//GeometricObject o = new Circle(2.3, "Red", 1.0);
System.out.println(o.findArea());
}
}
韧桂 2019-12-25