【问题标题】:how to print instance variable in methods in java?如何在java中的方法中打印实例变量?
【发布时间】:2021-04-16 03:40:53
【问题描述】:

我正在尝试在 show() 方法中打印变量。但是每次它都显示默认值,例如字符串 NULL 和整数 0。我采用了参数化构造函数。因为我需要在创建类的对象时传递参数,而不是我想传递任何变量,该变量将携带来自用户的该变量的值,但我无法执行。

import java.util.*;

public class Cons_With_Arg {
    String s;
    int i;
    // Scanner sc=new Scanner(System.in);

    Cons_With_Arg(String name, int id) {
        Scanner sc = new Scanner(System.in);
        this.s = name;
        System.out.print("enter Name:");
        name = sc.nextLine();
        this.i = id;
        System.out.print("enter id:");
        id = sc.nextInt();
    }

    public void show() {
        System.out.println("Name:" + this.s);
        System.out.println("Id:" + this.i);
    }

    public static void main(String[] args) {
        Cons_With_Arg co = new Cons_With_Arg(s, i);
        // System.out.println("Name:" + co.s);
        // System.out.println("Id:" + co.i);
        co.show();
    }
}

【问题讨论】:

  • 您更新了错误的字段。您可以尝试更改 name = sc.nextLine(); to this.s= sc.nextLine();

标签: java class object constructor parameter-passing


【解决方案1】:

首先在 main 方法中获取用户输入,然后使用构造函数传递这些输入。然后在构造函数中使用 this 关键字将这些值设置为实例变量(就像你已经做过的那样)。

import java.util.*;

public class app {
    String s;
    int i;

    app(String name, int id) {
        this.s = name;
        this.i = id;
    }

    public void show() {
        System.out.println("Name:" + this.s);
        System.out.println("Id:" + this.i);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("enter Name:");
        String s = sc.nextLine();

        System.out.print("enter id:");
        int i = sc.nextInt();

        app co = new app(s, i);
        
        co.show();
    }
}

【讨论】:

    【解决方案2】:

    你不应该在构造函数中读取值。

    首先阅读它们

    public static void main(String[] args) {
        String s;
        int i;
    
        Scanner sc = new Scanner(System.in);
        System.out.print("enter Name:");
        s = sc.nextLine();
    
        System.out.print("enter id:");
        i = sc.nextInt();
    
        Cons_With_Arg co = new Cons_With_Arg(s, i);
        
        co.show();
    }
    

    然后在你的类构造函数中使用它

    Cons_With_Arg(String name, int id) {
        this.s = name;
        this.i = id;
    }
    

    现在一切正常:

    enter Name:a
    enter id:1
    Name:a
    Id:1
    

    【讨论】:

    • 虽然您的解决方案有效,但最好解释一下为什么 OP 代码无效。
    【解决方案3】:

    在通过扫描仪分配 s 和 i 之前,您正在为它们分配值。

     test(String name, int id) {
            Scanner sc = new Scanner(System.in);
    
            System.out.print("enter Name:");
            name = sc.nextLine();
            s = name;
    
            System.out.print("enter id:");
            id = sc.nextInt();
            i = id;
        }
    

    【讨论】:

    • 能否请您编写完整的代码实际上我没有得到我需要在 main() 方法中编写的内容。我需要作为论点传递什么。当我创建类的实例时,它默认将 arg 作为名称和 id,但是当我传递 s 和 I 时,它会引发错误。
    猜你喜欢
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多