Java第八章作业

(1)

package Test;



interface View{
	void  display();
}
class Notice implements View{
	public void display(){
		System.out.println("通知内容");
	}
}
class Car implements View{
	public void display() {
		System.out.println("汽车油量");	
	}
}
class Ad implements View{
	public void display() {
		System.out.println("广告消息");
		
	}
}
public class test
{
	public static void main(String[] args){
		View x = new Notice();
		x.display();
		x = new Car();
		x.display();
		x = new Ad();
		x.display();
	}
}

(2)

package Test;



interface Shape{
	double size();
}
class Rec implements Shape{
	int l,h;//长宽
	public Rec() {
		super();
		this.l=0;
		this.h=0;
	}
	public Rec(int l, int h) {
		super();
		this.l = l;
		this.h = h;
	}

	public double size() {
		return l*h;
	}
}
class Circular implements Shape{
	int r;//半径
	public Circular() {
		super();
		this.r=0;
	}

	public Circular(int r) {
		super();
		this.r = r;
	}
	public double size() {
		return Math.PI*r*r;
	}
}
class Cylinder implements Shape{
	int r,h;//底面半径,高
	public Cylinder() {
		super();
		this.r = 0;
		this.h = 0;
	}
	public Cylinder(int r, int h) {
		super();
		this.r = r;
		this.h = h;
	}
	public double size() {
		return Math.PI*r*r*h;
	}
	
}
public class test
{
	public static void main(String[] args){
		Shape[] x=new Shape[3];
		x[0] = new Rec(1,2);
		x[1] = new Circular(2);
		x[2] = new Cylinder(3,4);
		for(Shape i:x) {
			System.out.println(i.size());
		}
	}
}


(3)

package Test;

abstract class Fruit{
 	public Fruit() {
		super();
	}
	abstract int getWeight();
}
class Apple extends Fruit{
	private int w;//重量
	public Apple(int w) {
		super();
		this.w = w;
	}
	int getWeight() {
		return w;
	}
}
class Peach extends Fruit{
	private int w;//重量
	public Peach(int w) {
		super();
		this.w = w;
	}
	int getWeight() {
		return w;
	}
}
class Orange extends Fruit{
	private int w;//重量

	public Orange(int w) {
		super();
		this.w = w;
	}
	int getWeight() {
		return w;
	}
}
public class test
{
	public static void main(String[] args){
		Fruit[] x = new Fruit[3];
		x[0] = new Apple(1);
		x[1] = new Peach(2);
		x[2] = new Orange(3);
		for(Fruit i:x) {
			System.out.println(i.getClass().getName()+"重 :"+i.getWeight()+"kg");
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值