所以我創建了一個簡單的Java類是這樣的:Java:如何在這種情況下解析字符串?
public class Book {
private String author;
private String title;
public Book (String author, String title) {
this.author = author;
this.title = title;
}
}
public void checkInfo
有沒有一種方法,以獲得簿屬性就是這樣,而不是做bookA.title
解析字符串(屬性)?
Book bookA = new Book("George Orwell","Animal Farm")
String property = "title";
System.out.print(bookA.property);
非常感謝!
你可以做類似' bookA.get(property);'(在'Book'中添加'get(String)'方法之後)。我更喜歡'getTitle()'和'getAuthor()'(在這種情況下,您可以使用[JavaBean properties](https://docs.oracle.com/javase/tutorial/javabeans/writing/properties)訪問您的字段。 HTML))。 –
你需要爲'Book'類實現'getter'和'setters'。它會更方便,更好的方法由java引入。 –
我猜你實際上並不打算在你的類定義中使用「static」。 (想你會想要多個''Book''') – chsbellboy