Java getter和setter不起作用

时间:2017-04-02 17:15:18

标签: java constructor setter getter

当我第一次实例化类的实例时,为什么我的getter和setter会生成'null'?当我自己设置属性时,它们可以工作,但它们似乎没有适当地通过构造函数?

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", "jflash@optonline.net");  //instantiate
    Student s2 = new Student("Arnold","Lane","15 Broadway","Bronx", "NY", "alane@yahoo.net");  //instantiate
    Student s3 = new Student("Brian","Wilson","409 Surf Ave","Brooklyn", "NY", "bwilson@godonlyknows.com");  //instantiate
    Student s4 = new Student("Rosie","OGrady","190 Bowery","New York", "NY", "sweetrosie@yahoo.com");  //instantiate
    Student s5 = new Student("Gilbert","Sullivan","188 Savoy Street","Colonia", "NJ", "verymodel@gmail.com");  //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);
}
}

3 个答案:

答案 0 :(得分:2)

你的构造函数分配错误!

设置field = constructor参数;

例如

address = location;

在风格上,将参数名称设置为字段名称是很好的,并写入

this.address = address;

答案 1 :(得分: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 :(得分:-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;
}