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

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

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

问题

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

任何提示都会有所帮助

  1. while (true) {
  2. System.out.print("姓名:");
  3. String nameInput = input.nextLine();
  4. input.next();
  5. System.out.print("号码:");
  6. String number = input.nextLine();
  7. input.next();
  8. try {
  9. System.out.println("类型:\n1/2/3...?");
  10. int choice = input.nextInt();
  11. switch (choice) {
  12. case 1: {
  13. member object1 = new member(nameInput, number);
  14. System.out.println("默认姓名:" + object1.getName());
  15. }
  16. case 2: {
  17. // 这只是一个示例打印
  18. System.out.println("选择了第二种情况");
  19. }
  20. case 3: {
  21. System.out.println("选择了第三种情况");
  22. }
  23. default: {
  24. System.out.println("错误");
  25. }
  26. }
  27. } catch (InputMismatchException e) {
  28. System.out.println("错误");
  29. input.next();
  30. }
  31. }

这是"member"类:

  1. public class member {
  2. private String Name;
  3. private String Number;
  4. public member(String name, String number) {
  5. Name = name;
  6. Number = number;
  7. }
  8. public String getName() {
  9. return Name;
  10. }
  11. public String getIDNumber() {
  12. return Number;
  13. }
  14. }
英文:

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

  1. while (true) {
  2. System.out.print("Name :");
  3. String nameInput=input.nextLine();
  4. input.next();
  5. System.out.print("Number : ");
  6. String number=input.nextLine();
  7. input.next();
  8. try {
  9. System.out.println("Type: \n 1/2/3...?");
  10. int choice=input.nextInt();
  11. switch (choice){
  12. case 1:{
  13. member object1=new member(nameInput,number);
  14. System.out.println("Default name : "+object1.getName());
  15. } case 2:{
  16. //this is just a sample print
  17. System.out.println("Case 2 selected");
  18. } case 3:{
  19. System.out.println("Case 3 selected");
  20. } default:{
  21. System.out.println("Error");
  22. }
  23. }
  24. }catch (InputMismatchException e){
  25. System.out.println("Error");
  26. input.next();

This is the member class

  1. public class member {
  2. private String Name;
  3. private String Number;
  4. public member(String name, String number) {
  5. Name = name;
  6. Number = number;
  7. }
  8. public String getName() { return Name; }
  9. public String getIDNumber() { return Number;}
  10. }

答案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:

确定