标题:如何实现两个类的结合啊..一个类调用另一类....
只看楼主
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
 问题点数:0 回复次数:14 
如何实现两个类的结合啊..一个类调用另一类....
请问大家:
比如我设计一个类
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初学者...谢谢
搜索更多相关主题的帖子: double public quarter payment purchase 
2007-06-15 14:04
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
得分:0 

书找了好久了,找不到相关的内容..
求知若渴啊.


2007-06-15 14:07
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
得分:0 

怎么没有人帮忙啊??


2007-06-15 14:13
pity1115
Rank: 1
等 级:新手上路
威 望:2
帖 子:184
专家分:0
注 册:2006-9-15
得分:0 
你的意思是把两个类合并成一个类?还是把两个类合并在一个.java文件中?

2007-06-15 14:43
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
得分:0 

恩是合并在一个.java的文件中啊..


2007-06-15 14:49
xuxinmiao
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-5-26
得分:0 
//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());
}

}
//这个就是组合啊。


2007-06-15 15:09
大嘴先生2
Rank: 1
等 级:新手上路
威 望:2
帖 子:815
专家分:0
注 册:2006-4-17
得分:0 
继承不就可以了吗?!
第2个类继承第一个类!

骑白马的未必是王子,也可能是唐僧;有翅膀的未必是天使,也可能是鸟人。
2007-06-15 15:11
pity1115
Rank: 1
等 级:新手上路
威 望:2
帖 子:184
专家分:0
注 册:2006-9-15
得分:0 
以下是引用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());
}

}
//这个就是组合啊。


2007-06-15 15:11
geqmoon
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2004-10-16
得分:0 
谢谢 pity1115 大嘴先生2 xuxinmiao

2007-06-15 16:22
大嘴先生2
Rank: 1
等 级:新手上路
威 望:2
帖 子:815
专家分:0
注 册:2006-4-17
得分:0 
不要客气!
说谢谢大家就好了

骑白马的未必是王子,也可能是唐僧;有翅膀的未必是天使,也可能是鸟人。
2007-06-15 17:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-147708-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.175938 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved