2

我有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;
}

然后我的另一堂课:

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(){   
}

在这个类中,所有的方法都是必需的和需要的。其中最大的部分是空的,因为我不知道在那里做什么。我的意思是,我知道该怎么做,我不知道该怎么做。

请求是:将文章添加到包中,获取每个文章的名称和当前库存。在这里,我需要让它们与其他类的方法连接,对吗?但是如何?

4

3 回答 3

1

您的问题是将一个类数据访问到另一个类,这可以通过使用 get 方法来完成,即使您可以通过保持 set 方法来设置数据。前任:

class A
{
private int price;

public int getPrice()
{
return price;
}

public void setPrice()
{
this.price=price;
}
}

现在,如果我想访问 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();
}
于 2013-05-07T05:08:06.360 回答
0

或者你可以从基类扩展

public class Pack extends Artigo{
    public Pack(String descricao, double desconto){
            super(descricao,desconto);
}
}

如果你想访问基类的方法,你只需输入 super.methodname;

于 2013-05-07T04:13:37.367 回答
0

你应该在 Pack 中声明一个对象 Artigo

private Artigo artigo;

然后你可以在构造函数 Pack 中初始化这个对象

public Pack(String descricao, double desconto){
artigo =new Artigo(descricao,desconto);
this.descricao = descricao;
this.DESCONTO = desconto;
}

之后你可以使用 artigo 的方法

artigo.sell(1);

如你所愿

于 2013-05-07T03:49:10.540 回答