代码如下:
interface Shape {float perimeter()
}
class Sequare implements Shape {
private float a
public Sequare(float a) {
super()
this.a = a
}
@Override
public float perimeter() {
return 4 * a
}
}
class Rou implements Shape {
private float r
public Rou(float r) {
this.r = r
}
@Override
public float perimeter() {
return 2 * 3.14f * r
}
}
public class MainClass {
public static 竖蔽float getPerimeter(Shape s) {
return s.perimeter()
}
public static void main(String[] 早吵args) {
Sequare s = new Sequare(20)
System.out.println("Square周长:" + getPerimeter(s))
Rou r = new Rou(15.5f)
System.out.println("Rou周长:陆纤侍" + getPerimeter(r))
}
}
运行结果:
1.定义接口Universe,提供抽象方法doAnything2定义普通类Star,提亩碰供成员发光shine方法
3.定义郑耐旅普通类Sun,继承喊凳Star,实现Universe接口
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)