2017-04-02 87 views
-2

爲什麼我的getter和setters在首次實例化類的實例時生成'null'?當我自己設置屬性時,它們可以工作,但是它們似乎並沒有適當地通過構造函數?Java getter和setter不起作用

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

    System.out.println("Number of Students: "  + Student.getClassNumber()); 

    Student s1 = new Student("Jack","Sprat","9 Monroe Street","New York", "NY", "[email protected]"); //instantiate 
    Student s2 = new Student("Arnold","Lane","15 Broadway","Bronx", "NY", "[email protected]"); //instantiate 
    Student s3 = new Student("Brian","Wilson","409 Surf Ave","Brooklyn", "NY", "[email protected]"); //instantiate 
    Student s4 = new Student("Rosie","OGrady","190 Bowery","New York", "NY", "[email protected]"); //instantiate 
    Student s5 = new Student("Gilbert","Sullivan","188 Savoy Street","Colonia", "NJ", "[email protected]"); //instantiate 


    System.out.println(s1.toString());   //call the toString() method 
    System.out.println(s2.toString());      
    System.out.println(s3.toString()); 
    System.out.println(s4.toString()); 
    System.out.println(s5.toString()); 

    String before = s1.getFirstname() + " " + s2.getLastname(); //call getters 

    System.out.println("\nBefore change: " + before); 

    s1.setLastname("Paulson");         
    s2.setFirstname("George"); 
    s3.setState("NJ"); 
    s3.setCity("Wayne"); 

    System.out.println("After change: " + s2.getFirstname() + 
            " " + s1.getLastname()); 
    System.out.println(); 

    System.out.println(s1.toString());   //call the toString() method 
    System.out.println(s2.toString());      
    System.out.println(s3.toString()); 
    System.out.println(s4.toString()); 
    System.out.println(s5.toString()); 

} 
} 


class Student { 
private static int classNumber; 
private static String className = "Java 101"; 
private static String Instructor = "James Gosling"; 

private int studentId = 999; 
private String firstname; 
private String lastname; 
private String address; 
private String city; 
private String state; 
private String email; 


Student(String first, String last) { 
    firstname = first; 
    lastname = last; 
    classNumber += 1; 
    studentId += 1; 
} 


Student(String first, String last, String location, String town, String province, 
     String ping) { 
    this(first,last); //call first constructor with two variables 
    location = address; 
    town = city; 
    province = state; 
    ping = email; 
} 


static int getClassNumber()    //class method (getter) 
{ 
    return (classNumber); 
} 
static String getClassName()    
{ 
    return (className); 
} 
static String getInstructor()    
{ 
    return (className); 
} 

int getStudentId()      //instance method (getter) 
{ 
    return (studentId); 
} 
String getFirstname()     
{ 
    return (firstname); 
} 
String getLastname()     
{ 
    return (lastname); 
} 
String getAddress()      
{ 
    return (address); 
} 
String getCity()       
{ 
    return (city); 
} 
String getState()       
{ 
    return (state); 
} 
String getEmail()       
{ 
    return (email); 
} 

static void setClassNumber(int classNumber)  //static method (setter) 
{ 
    Student.classNumber = classNumber;    //"Employee" to indicate static field 
} 
static void setClassName(String className)  //static method (setter) 
{ 
    Student.className = className;    //"Employee" to indicate static field 
} 
static void setInstructor(String Instructor)  //static method (setter) 
{ 
    Student.Instructor = Instructor;    //"Employee" to indicate static field 
} 

void setFirstname(String firstname)  
{ 
    this.firstname = firstname; 
} 
void setLastname(String lastname)   
{ 
    this.lastname = lastname; 
} 
void setAddress(String address)  
{ 
    this.address = address; 
} 
void setCity(String city)   
{ 
    this.city = city; 
} 
void setState(String state)     
{ 
    this.state = state; 
} 
void setEmail(String email)     
{ 
    this.email = email; 
} 


public String toString()      // toString instance method 
{ 
    String studentProfile = 
      "\t Class Name: " + className + 
      "\t Instructor: " + Instructor + 
      "\t Number of Students: " + classNumber + 
      "\t Name: " + firstname + " " + lastname + 
      "\t Address: " + address + 
      "\t City: " + city + 
      "\t State: " + state + 
      "\t Email: " + email + 
      "\t ID: " + studentId; 
    return (studentProfile); 
} 
} 

回答

1

你可以仔細看一下你的構造函數,你會解決這一問題:

Student(String first, String last, String location, String town, String province, 
      String ping) { 
     this(first,last); 
     location = address; 
     town = city; 
     province = state; 
     ping = email; 
    } 

的問題是,你正試圖將值分配給方法的參數,但你需要分配值實例變量(不是方法參數)

以下是更好的(但不是最好的)做法:

Student(String first, String last, String location, String town, String province, 
       String ping) { 
      this(first,last); 
      address = location; 
      city = town; 
      state = province; 
      email = ping; 
     } 

爲了避免這些問題,我強烈建議您重命名相同的實例變量的變量,這樣就可以使用this操作設定值,如下圖所示:

Student(String first, String last, String address, 
    String city, String state, String email) { 
     this(first,last); 
     this.address = address; 
     this.city = city; 
     this.state = state; 
     this.email = email; 
    } 
2

你有構造函數賦值錯誤的方式!

Set field = constructor parameter;

例如

address = location; 

文體很不錯的名字dentical到字段名的參數,並寫

this.address = address; 

-1

你錯誤地分配類字段到構造函數參數。簡而言之,你的構造函數分配是錯誤的。

改變這一點:

Student(String first, String last, String location, String town, String province, 
     String ping) { 
    this(first,last); //call first constructor with two variables 
    location = address; 
    town = city; 
    province = state; 
    ping = email; 
} 

這樣:

Student(String first, String last, String location, String town, String province, 
     String ping) { 
    this(first,last); //call first constructor with two variables 
    this.address = location; 
    this.city = town; 
    this.state = province; 
    this.email = ping; 
} 
+0

感謝您的答覆!對我而言,這是另一個滑稽的疏忽。 –

+0

@ J.D.Shatz不用客氣。 :) –