你确定答案是正确的?
我自己的回答,不知道是不是正确答案,而且感觉你的题不全。
水平有限,错误请指正。
个人感觉,这题主要是考察,接口,实现,继承,多态。
(以下放在Tjava你可以运行看一下。)
--------------------
interface FlyBehavior {
public void fly();
}
class SubSonicFly implements FlyBehavior {
public void fly() {
Systemoutprintln("亚音速飞行!");
};
}
class SuperSonicFly implements FlyBehavior {
public void fly() {
Systemoutprintln("超音速飞行!"); //此处打错一个,应该是超音速飞行吧
};
}
interface TakeOffBehavior {
public void takeOff();
}
class VerticalTakeOff implements TakeOffBehavior {
public void takeOff() {
Systemoutprintln("垂直起飞!");
};
}
class LongDistanceTakeOff implements TakeOffBehavior {
public void takeOff() {
Systemoutprintln("长距离起飞!");
};
}
abstract class AirCraft { //既然是abstract的了,怎么没有abstract的方法
protected FlyBehavior flyBehavior = new SubSonicFly(); //这里应该考查的是关于多态的知识吧
protected TakeOffBehavior takeOffBehavior = new LongDistanceTakeOff(); //这里也应该考查的是关于多态的知识吧
public abstract void fly(); //就把它设成abstract的吧,反正它也不实现什么,就让子类去做具体实现吧。
public abstract void takeOff(); //把它也设成abstract的吧
}
class Helicopter extends AirCraft {
public Helicopter() {
flyBehavior = new SuperSonicFly(); //这里因为是从父类继承的。所以不用在定义了。
takeOffBehavior = new VerticalTakeOff();
}
@Override
public void fly() { //实现父类的方法
flyBehaviorfly();
}
@Override
public void takeOff() { //实现父类的方法
takeOffBehaviortakeOff();
}
}
public class T{ //在这里,我增加了一个main方法,来观察效果
private static Helicopter he = new Helicopter();
public static void main(String[] args){
hefly();
hetakeOff();
}
}
package comshape;
public interface Volume {
double PI = 31415926;
//计算体积
public double calVolume();
//计算表面积
public double calArea();
}
package comshape;
//圆柱体
public class Cylinder implements Volume {
private double Radius ;
private double Height;
public Cylinder(double Radius, double Height) {
thisRadius = Radius;
thisHeight = Height;
}
public double getRadius() {
return Radius;
}
public void setRadius(double radius) {
Radius = radius;
}
public double getHeight() {
return Height;
}
public void setHeight(double height) {
Height = height;
}
@Override
public double calVolume() {
// TODO Auto-generated method stub
return PI Radius Radius Height;
}
@Override
public double calArea() {
// TODO Auto-generated method stub
return PI Radius Radius 2 + PI Radius Height 2;
}
}
package comshape;
//圆锥
public class Cone implements Volume {
private double Radius;
private double Height;
public double getRadius() {
return Radius;
}
public void setRadius(double radius) {
Radius = radius;
}
public double getHeight() {
return Height;
}
public void setHeight(double height) {
Height = height;
}
public Cone(double Radius, double Height) {
thisRadius = Radius;
thisHeight = Height;
}
//圆锥体的母线的长
public double getConeLine() {
double Line = Radius Radius + Height Height;
return Mathsqrt(Line);
}
@Override
public double calVolume() {
// TODO Auto-generated method stub
return PI Radius Radius Height / 3;
}
@Override
public double calArea() {
// TODO Auto-generated method stub
return PI Radius Radius + PI Radius getConeLine();
}
}
public class ShapeTestDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cylinder cy = new Cylinder(3, 4);
Systemoutprintln(cycalVolume());
Systemoutprintln(cycalArea());
Cone cone = new Cone(3, 4);
Systemoutprintln(conecalVolume());
Systemoutprintln(conegetConeLine());
Systemoutprintln(conecalArea());
}
}
以上就是关于关于JAVA接口程序设计全部的内容,包括:关于JAVA接口程序设计、java编程关于接口的程序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)