如何在一个方法中添加多个 return 语句?
How do I add more than one return statement inside a method?
我正在尝试使用 getter
和 setter
方法 return 某些对象的某些值。现在我不能在一个方法中 return 多个值。这种情况下,是否需要为每个方法创建不同的方法return.
如果不需要,我该如何解决?
我的代码:
package books;
public class BooksObject {
//variable for book 1
String name1 = "The brief history of time";
String isbn1 = "111";
String[] authName1 = {"S. Hawking", " Hawking's friend"};
//variable for book 2
String name2 = "100 years of solitude";
String isbn2 = "222";
String[] authName2 = {"G. Marquez", "Marquezs friend"};
//All setters
public void setBook1(String n_1, String i_1, String[] a_N1) {
name1 = n_1;
isbn1 = i_1;
String[] authName1 = a_N1;
}
public void setBook2(String n_2, String i_2, String[] a_N2) {
name2 = n_2;
isbn2 = i_2;
String[] authName2 = a_N2;
}
//All getters method
public String getBook1() {
return name1;
//return isbn1; //shows error
//return String[]authName1;//Shows error
}
}
[注意:当然我会在我的主程序中调用所有这些方法class。我只是没有在这里发布它。]
您应该创建一个包含 3 个属性的书籍 class,并且您的 getter 将 return 一个书籍实例。
而不是
String name1 = "The brief history of time";
String isbn1 = "111";
String[] authName1 = {"S. Hawking", " Hawking's friend"};
你将拥有
Book book1 = new Book ("The brief history of time", "111", {"S. Hawking", " Hawking's friend"});
然后:
public Book getBook1() {
return book1;
}
您可以通过使用书籍数组 (Book[]
) 来进一步改进您的 BooksObject
,而不是为每本书使用不同的变量。那么你就不需要为每本书单独使用 getBooki
方法。
我认为您应该按如下所示更改代码:
public class Book
{
private String name;
private String isbn;
private String[] authors;
/* constructor */
public Book(String name, String isbn, String[] authors) {
this.name = name;
this.isbn = isbn;
this.authors = authors;
}
/* setter */
public void setName(String name) {
this.name = name;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public void setAuthors(String[] authors) {
this.authors = authors;
}
/* getter */
public String getName() {
return name;
}
public String getIsbn() {
return isbn;
}
public String[] getAuthors() {
return authors;
}
}
public class Main
{
public static void main(String[] args) {
Book book1 = new Book(
"The brief history of time",
"111",
new String[]{"S. Hawking", " Hawking's friend"}
);
Book book2 = new Book(
"100 years of solitude",
"222",
new String[]{"G. Marquez", "Marquezs friend"}
);
}
}
我正在尝试使用 getter
和 setter
方法 return 某些对象的某些值。现在我不能在一个方法中 return 多个值。这种情况下,是否需要为每个方法创建不同的方法return.
如果不需要,我该如何解决?
我的代码:
package books;
public class BooksObject {
//variable for book 1
String name1 = "The brief history of time";
String isbn1 = "111";
String[] authName1 = {"S. Hawking", " Hawking's friend"};
//variable for book 2
String name2 = "100 years of solitude";
String isbn2 = "222";
String[] authName2 = {"G. Marquez", "Marquezs friend"};
//All setters
public void setBook1(String n_1, String i_1, String[] a_N1) {
name1 = n_1;
isbn1 = i_1;
String[] authName1 = a_N1;
}
public void setBook2(String n_2, String i_2, String[] a_N2) {
name2 = n_2;
isbn2 = i_2;
String[] authName2 = a_N2;
}
//All getters method
public String getBook1() {
return name1;
//return isbn1; //shows error
//return String[]authName1;//Shows error
}
}
[注意:当然我会在我的主程序中调用所有这些方法class。我只是没有在这里发布它。]
您应该创建一个包含 3 个属性的书籍 class,并且您的 getter 将 return 一个书籍实例。
而不是
String name1 = "The brief history of time";
String isbn1 = "111";
String[] authName1 = {"S. Hawking", " Hawking's friend"};
你将拥有
Book book1 = new Book ("The brief history of time", "111", {"S. Hawking", " Hawking's friend"});
然后:
public Book getBook1() {
return book1;
}
您可以通过使用书籍数组 (Book[]
) 来进一步改进您的 BooksObject
,而不是为每本书使用不同的变量。那么你就不需要为每本书单独使用 getBooki
方法。
我认为您应该按如下所示更改代码:
public class Book
{
private String name;
private String isbn;
private String[] authors;
/* constructor */
public Book(String name, String isbn, String[] authors) {
this.name = name;
this.isbn = isbn;
this.authors = authors;
}
/* setter */
public void setName(String name) {
this.name = name;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public void setAuthors(String[] authors) {
this.authors = authors;
}
/* getter */
public String getName() {
return name;
}
public String getIsbn() {
return isbn;
}
public String[] getAuthors() {
return authors;
}
}
public class Main
{
public static void main(String[] args) {
Book book1 = new Book(
"The brief history of time",
"111",
new String[]{"S. Hawking", " Hawking's friend"}
);
Book book2 = new Book(
"100 years of solitude",
"222",
new String[]{"G. Marquez", "Marquezs friend"}
);
}
}