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编辑过]