2015-12-16 35 views
-1

這兩行之間有什麼區別?Getters:區別「return firstName.get();」和「返回firstName;」

return firstName.get(); 

return firstName; 

什麼時候應該使用一個或其他?

在這裏你有當使用這些線路兩類:

package application.model; 

import javafx.beans.property.StringProperty; 

public class Employee { 
    private int id; 
    private String firstName,lastName; 

    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 


    public String getFirstName() { 
     return firstName; 
    } 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 


    public String getLastName() { 
     return lastName; 
    } 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 


    public StringProperty firstNameProperty() { 
     return firstName; 
    } 

} 
package ch.makery.address.model; 

import java.time.LocalDate; 

import javafx.beans.property.IntegerProperty; 
import javafx.beans.property.ObjectProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.beans.property.SimpleObjectProperty; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.beans.property.StringProperty; 

/** 
* Model class for a Person. 
* 
* @author Marco Jakob 
*/ 
public class Person { 

    private final StringProperty firstName; 
    private final StringProperty lastName; 
    private final StringProperty street; 
    private final IntegerProperty postalCode; 
    private final StringProperty city; 
    private final ObjectProperty<LocalDate> birthday; 

    /** 
    * Default constructor. 
    */ 
    public Person() { 
     this(null, null); 
    } 

    /** 
    * Constructor with some initial data. 
    * 
    * @param firstName 
    * @param lastName 
    */ 
    public Person(String firstName, String lastName) { 
     this.firstName = new SimpleStringProperty(firstName); 
     this.lastName = new SimpleStringProperty(lastName); 

     // Some initial dummy data, just for convenient testing. 
     this.street = new SimpleStringProperty("some street"); 
     this.postalCode = new SimpleIntegerProperty(1234); 
     this.city = new SimpleStringProperty("some city"); 
     this.birthday = new SimpleObjectProperty<LocalDate>(LocalDate.of(1999, 2, 21)); 
    } 

    public String getFirstName() { 
     return firstName.get(); 
    } 

    public void setFirstName(String firstName) { 
     this.firstName.set(firstName); 
    } 

    public StringProperty firstNameProperty() { 
     return firstName; 
    } 

    public String getLastName() { 
     return lastName.get(); 
    } 

    public void setLastName(String lastName) { 
     this.lastName.set(lastName); 
    } 

    public StringProperty lastNameProperty() { 
     return lastName; 
    } 

    public String getStreet() { 
     return street.get(); 
    } 

    public void setStreet(String street) { 
     this.street.set(street); 
    } 

    public StringProperty streetProperty() { 
     return street; 
    } 

    public int getPostalCode() { 
     return postalCode.get(); 
    } 

    public void setPostalCode(int postalCode) { 
     this.postalCode.set(postalCode); 
    } 

    public IntegerProperty postalCodeProperty() { 
     return postalCode; 
    } 

    public String getCity() { 
     return city.get(); 
    } 

    public void setCity(String city) { 
     this.city.set(city); 
    } 

    public StringProperty cityProperty() { 
     return city; 
    } 

    public LocalDate getBirthday() { 
     return birthday.get(); 
    } 

    public void setBirthday(LocalDate birthday) { 
     this.birthday.set(birthday); 
    } 

    public ObjectProperty<LocalDate> birthdayProperty() { 
     return birthday; 
    } 
} 
+3

這兩個例子中'firstName'的類型不一樣。在一個案例中,它是一個'String',另一個是'StringProperty'。 – azurefrog

+0

除了您發佈的代碼無法編譯的事實之外,一種方法使用標準的「Java Bean」模式,另一種使用「JavaFX屬性模式」。請參閱http://docs.oracle.com/javase/8/javafx/properties-binding-tutorial/binding.htm#JFXBD107 –

回答

0

簡而言之:這取決於上下文。

當對象具有方法get時,您只能使用.get()String對象與基元非常相似,它們是一個值,因此可以直接返回(它們根本沒有get方法)。

這裏,StringProperty是一個包裝周圍(assumedly)一String,如果從Person類是對return firstName;意思,你會得到一個StringProperty實例,而不是一個String - 這極有可能無法在大多數操作中使用。所以,你返回你需要的任何數據類型。

請注意,如果您在源類和目標類中都使用字符串,則可以直接返回值,它已經以可用的形式存在。

相關問題