Java 从Book类简单获取作者名称
所以,我是面向对象编程新手。我正在做下一个练习: 给定定义为具有以下属性的类书:Java 从Book类简单获取作者名称,java,Java,所以,我是面向对象编程新手。我正在做下一个练习: 给定定义为具有以下属性的类书: Author author; String title; int noOfPages; boolean fiction; 为每个属性编写标准的get/set方法头 [编码]实际上是根据练习1中的属性和get/set方法对Book类进行编码和编译 这是我的代码: public class Author { //private variable private String name; pr
Author author;
String title;
int noOfPages;
boolean fiction;
为每个属性编写标准的get/set方法头
[编码]实际上是根据练习1中的属性和get/set方法对Book类进行编码和编译
这是我的代码:
public class Author {
//private variable
private String name;
private String gender;
//constructor
public Author (String name, String gender){
this.name = name;
this.gender = gender;
}
//getters
public String getName(){
return name;
}
public String getGender(){
return gender;
}
public class Book {
//private variables
private Author author;
private String title;
private int noOfPages;
private boolean fiction;
//constructor
public Book(String title, int noOfPages, boolean fiction){
this.author=new Author ("Jacquie Barker","Female");
this.title = title;
this.noOfPages=noOfPages;
this.fiction = fiction;
}
//getters
public Author getAuthorsName(){
return this.author;
}
public String getTitle(){
return title;
}
public int getNoOfPages(){
return noOfPages;
}
public boolean getFiction(){
return fiction;
}
//setters
public void setAuthor(Author newAuthor){
author=newAuthor;
}
public void setTitle (String title){
this.title=title;
}
public void setNoOfPages(int noOfpages){
this.noOfPages=noOfpages;
}
public void setfiction(boolean fiction){
this.fiction=false;
}
public String toString(){
return "Title: " + this.title + "\n"+"Author: " + this.author + "\n" +
"No. of pages: " + this.noOfPages + "\n" + "Fiction: " + this.fiction;
}
}
以下是主要内容的摘录:
Title: Beginning in Java Objects
Author: book.Author@15db9742
No. of pages: 300
Fiction: true
如您所见,程序不会打印作者的姓名
我感谢所有的帮助 如果要返回作者姓名,请将其更改为:
public Author getAuthorsName(){
return this.author;
}
致:
当前,您正在从方法返回一个类Author的对象,因此要获取作者的姓名,您需要在调用代码中调用Author.getName,或者如上所述更改现有方法以返回作者的姓名。如果要返回作者的姓名,请将此更改为:
public Author getAuthorsName(){
return this.author;
}
致:
当前,您正在从方法返回类Author的对象,因此要获取作者的姓名,您需要在调用代码中调用Author.getName,或者如上所述更改现有方法以返回作者的姓名。您应该在Author类中实现toString
public class Author {
//private variable
private String name;
private String gender;
//constructor
public Author (String name, String gender){
this.name = name;
this.gender = gender;
}
...
public String toString() {
return "Name " + name + "\t Gender: " + gender + "\n"; //Somethign like this.
}
}
您应该在Author类中实现toString
public class Author {
//private variable
private String name;
private String gender;
//constructor
public Author (String name, String gender){
this.name = name;
this.gender = gender;
}
...
public String toString() {
return "Name " + name + "\t Gender: " + gender + "\n"; //Somethign like this.
}
}
你有两个选择。首先,您可以简单地重构Book.toString方法中的代码,以打印作者的实际姓名:
public String toString(){
return "Title: " + this.title + "\n"+"Author: " + this.author.getName() + "\n" +
"No. of pages: " + this.noOfPages + "\n" + "Fiction: " +
}
其次,可以重写Author类中的toString方法以返回作者的姓名。然后,您可以保持Book.toString方法不变,因为当您尝试打印Author对象时,Java将调用此toString方法:
然后就像你一样:
public class Book {
public String toString(){
return "Title: " + this.title + "\n"+"Author: " + this.author + "\n" +
"No. of pages: " + this.noOfPages + "\n" + "Fiction: " + this.fiction;
}
}
你有两个选择。首先,您可以简单地重构Book.toString方法中的代码,以打印作者的实际姓名:
public String toString(){
return "Title: " + this.title + "\n"+"Author: " + this.author.getName() + "\n" +
"No. of pages: " + this.noOfPages + "\n" + "Fiction: " +
}
其次,可以重写Author类中的toString方法以返回作者的姓名。然后,您可以保持Book.toString方法不变,因为当您尝试打印Author对象时,Java将调用此toString方法:
然后就像你一样:
public class Book {
public String toString(){
return "Title: " + this.title + "\n"+"Author: " + this.author + "\n" +
"No. of pages: " + this.noOfPages + "\n" + "Fiction: " + this.fiction;
}
}
如果您使用的是Eclipse、IntelliJ或NetBeans等IDE,则可以让它们生成这些标准的getter和setter。浏览菜单 这样,就不会像在科幻小说中那样出现打字错误,f应该是大写
由于您是初学者,您应该首先自己编写它们,然后让它们自动生成,这样您就可以比较您的解决方案。如果您使用的是Eclipse、IntelliJ或NetBeans等IDE,您可以让它们生成这些标准的getter和setter。浏览菜单 这样,就不会像在科幻小说中那样出现打字错误,f应该是大写
由于您是初学者,您应该首先自己编写它们,然后让它们自动生成,这样您就可以比较您的解决方案。您希望它打印什么以及为什么?覆盖toString方法。当您将作者与字符串连接在一起时,会隐式调用它。您希望它打印什么以及为什么?重写toString方法。当你用字符串连接作者时,它被隐式调用。非常感谢你的快速和有用的回答非常感谢你的快速和有用的回答非常感谢你对我的问题非常有用和详细的回答!非常感谢您对我的问题的非常有帮助和详细的回答!