从另一个类JAVA调用变量

时间:2014-11-02 21:35:12

标签: java variables private getter accessor

好的,所以我做了三节课。一个是测试人员,另外两个是蓝图类。我似乎无法从蓝图类ScanShop调用私有双推车变量到ShoppingCart。我想过使用存取器和吸气剂,但我现在困惑自己,不知道我哪里出错了。这是我的代码:

package exercise3;
import java.util.Scanner;
public class ScanShop 
{
private double cart =0;

public double getcart()
{
    return cart;
}
public void setcart(double cart)
{
    this.cart =cart;
}
public void scan()
{

    //the prices of items
    double p1;
    double p2;
    double p3;
    double total;

    Scanner in = new Scanner(System.in);

    System.out.println("what price is item one?");
    p1 = in.nextDouble();
    System.out.println("What is price of item two?");
    p2= in.nextDouble();
    System.out.println("And what is the price of the third item?");
    p3= in.nextDouble();

      total = p1 + p2 + p3;


    System.out.printf("The total bill is %.2f\n\n", total);


    setcart(total);

    System.out.println("the cart is: " + getcart());

    in.close();




}
}

这是ShoppingCart蓝图类:

package exercise3;

import java.util.Scanner;
import javax.swing.JOptionPane;
public class ShoppingCart 
{
ScanShop amount = new ScanShop();





public void getbill()
{

    JOptionPane.showMessageDialog(null,"your total is: " +  amount.getcart());
}

public void billCal()
{
    String answer;
    int number;
    Scanner input = new Scanner(System.in);

    /*System.out.println("please enter how much your bill is:...");
    //how much bill is:
    cart = in.nextDouble();
    in.nextLine();
    System.out.printf("you have entered: %.2f", cart);*/

    System.out.println("Do you have a loyalty card? y or n");
    // asking do you have loyalty card
    answer= input.next();

    if (answer.equalsIgnoreCase("y"))
    {

        amount.setcart(amount.getcart()*0.9);

        //other vouchers to discount
        System.out.println("thats great! do you have a voucher: "
                + "\n(1) £5  "
                + "\n(2) £10 "
                + "\n (3) no vouchers");
        number= input.nextInt();
        switch(number)
        {
        case 1 :
            amount.setcart(amount.getcart()-5);
            getbill(); 
            break;

        case 2 : 
            amount.setcart(amount.getcart()-10);
            getbill();
            break;

        default : 
            getbill();
            break;
        }
    }

    else
    {
        getbill();
    }

    input.close();
}//closing billCal

}

最后这是我的测试人员课程:

package exercise3;

public class ShoppingCart_Test {
public static void main (String [] arg){
    ShoppingCart customerOne = new ShoppingCart();
    //c1 is customer one
    ScanShop  c1 = new ScanShop();


    c1.scan();
    customerOne.billCal();


}
}

2 个答案:

答案 0 :(得分:2)

您无法从其他类访问类的private变量。 private字段只能从字段的所有者类访问。

如果要获取私有字段的值,则应为该字段创建公共getter方法。

答案 1 :(得分:2)

如果您想在 ShoppingCart billCal 方法中使用 ScanShop 购物车变量,您应该改变它取一个数字(双)并对其进行处理。如果您在测试仪中创建了 ShoppingCart ,则不要在 ShoppingCart 中创建 ScanShop 的实例。就像将代码更改为:

public void getbill(double total)
{
JOptionPane.showMessageDialog(null,"your total is: " + total);
}

并像这样做你的billCal:

public void billCal(Double totalAmount)
{
    (...)
switch(number)
    {
    case 1 :
        getbill(totalAmount-5); 
        break;
    (...)
}

最后你的主要方法是:

public static void main (String [] arg){
ShoppingCart customerOne = new ShoppingCart();
ScanShop  c1 = new ScanShop();


c1.scan();
customerOne.billCal(c1.getCart());

}