每当我使用 showMessage(( 时,我都会得到空值;我相信这是因为 我在方法结束时返回 c。有没有办法打印出getName((,而不让它在main方法中,就像getName((在showMessage((方法中一样。 . 这是我的候选人课程
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;
}
}
只需替换
c1 = showMessage();
跟
showMessage(c1);
并修改方法showMessage
如下所示
private static void showMessage(Candidate c ){
System.out.println(c.getName());
}
所以同样,您可以使用showMessage
为其余对象打印消息,例如
showMessage(c2);
showMessage(c3);
showMessage(c4);
看构造函数:
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
//...
}
尝试将 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());
}
}
试试这个:
公共候选人(( {
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;
}