课外天地 李树青学习天地Java程序语言课件 → [推荐]Java授课视频第八课:面向对象——多态


  共有19299人关注过本帖平板打印复制链接

主题:[推荐]Java授课视频第八课:面向对象——多态

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
课上代码——银行货币处理  发帖心情 Post By:2011/5/22 13:40:41 [只看该作者]

class Currency {
        private double amount;
        private String type;

        public Currency() {

        }

        public Currency(double amount) {
                setAmount(amount);

        }

        public double getAmount() {
                return amount;
        }

        public void setAmount(double amount) {
                this.amount = amount;
        }

        public String getType() {
                return type;
        }

        public void setType(String type) {
                this.type = type;
        }

        public String toString() {
                return type + amount;
        }
}

class Yuan extends Currency {
        public Yuan() {
                setType("Y");
        }

        public Yuan(double amount) {
                setAmount(amount);
                setType("Y");
        }

}

class Dollar extends Currency {
        public Dollar() {
                setType("$");
        }

        public Dollar(double amount) {
                setAmount(amount);
                setType("$");
        }
}

class Transaction {
        public void action(Currency a, double amount) {
                a.setAmount(amount);
                System.out.println(a);
        }
}

class Pound extends Currency
{
        public Pound() {
                setType("GBP");
        }
        public Pound(double amount) {
                setAmount(amount);
                setType("GBP");
        }
}

public class Exec {
        public static void main(String[] args) {
                Transaction tran1 = new Transaction();
                tran1.action(new Yuan(), 2000);
                tran1.action(new Dollar(), 3000);
                tran1.action(new Pound(), 500);
        }
}

[此贴子已经被作者于2011-05-22 13:42:09编辑过]

 回到顶部