Java两个关于接口的小实验

因为没有带优盘,见谅见谅!


实验1:

接口Person中:

package test;

public interface Person 
{
	void action();
}

类PersonTest中:

package test;
class Teacher implements Person
{
	public void action()
	{
		System.out.println("教师在讲课!");
	}
}

class Student implements Person
{
	public void action()
	{
		System.out.println("学生在学习!");
	}
}

class Lab implements Person
{
	public void action()
	{
		System.out.println("实验员在管理实验室!");
	}
}

public class PersonTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Person p = new Teacher();
		p.action();
		Person p1 = new Student();
		p1.action();
		Person p2 = new Lab();
		p2.action();
	}

}

结果截图:





实验2:

接口Shape中:

package txt02;

public interface Shape 
{
	void Perimeter();
	void Area();
}

类Test中:

package txt02;

class Rect implements Shape
{
	private int length;
	private int wide;
	public void Area()
	{
		System.out.println("长方形长为:"+this.length+" 宽为:"+this.wide+"它的面积是:"+(this.length*this.wide));
	}
	public void Perimeter()
	{
		System.out.println("长方形长为:"+this.length+" 宽为:"+this.wide+"它的周长是:"+((this.length+this.wide)*2));
	}
	public int getLength()
	{
		return length;
	}
	public int getWide()
	{
		return wide;
	}
	public void setLength(int length)
	{
		this.length=length;
	}
	public void setWide(int wide)
	{
		this.wide=wide;
	}
	public Rect(int length, int wide)
	{
		setLength(length);
		setWide(wide);
	}
	
}

class Circle implements Shape
{
	private double radius;
	public void Area()
	{
		System.out.println("圆的半径为:"+this.radius+"它的面积是:"+(3.14*this.radius*this.radius));
	}
	public void Perimeter()
	{
		System.out.println("圆的半径为:"+this.radius+"它的周长是:"+(3.14*this.radius*2));
	}
	public double getRadius()
	{
		return radius;
	}
	public void setRadius(double radius)
	{
		this.radius=radius;
	}
	public Circle(double radius)
	{
		setRadius(radius);
	}
}

public class Test {

	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		Shape[] Shape = new Shape[2];
		Rect a = new Rect(5,5);
		Circle b = new Circle(8);
		Shape[0]=a;
		Shape[1]=b;
		for(int i=0; i<2; i++)
		{
			Shape[i].Perimeter();
			Shape[i].Area();
		}

	}

}

实验结果截图:



下次题目补上,哈哈


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值