1.建立InstanceTest 类,在类中定义方法method(Person e);
在method 中
(1)根据 e 的类型调用相应类的 getInfo 方法。
(2)根据 e 的类型执行:如果e 为 Person 类的对象,输出“a person”
如果e 为 Student 类的对象 输出:“a student”,“a person ”
如果e 为 Graduate 类的对象,输出:“a graduated student”,“a student”,“a person”
首先,创建Person,Student和Graduate类
public class Person {
protected String name="person";
protected int age=50;
public String getInfo(){
return "Name:"+name+"\n"+"age:"+age;
}
}
public class Student extends Person{
protected String school="pku";
public String getInfo(){
return "Name:"+name+"\n"+"age:"+age+"\nschool:"+school;
}
}
public class Graduate extends Student {
public String major="IT";
public String getInfo(){
return "Name:"+name+"\n"+"age:"+age+"\nschool:"+school+"\nmajor:"+major;
}
}
然后,建立InstanceTest 类进行测试
public class InstanceTest {
public static void main(String[] args) {
//InstanceTest m=new ;
InstanceTest m=new InstanceTest();
//Person n=new Person();
m.method(new Graduate());
}
public void method(Person e){
String a=e.getInfo();
System.out.println(a);
//方式一:
// if(e instanceof Graduate){
// System.out.println("a graduate student"+"\na student"+"\na person");
// }else if(e instanceof Student){
// System.out.println("a student"+"\na person");
// }else{
// System.out.println("a person");
// }
//方式二:
if(e instanceof Graduate){
System.out.println("a graduate student");
}
if(e instanceof Student){
System.out.println("a student");
}
if(e instanceof Person){
System.out.println("a person");
}
}
}
2. 定义三个类,父类 GeometricObject 代表几何形状,子类 Circle 代表圆形, MyRectangle 代表矩形 。定义一个测试类 GeometricTest 编写 equalsArea 方法测试两个对象的面积是否相等(注意方法的参
数类型,利用动态绑定技术),编写 displayGeometricObject 方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)。
(1) 创建子类和父类
public class GeometricObject {
protected String color;
protected double weight;
protected GeometricObject(){
}
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;
}
}
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;
}
public double findArea(){
return Math.PI*radius*radius;
}
}
ublic 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;
}
}
(2)进行测试
public class GeometricTest {
public static void main(String[] args) {
GeometricTest test=new GeometricTest();
MyRectangle r=new MyRectangle(1, 1, "red", 1);
Circle c=new Circle(2.3,"yellow",1.0);
Circle c1=new Circle(2.3,"yellow",1.0);
test.displayGeometricObject(c);
test.displayGeometricObject(r);
boolean isEqual=test.equalsArea(c1, c);
System.out.println("面积是否相等?"+isEqual);
}
public boolean equalsArea(GeometricObject a,GeometricObject b){
return a.findArea()==b.findArea();
}
public void displayGeometricObject(GeometricObject g){
double a1=g.findArea();
System.out.println("面积为"+a1);
}
}