import java.text.DecimalFormat;
interface ICurrency {
public double getAmount();
public void setAmount(double amount);
public String getType();
public void setType(String type);
}
abstract class Currency implements ICurrency {
private String type;
private double amount;
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() {
DecimalFormat df = new DecimalFormat("0000.0000");
return type + df.format(amount);
}
}
class Yuan extends Currency {
public Yuan() {
setType("Y");
}
public Yuan(double amount) {
setType("Y");
setAmount(amount);
}
}
class Dollar extends Currency {
public Dollar() {
setType("$");
}
public Dollar(double amount) {
setType("$");
setAmount(amount);
}
}
class France extends Currency {
public France() {
setType("F");
}
public France(double amount) {
setType("F");
setAmount(amount);
}
}
class Action {
public void transaction(Currency y, double amount) {
y.setAmount(amount);
System.out.println(y);
}
}
public class Exec {
public static void main(String args[]) {
Action a=new Action();
Yuan y=new Yuan();
a.transaction(y, 1000);
}
}
[此贴子已经被作者于2010-12-12 08:31:29编辑过]