java基础练习## 多态性基础涉猎
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”
public class InstanceTest {
public static void main (String[]args){
InstanceTest test = new InstanceTest();
test.method(new Student());
}
public void method (Person e){
//虚拟方法的调用
String info = e.getInfo();
System.out.println(info);
if(e instanceof Graduate ){
System.out.println("a graduated student");
System.out.println("a student");
System.out.println("a person");
}else if(e instanceof Student){
System.out.println("a student");
System.out.println("a person");
}else{
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;
}
}
2.定义三个类,父类GeometricObject代表几何形状,子类Circle代表圆形,MyRectangle代表矩形。
定义一个测试类GeometricTest,编写equalsArea方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),编写displayGeometricObject方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)
package lyj.java;
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;
}
}
package lyj.java;
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 3.14 * radius * radius;
}
}
``package lyj.java;
public class MyRectangle extends GeometricObject {
private double width;
private double height;
public MyRectangle(double height,double width,String color, double weight) {
super(color, weight);
this.height = height;
this.width = width;
}
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;
}
}
```java
package lyj.java;
public class GeometricTest {
public static void main(String[] args) {
GeometricTest test = new GeometricTest();
Circle c1 = new Circle(2.3,"white",1.0);
test.displayGeometricObject(c1); //多态的使用
Circle c2 = new Circle(3.3,"white",1.0);
test.displayGeometricObject(c2);
boolean isEqual = test.equalsArea(c1, c2);
System.out.println("c1和c2的面积是否相等 " + isEqual);
}
//编写equalsArea方法测试两个对象的面积是否相等
public boolean equalsArea (GeometricObject o1,GeometricObject o2){
return o1.findArea() == o2.findArea();
}
// 编写displayGeometricObject方法显示对象的面积
public void displayGeometricObject(GeometricObject o){//GeometricObject o = new circle
System.out.println("面积为:" + o.findArea());
}
}