2021-06-16

#第四章抽象类和接口
练习
一、书本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));
	}

}

总结:不要再犯同一个错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值