从另一个JAVA类调用变量
好的,我上了三节课。一个是测试人员,另外两个是blueprint类。我似乎无法从blueprint类ScanShop调用私有双车变量到ShoppingCart。我曾想过使用访问器和获取器,但现在我弄糊涂了,不知道哪里出了问题。这是我的密码:从另一个JAVA类调用变量,java,variables,private,getter,accessor,Java,Variables,Private,Getter,Accessor,好的,我上了三节课。一个是测试人员,另外两个是blueprint类。我似乎无法从blueprint类ScanShop调用私有双车变量到ShoppingCart。我曾想过使用访问器和获取器,但现在我弄糊涂了,不知道哪里出了问题。这是我的密码: package exercise3; import java.util.Scanner; public class ScanShop { private double cart =0; public double getcart() { retu
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 blueprint类:
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();
}
}
无法从另一个类访问某个类的私有变量。只能从字段的所有者类访问私有字段
如果要获取私有字段的值,则应为该字段创建公共getter方法。如果要在ShoppingCart的billCal方法中使用ScanShop的cart变量,则应将其更改为双倍数字,并对其进行处理。如果在测试仪中同时创建ScanShop实例,则不要在ShoppingCart中创建ScanShop实例。如需将代码更改为:
public void getbill(double total)
{
JOptionPane.showMessageDialog(null,"your total is: " + total);
}
你的账单是这样的:
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());
}这个问题似乎离题了,因为它需要介绍OO编程。堆栈溢出不是好书、教程或教师的替代品。in.close;你知道你的程序因为抛出NosTouchElementException而崩溃吗?谢谢你,这就是我要做的,但我想知道你是否可以在同一个测试仪上使用两个类。所以我在ScanShop类中更改了:私有双车改为公共双车,我仍然得到相同的错误。公共的接受者和接受者和私人的一样吗?