【简答题】
阅读程序,写出运行结果 //例5.23 接口及其实现类的示例 intece Cost{//定义接口 public static final String RMB = "元"; public static final String DOLLAR = "美元"; public static final String POUND = "英镑"; public static final String DEFAULT_CURRENCY = RMB;//默认货币为人民币 public abstract double cost();//声明抽象方法,返回学习费用(按默认货币计算) public abstract double cost(String currency); //返回学习费用(按不同币种计算) } class Student {//定义学生超类 private String name; public Student(String name){ this.name = name; } } // CollegeStudent类从Student类继承,并实现Cost接口 class CollegeStudent extends Student implements Cost{ public CollegeStudent(String name) { super(name); } public double cost() { return cost(DEFAULT_CURRENCY);//按照默认货币返回费用 } public double cost(String currency) { if(currency == RMB) return 20000; //返回按人民币计算的费用 else if(currency == DOLLAR) return 3000; //返回按美元计算的费用 else return 2000; //返回按英镑计算的费用 } } class Test{ public static void main(String[] args){ CollegeStudent student = new CollegeStudent("张三"); System.out.println("学习费用:"+student.cost()+ Cost.DEFAULT_CURRENCY); System.out.println("学习费用:"+student.cost(Cost.DOLLAR)+Cost.DOLLAR); System.out.println("学习费用:"+student.cost(Cost.POUND)+Cost.POUND); } }
手机使用
分享
复制链接
新浪微博
分享QQ
微信扫一扫
微信内点击右上角“…”即可分享
反馈
收藏
举报
参考答案:
参考解析: