#第四章抽象类和接口
练习
一、书本p67页,学生实践练习
//父类:(Animal )
package zuoye;
public abstract class Animal {
public double predation;
public double eat;
public Animal() {
}
public abstract String predation();
public void eat() {
}
}
//子类:狼(Wolf)
```java
package zuoye;
public class Wolf extends Animal{
public Wolf() {
super();
}
public String predation() {
return "狼王有组织地安排捕食进攻计划";
}
public void eat() {
System.out.println("狼王有组织地安排捕食进攻计划");
System.out.println("嚼碎食物,将嚼碎食物咽进肚子里...");
}
}
//子类 老虎:(Tiger )
package zuoye;
public class Tiger extends Animal{
public Tiger() {
super();
}
public String predation() {
return "老虎单枪匹马作战发现猎物一击致命";
}
public void eat() {
System.out.println("老虎单枪匹马作战发现猎物一击致命");
System.out.println("嚼碎食物,将嚼碎食物咽进肚子里...");
}
}
//子类 :猴子(Monkey)
```java
package zuoye;
public class Monkey extends Animal{
public Monkey() {
super ();
}
public String predation() {
return"猴子悠哉悠哉地爬到树上采摘香蕉";
}
public void eat() {
System.out.println("猴子悠哉悠哉地爬到树上采摘香蕉");
System.out.println("嚼碎食物,将嚼碎食物咽进肚子里...");
}
}
//测试类:(AnimalText )
package zuoye;
public class AnimalText {
public static void main(String[] args) {
Animal wolf=new Wolf();
Animal tiger=new Tiger();
Animal monkey=new Monkey();
wolf.eat();
tiger.eat();
monkey.eat();
}
}
二、创建一个父类,拥有方法计算面积getPer,获得图形类型getType,三个子类,三角形,圆形、矩形分别继承该父类,并实现对应的方法
//父类:(Shape )
public class Shape {
private String color;
public double calcPerimeter;
public String getType;
public Shape(String color) {
this.color = color;
System.out.println("----执行了Shape类的构造方法-----");
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
//三角形:(Triangl)
public class Triangl extends Shape{
private double x;
private double y;
private double z;
public Triangl(String color,double x,double y,double z) {
super(color);
this.setSide(x,y,z);
}
private void setSide(double x, double y, double z) {
if(x>y+z||y>x+z||z>x+y) {
System.out.println("三角形两边之和必须打于第三边");
return ;
}
this.x=x;
this.y=y;
this.z=z;
}
public double calcPerimeter() {
return x+y+z;
}
public String getType() {
return "三角形";
}
}
//圆形:(Circle )
public class Circle extends Shape{
private double r;
public Circle(String color,double r) {
super(color);
this.r=r;
}
public double calcPerimeter() {
return 2*Math.PI*this.r;
}
public String getType() {
return "圆形";
}
public double getR() {
return r;
}
}
//矩形:Rectangle
public class Rectangle extends Shape{
private double length;
private double width;
public Rectangle(String color,double length,double width) {
super(color);
this.getArea(length,width);
}
public double getArea(double length, double width) {
return length*width;
}
public double calcPerimeter(double length, double width) {
return (length+width)*2;
}
public String getType() {
return "矩形";
}
}
//测试类:(ShapeTest )
public class ShapeTest {
public static void main(String[] args) {
Triangl ti = new Triangl("红色", 3,4,5);
Circle cr = new Circle("红色", 4);
Rectangle rt = new Rectangle("红色", 12,10);
System.out.println(ti.getType()+"周长"+ti.calcPerimeter());
System.out.println(cr.getType()+"周长"+cr.calcPerimeter());
System.out.println(rt.getType()+"周长"+rt.calcPerimeter(12,6));
}
}
总结:不要再犯同一个错误。