如何在不将 null 作为输出的情况下打印 getter 字符串?

How to print getter String without getting null as output?

每当我使用 showMessage() 时,我都会得到空值;我相信这是因为 我在方法结束时返回 c。有没有办法打印出 getName() 而不是在 main 方法中,就像 getName() 在 showMessage() 方法中一样。 . 这是我的候选人 class

public class Candidate {

private String name;
private int votes;

//default constructor
public Candidate() {

String name = "Not Available! ";
int votes = 0; 
}

//overloaded constructor
public Candidate(String _name, int _votes){
  name  = _name;
  votes = _votes;

}

//getter
public String getName(){return name;}
public int getVotes(){return votes;}

//setter

public void incrementVote(){
  votes = votes + 1;

}

public void setName(String _name){
    name =_name;
}

public void print(){

    System.out.println("To vote for " + name);


}
}

这是我的主要内容

import java.util.*;

public class Election {
public static void main(String args[]){

System.out.println("Welcome to the polls! ");

 Candidate c1 = new Candidate();
 Candidate c2 = new Candidate();
 Candidate c3 = new Candidate();
 Candidate c4 = new Candidate();


c1 = inputCandidate();
c2 = inputCandidate();
c3 = inputCandidate();
c4 = inputCandidate();



c1 = showMessage();

}

private static Candidate inputCandidate(){
    Scanner sc = new Scanner(System.in);
    String inputN;

    Candidate c = new Candidate();

    System.out.println("Enter candidate");

    inputN = sc.nextLine();
    c.setName(inputN);

   return c;
}

private static Candidate showMessage(){
    Candidate c = new Candidate();

    System.out.println(c.getName());

    return c;
}


}

查看构造函数:

public Candidate() {    
  String name = "Not Available! ";
  int votes = 0; 
}

您给局部变量赋值而不是 name 实例字段。
这些指的是两个不同的对象。所以重视一个对另一个没有任何影响。
您对 votes 字段犯了同样的错误,但这不会导致任何问题,因为 int 字段的默认值为 0

因此,设置 name 字段:

public Candidate() {    
  this.name = "Not Available! ";
  // not required to set votes as it has 0 as default value
}

或者只使用字段初始值设定项为这些字段提供默认值:

public class Candidate {
   //...       
   private String name = "Not Available! ";    
   private int vote; //  0 as default value
   //...
}

试试这个:

public候选人(){

 this.name = "Not Available! ";// Previous code is creating a new temp variable by declaring it again, This implementation will set object variable not the temp variable

 this.votes = 0; 

}

只是替换

c1 = showMessage();

有,

showMessage(c1);

并修改方法 showMessage 如下

private static void showMessage(Candidate c ){    

    System.out.println(c.getName());
}

同样,您可以使用 showMessage 打印剩余对象的消息,例如

showMessage(c2);
showMessage(c3);
showMessage(c4);

尝试将候选人作为参数传递给 showMessage 函数。然后在 showMessage 函数中使用 getName() 获取候选人姓名。 检查下面:

import java.util.*;

public class Election {
    public static void main(String args[]){
    System.out.println("Welcome to the polls! ");

    Candidate c1 = new Candidate();
    Candidate c2 = new Candidate();
    Candidate c3 = new Candidate();
    Candidate c4 = new Candidate();


    c1 = inputCandidate();
    c2 = inputCandidate();
    c3 = inputCandidate();
    c4 = inputCandidate();



    showMessage(c1);

}

private static Candidate inputCandidate(){
    Scanner sc = new Scanner(System.in);
    String inputN;

    Candidate c = new Candidate();

    System.out.println("Enter candidate");

    inputN = sc.nextLine();
    c.setName(inputN);

    return c;
}

private static void showMessage(Candidate c){


    System.out.println(c.getName());

}


}