因为没有带优盘,见谅见谅!
实验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();
}
}
}
实验结果截图:
下次题目补上,哈哈