JAVA - 使用另一个方法类的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16410768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAVA - Use method from another method class
提问by Tiago Brites
I have 2 classes; The first has this code:
我有 2 个班级;第一个有这个代码:
public class Artigo {
private String descricao;
private double preco;
private int stockCorrente;
public Artigo(String descricao, double preco){
Artigo(descricao, preco, 0);
}
private void Artigo(String descricao, double preco, int stockCorrente){
this.descricao = descricao;
if (preco < 0 || stockCorrente < 0) {
System.out.println("Erro: Valores negativos.");
System.exit(0);
} else
this.preco = preco;
this.stockCorrente = stockCorrente;
}
public String getDescricao(){
return descricao;
}
public double getPreco(){
return preco;
}
public void setPreco(double preco){
this.preco = preco;
}
public int getStockCorrente(){
return stockCorrente;
}
public boolean isStockEnough(int nUnits){
if (stockCorrente < nUnits){
return false;
}else{
return true;
}
}
public boolean sell(int nUnits){
if (isStockEnough(nUnits)){
stockCorrente = stockCorrente - nUnits;
return true;
}else{
return false;
}
}
public void recharge(int nUnits){
stockCorrente = nUnits;
}
public boolean equals(Artigo otherProducts){
return this.descricao.equalsIgnoreCase(otherProducts.descricao);
}
public String toString(){
return descricao + ", pre?o: " + preco + ", stock: " + stockCorrente;
}
And then my other class:
然后是我的另一堂课:
public class Pack {
private String descricao;
private int nArtigos;
private double DESCONTO;
public Pack(String descricao, double desconto){
this.descricao = descricao;
this.DESCONTO = desconto;
}
public String getDescricao(){
return descricao;
}
public int getnArtigos(){
return nArtigos;
}
public double getDesconto(){
return DESCONTO;
}
public int getStockCorrente(){
return stockCorrente;
}
public boolean equals(Pack otherpacks){
return this.descricao.equalsIgnoreCase(otherpacks.descricao);
}
public String toString(){
return descricao + " com os artigos [ " + " ], com desconto de " + DESCONTO + ", com preco de " + "PRECO" + ", com stock corrente de " + "STOCK" ;
}
public boolean existArtigo(Artigo otherProducts){
}
public boolean addArtigo(Artigo newArtigo){
}
public boolean haveStock(int nUnits){
}
public boolean sell(int nUnits){
}
public double getPreco(){
}
In this class, all the methods are required and needed. The biggest part of them are empty because I don't know what to do in there. I mean, I know what to do, what I don't know it HOW to do.
在这个类中,所有的方法都是必需的和需要的。其中最大的部分是空的,因为我不知道在那里做什么。我的意思是,我知道该怎么做,我不知道该怎么做。
The request is: Add articles to the pack, get the name and the current stock of each. Here I need to get them connecting with the methods of the other class, right? But How?
请求是:将文章添加到包中,获取每个文章的名称和当前库存。在这里,我需要让它们与其他类的方法连接,对吗?但是如何?
回答by Leo
you should declare a object Artigo in Pack
你应该在 Pack 中声明一个对象 Artigo
private Artigo artigo;
then you could initialize this object in constructor Pack
那么你可以在构造函数包中初始化这个对象
public Pack(String descricao, double desconto){
artigo =new Artigo(descricao,desconto);
this.descricao = descricao;
this.DESCONTO = desconto;
}
after that you could use method of artigo by
之后你可以使用artigo的方法
artigo.sell(1);
as you want to
如你所愿
回答by Leo
or you could extend from base class
或者你可以从基类扩展
public class Pack extends Artigo{
public Pack(String descricao, double desconto){
super(descricao,desconto);
}
}
if you want to access method of base class you just type super.methodname;
如果你想访问基类的方法,你只需输入 super.methodname ;
回答by Jayant Jadhav
your problem is accessing one class data into other class this can be done by using get method even you can set data by keeping set Method. ex:
您的问题是将一个类的数据访问到另一个类中,这可以通过使用 get 方法来完成,即使您可以通过保留 set 方法来设置数据。前任:
class A
{
private int price;
public int getPrice()
{
return price;
}
public void setPrice()
{
this.price=price;
}
}
Now if i want to access price data in Class B then
现在,如果我想访问 B 类中的价格数据,那么
class B
{
//create object of class A in any method where you want to access price data
A a=new A();
int price =a.getPrice();
}