繁体   English   中英

从另一个具有自己变量的类中调用方法

[英]Calling a method from another class that has its own variables

所以我试图从Customer类的Customer类中调用方法printDetails() 变量的值没有被记录,当我运行它时,我的名字最终为空,而数组却出错。 我只是不确定如何获取要记录和打印出的变量的值。

public class Customer {   
    public String name;
    public int[] itemCost;    
    public void printDetails(){
        System.out.print("Great, here is your customer's purchase details: \n");
        System.out.print("Name:"+name);
        System.out.println();
        for (int i = 0; i < itemCost.length; i ++){
            System.out.print("Item Cost #" + (i+1) + " : ");
            System.out.print(itemCost[i] + "\n");       
            }
        int sum = IntStream.of(itemCost).sum();
        System.out.println("Total:" + sum);
    }
}

public class CustomerTest {
    public static void main(String[] args){
        Customer main = new Customer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Do you want to create a customer? \n");
        String s = scan.next();
        if(s.equals("y") || s.equals("yes")){
            System.out.print("Ok, what's his/her name? \n");
            String name = scan.next();
            System.out.print("How many items is the customer buying? \n");
            int n = scan.nextInt();
            int itemCost[] = new int[n];
            for (int i = 0; i < itemCost.length; i ++){
                System.out.print("Enter a value for item #"+(i+1) );
                System.out.printf("%n");
                int j = scan.nextInt();                              
                itemCost[i] = j;
            }
        main.printDetails();
        }
    }
}

您将扫描仪中的值保存在局部变量中,而不将其添加到客户变量中。

应该看起来像这样:

    Customer main = new Customer();
    Scanner scan = new Scanner(System.in);
    System.out.print("Do you want to create a customer? \n");
    String s = scan.next();
    if(s.equals("y") || s.equals("yes")){
        System.out.print("Ok, what's his/her name? \n");
        main.name = scan.next();    //HERE
        System.out.print("How many items is the customer buying? \n");
        int n = scan.nextInt();
        main.itemCost = new int[n];    //HERE
        for (int i = 0; i < main.itemCost.length; i ++){    //HERE
            System.out.print("Enter a value for item #"+(i+1) );
            System.out.printf("%n");
            int j = scan.nextInt();                              
            main.itemCost[i] = j;    //AND HERE
        }
    main.printDetails();
    }

您需要为Customer类的实例分配值。

样例代码

 public class CustomerTest {
    public static void main(String[] args){
        Customer main = new Customer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Do you want to create a customer? \n");
        String s = scan.next();
        if(s.equals("y") || s.equals("yes")){
            System.out.print("Ok, what's his/her name? \n");
            String name = scan.next();
            main.name = name;             //added to assign value of name to Custommer's name.
            System.out.print("How many items is the customer buying? \n");
            int n = scan.nextInt();
            int itemCost[] = new int[n];
             main.itemCost = new int[n];   //Here also need to initialize size for array.
            for (int i = 0; i < itemCost.length; i ++){
                System.out.print("Enter a value for item #"+(i+1) );
                System.out.printf("%n");
                int j = scan.nextInt();                              
                itemCost[i] = j;
                main.itemCost[i] = j; //added to assign value of itemCost to Custommer's itemCost.
            }
        main.printDetails();
        }
    }
}

现在您不能获得nameitemCost空值。

希望对您有帮助。

Customer类中使用设置Setters 方法 ,以便可以将CustomerTest的值设置为Customer类变量。 像这样:

客户类别:

public class Customer {   
public String name;
public int[] itemCost;    
public void printDetails(){
    System.out.print("Great, here is your customer's purchase details: \n");
    System.out.print("Name:"+name);
    System.out.println();
    for (int i = 0; i < itemCost.length; i ++){
        System.out.print("Item Cost #" + (i+1) + " : ");
        System.out.print(itemCost[i] + "\n");       
        }
    int sum = IntStream.of(itemCost).sum();
    System.out.println("Total:" + sum);
}
/**
 * Your Setters method...
 */
public void setName(String name){
    this.name = name;
}
public void setItemCost(int[] itemCost){
    this.itemCost= itemCost;
}
}

之后,您可以在CustomerTest像这样使用它:

CustomerTest类:

// In the End part of your CustomerTestClass
main.setName(name);
main.setItemCost(itemCost);
main.printDetails();

您已经在CustomerTest中创建了与Customer类中相同的变量。

这将行不通,因为这些本地-CustomerTest类变量与实际的Customer对象属性之间没有绑定。

您需要执行以下操作才能使更改实际反映到所有位置:

class CustomerTest{

    public static void main(){
    ...
    ...
    Scanner sc = new Scanner();
    Customer obj = new Customer();
    obj.name = sc.next();
    ...
    ...
    //Test print function
    obj.printDetails();
    }
}

这将起作用! 需要记住的一点是:在类之间创建相同的名称变量不会保留对象之间的值!

您可以使用setter和getter来设置客户nameitemCost ,然后调用printDetails()方法,使用此方法时,您必须为每个对象创建客户对象,另一方面,可以通过printDetails()参数传递值像那个public void printDetails(String name, int[] itemCost) { // same logic here }然后使用main.printDetails(name, itemCost);调用它

您可以使用名称和itemCost作为参数声明一个Customer构造函数,如下所示

public Customer(String name, int[] itemCost){
    this.name = name;
    this.itemCost = itemCost;
}

在您的测试类中,您可以在用户输入后构建一个Customer对象,就像这样

public class CustomerTest {
    public static void main(String[] args){
        Customer main; // = new Customer();
        Scanner scan = new Scanner(System.in);
        System.out.print("Do you want to create a customer? \n");
        String s = scan.next();
        if(s.equals("y") || s.equals("yes")){
            System.out.print("Ok, what's his/her name? \n");
            String name = scan.next();
            System.out.print("How many items is the customer buying? \n");
            int n = scan.nextInt();
            int itemCost[] = new int[n];
            for (int i = 0; i < itemCost.length; i ++){
                System.out.print("Enter a value for item #"+(i+1) );
                System.out.printf("%n");
                int j = scan.nextInt();                              
                itemCost[i] = j;
            }
        main = new Customer(name, itemCost);
        main.printDetails();
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM