Getter在我尝试使用get方法打印时没有返回任何内容

huangapple go评论50阅读模式
英文:

Getter isn't returning anything when I try to print using the get method

问题

我在主类中创建了一个对象,并将所有相关参数传递给了"member"类的构造函数。
但是,当我尝试调用getName()来打印姓名时,它打印出一个空格...

任何提示都会有所帮助

while (true) {
    System.out.print("姓名:");
    String nameInput = input.nextLine();
    input.next();
    System.out.print("号码:");
    String number = input.nextLine();
    input.next();
    try {
        System.out.println("类型:\n1/2/3...?");
        int choice = input.nextInt();
        switch (choice) {
            case 1: {
                member object1 = new member(nameInput, number);
                System.out.println("默认姓名:" + object1.getName());

            }
            case 2: {
                // 这只是一个示例打印
                System.out.println("选择了第二种情况");
            }
            case 3: {
                System.out.println("选择了第三种情况");
            }
            default: {
                System.out.println("错误");
            }
        }
    } catch (InputMismatchException e) {
        System.out.println("错误");
        input.next();
    }
}

这是"member"类:

public class member {
    private String Name;
    private String Number;

    public member(String name, String number) {
        Name = name;
        Number = number;
    }

    public String getName() {
        return Name;
    }

    public String getIDNumber() {
        return Number;
    }
}
英文:

I have created a object in the main class and gave all the relevant parameters to the constructor in the "member" class.
But, when I tried to print the name by calling the getName() it prints a empty space...

Any tips would be helpful

while (true) {
    System.out.print("Name :");
    String nameInput=input.nextLine();
    input.next();
    System.out.print("Number : ");
    String number=input.nextLine();
    input.next();
    try {
        System.out.println("Type: \n 1/2/3...?");
        int choice=input.nextInt();
        switch (choice){
            case 1:{
                member object1=new member(nameInput,number);
                System.out.println("Default name : "+object1.getName());

            } case 2:{
                //this is just a sample print
                System.out.println("Case 2 selected");
            } case 3:{
                System.out.println("Case 3 selected");
            } default:{
                System.out.println("Error");
            }
        }
   }catch (InputMismatchException e){
        System.out.println("Error");
        input.next();

This is the member class

public class member {
    private String Name;
    private String Number;

    public member(String name, String number) {
        Name = name;
        Number = number;
    }
    public String getName() { return Name; }
    public String getIDNumber() { return Number;}
}

答案1

得分: 2

自定义构造函数必须与类名完全相同(包括大小写),在开关的情况1中,您将对象命名为<b>关键字对象</b>,请尝试更改这些内容!

英文:

The custom constructor must be the same exact name as the class name(upper and lower case included), also in the case 1 of the switch you named the object with the <b>keyword Object</b> , try changing those things!!

答案2

得分: 0

问题在于你的构造函数。构造函数是一个特殊的方法,其名称与类名匹配。请使用大写字母来表示类名。

英文:

the problem is in your constructor. Constructor is a special method where name match with class name. and please use uppercase for class name

huangapple
  • 本文由 发表于 2020年7月30日 01:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63159427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定