1、简单实现方法之一
interface ICurrency
{
public void setAmount(double a);
public double getAmount();
public String toString();
}
abstract class Currency implements ICurrency
{
protected double amount=0;
public void setAmount(double a)
{
amount=a;
}
public double getAmount()
{
return amount;
}
public String toString()
{
return "";
}
}
class Yuan extends Currency
{
public String toString()
{
return "Y"+amount;
}
}
class Dollar extends Currency
{
public String toString()
{
return "$"+amount;
}
}
class Action
{
public void TransCurrency(ICurrency y,double amount)
{
y.setAmount(amount);
System.out.println(y);
}
}
public class Application
{
public static void main(String[] args)
{
Action a=new Action();
a.TransCurrency(new Yuan(),1234);
a.TransCurrency(new Dollar(),2345);
}
}
2、简单实现方法之二
interface ICurrency
{
public void setAmount(double a);
public double getAmount();
public String toString();
}
abstract class Currency implements ICurrency
{
protected double amount=0;
protected char sign = ' ';
public void setAmount(double a)
{
amount=a;
}
public double getAmount()
{
return amount;
}
public String toString()
{
return sign+String.valueOf(amount);
}
}
class Yuan extends Currency
{
public Yuan()
{
sign='Y';
}
}
class Dollar extends Currency
{
public Dollar()
{
sign='$';
}
}
class Action
{
public void TransCurrency(ICurrency y,double amount)
{
y.setAmount(amount);
System.out.println(y);
}
}
public class Application
{
public static void main(String[] args)
{
Action a=new Action();
a.TransCurrency(new Yuan(),1234);
a.TransCurrency(new Dollar(),2345);
}
}
3、带有工厂处理方法的程序
interface ICurrency
{
public void setAmount(double a);
public double getAmount();
public String toString();
}
abstract class Currency implements ICurrency
{
protected double amount=0;
protected char sign = ' ';
public void setAmount(double a)
{
amount=a;
}
public double getAmount()
{
return amount;
}
public String toString()
{
return sign+String.valueOf(amount);
}
}
class Yuan extends Currency
{
public Yuan()
{
sign='Y';
}
}
class Dollar extends Currency
{
public Dollar()
{
sign='$';
}
}
class Action
{
public void TransCurrency(char type, double amount)
{
ICurrency im=null;
switch(Character.toUpperCase(type))
{
case 'Y':
im=new Yuan();break;
case 'D':
im=new Dollar();break;
}
im.setAmount(amount);
System.out.println(im);
}
}
public class Application
{
public static void main(String[] args)
{
Action a=new Action();
a.TransCurrency('Y',1234);
a.TransCurrency('D',2345);
}
}
4、采用java.text.DecimalFormat类实现的格式化输出
interface ICurrency
{
public void setAmount(double a);
public double getAmount();
public String toString();
}
abstract class Currency implements ICurrency
{
protected double amount=0;
protected java.util.Locale sign =null;
public void setAmount(double a)
{
amount=a;
}
public double getAmount()
{
return amount;
}
public String toString()
{
java.text.DecimalFormat nf=new java.text.DecimalFormat("¤.0000");
nf.setCurrency(java.util.Currency.getInstance(sign));
return nf.format(amount);
}
}
class Yuan extends Currency
{
public Yuan()
{
sign=java.util.Locale.CHINA;
}
}
class Dollar extends Currency
{
public Dollar()
{
sign=java.util.Locale.US;
}
}
class Action
{
public void TransCurrency(char type, double amount)
{
ICurrency im=null;
switch(Character.toUpperCase(type))
{
case 'Y':
im=new Yuan();break;
case 'D':
im=new Dollar();break;
}
im.setAmount(amount);
System.out.println(im);
}
}
public class Application
{
public static void main(String[] args)
{
Action a=new Action();
a.TransCurrency('Y',1234);
a.TransCurrency('D',2345);
}
}
[此贴子已经被作者于2010-12-12 08:24:33编辑过]