以文本方式查看主题

-  课外天地 李树青  (http://www.njcie.com/bbs/index.asp)
--  Java程序语言课件  (http://www.njcie.com/bbs/list.asp?boardid=17)
----  [推荐]Java授课视频第八课:面向对象——多态  (http://www.njcie.com/bbs/dispbbs.asp?boardid=17&id=1040)

--  作者:admin
--  发布时间:2011/5/22 13:39:26
--  [推荐]Java授课视频第八课:面向对象——多态
媒体文件信息
文件来源:http://www.njcie.com/JavaSE/files/第八课:面向对象多态.wmv
您可以点击控件上的播放按钮在线播放。注意,播放此媒体文件存在一些风险。
附加说明:动网论坛系统禁止了该文件的自动播放功能。
由于该用户没有发表自动播放多媒体文件的权限或者该版面被设置成不支持多媒体播放。

--  作者:admin
--  发布时间: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编辑过]