如何实现两个类的结合啊..一个类调用另一类....
请问大家:比如我设计一个类
public class CashRegister
{
public CashRegister()
{
purchase = 0;
payment = 0;
}
public void recordpurchase(double amout)
{
double newtotal = purchase + amout;
purchase = newtotal;
}
public void enterpayment(double dollar,double quarter,double dime,double nickel)
{
payment = dollar*DOLLARVALUE + quarter*QUARTERVALUE + dime*DIMEVALUE + nickel*NICKELVALUE ;
}
public double givechange()
{
double change = payment - purchase;
purchase = 0;
payment =0;
return change;
}
public static final double DOLLARVALUE = 1;
public static final double QUARTERVALUE = 0.25;
public static final double DIMEVALUE = 0.1;
public static final double NICKELVALUE = 0.05;
private double purchase;
private double payment;
private double change;
}
又设计一类
public class CashRegisterTester
{
public static void main(String[] arg)
{
CashRegister register = new Cashregister();
register.recordpurchase(0.75);
register.recordpurchase(1.6);
register.enterpayment(1,2,3,4);
System.out.println(register.givechange());
}
如何实现他们的合并啊,前面有老帖有关这个内容但是讲的不是很清楚,而且编译的也没讲清楚.大家帮帮我啊...
我是相当的积极的java初学者...谢谢
----------------解决方案--------------------------------------------------------
书找了好久了,找不到相关的内容..
求知若渴啊.
----------------解决方案--------------------------------------------------------
怎么没有人帮忙啊??
----------------解决方案--------------------------------------------------------
你的意思是把两个类合并成一个类?还是把两个类合并在一个.java文件中?
----------------解决方案--------------------------------------------------------
恩是合并在一个.java的文件中啊..
----------------解决方案--------------------------------------------------------
//CashRegisterTester.java
public class CashRegister
{
public CashRegister()
{
purchase = 0;
payment = 0;
}
public void recordpurchase(double amout)
{
double newtotal = purchase + amout;
purchase = newtotal;
}
public void enterpayment(double dollar,double quarter,double dime,double nickel)
{
payment = dollar*DOLLARVALUE + quarter*QUARTERVALUE + dime*DIMEVALUE + nickel*NICKELVALUE ;
}
public double givechange()
{
double change = payment - purchase;
purchase = 0;
payment =0;
return change;
}
public static final double DOLLARVALUE = 1;
public static final double QUARTERVALUE = 0.25;
public static final double DIMEVALUE = 0.1;
public static final double NICKELVALUE = 0.05;
private double purchase;
private double payment;
private double change;
}
public class CashRegisterTester//主类
{
public static void main(String[] arg)
{
CashRegister register = new Cashregister();
register.recordpurchase(0.75);
register.recordpurchase(1.6);
register.enterpayment(1,2,3,4);
System.out.println(register.givechange());
}
}
//这个就是组合啊。
----------------解决方案--------------------------------------------------------
继承不就可以了吗?!
第2个类继承第一个类!
----------------解决方案--------------------------------------------------------
以下是引用xuxinmiao在2007-6-15 15:09:06的发言:
//CashRegisterTester.java
public class CashRegister //把这个public去掉,一个文件里只能有一个public class
{
public CashRegister()
{
purchase = 0;
payment = 0;
}
public void recordpurchase(double amout)
{
double newtotal = purchase + amout;
purchase = newtotal;
}
public void enterpayment(double dollar,double quarter,double dime,double nickel)
{
payment = dollar*DOLLARVALUE + quarter*QUARTERVALUE + dime*DIMEVALUE + nickel*NICKELVALUE ;
}
public double givechange()
{
double change = payment - purchase;
purchase = 0;
payment =0;
return change;
}
public static final double DOLLARVALUE = 1;
public static final double QUARTERVALUE = 0.25;
public static final double DIMEVALUE = 0.1;
public static final double NICKELVALUE = 0.05;
private double purchase;
private double payment;
private double change;
}
public class CashRegisterTester//主类
{
public static void main(String[] arg)
{
CashRegister register = new Cashregister();
register.recordpurchase(0.75);
register.recordpurchase(1.6);
register.enterpayment(1,2,3,4);
System.out.println(register.givechange());
}
}
//这个就是组合啊。
//CashRegisterTester.java
public class CashRegister //把这个public去掉,一个文件里只能有一个public class
{
public CashRegister()
{
purchase = 0;
payment = 0;
}
public void recordpurchase(double amout)
{
double newtotal = purchase + amout;
purchase = newtotal;
}
public void enterpayment(double dollar,double quarter,double dime,double nickel)
{
payment = dollar*DOLLARVALUE + quarter*QUARTERVALUE + dime*DIMEVALUE + nickel*NICKELVALUE ;
}
public double givechange()
{
double change = payment - purchase;
purchase = 0;
payment =0;
return change;
}
public static final double DOLLARVALUE = 1;
public static final double QUARTERVALUE = 0.25;
public static final double DIMEVALUE = 0.1;
public static final double NICKELVALUE = 0.05;
private double purchase;
private double payment;
private double change;
}
public class CashRegisterTester//主类
{
public static void main(String[] arg)
{
CashRegister register = new Cashregister();
register.recordpurchase(0.75);
register.recordpurchase(1.6);
register.enterpayment(1,2,3,4);
System.out.println(register.givechange());
}
}
//这个就是组合啊。
----------------解决方案--------------------------------------------------------
谢谢 pity1115 大嘴先生2 xuxinmiao
----------------解决方案--------------------------------------------------------
不要客气!
说谢谢大家就好了
----------------解决方案--------------------------------------------------------