所以我在大学里参加了这个考试,那就是创作一个具有作者,头衔,内容和翻译的书籍课程。
主要要求是只创建一个类并在其中创建内部类(作者和翻译者)
和一个Main类来测试它。
问题在于,当我创建作者和翻译器对象并尝试更改其名称时,它们都会更改名称。
这是我的代码:
书籍课程:
public class Book {
private Author author;
private Translator translator;
private String title;
private String text;
public interface IPerson {
public void setFirstName(String name);
public String getFirstName();
public void setLastName(String name);
public String getLastName();
}
//Author
public class Author implements IPerson{
private String firstName;
private String lastName;
public Author(String f, String l){
firstName = f;
lastName = l;
author = this;
}
public void setFirstName(String name){
this.firstName = name;
}
public String getFirstName(){
return this.firstName;
}
public void setLastName(String name){
this.lastName = name;
}
public String getLastName(){
return this.lastName;
}
}
public Author getAuthor(){
return this.author;
}
//Translator
public class Translator extends Author{
public Translator(String f, String l) {
super(f, l);
translator = this;
}
}
public Translator getTranslator(){
return this.translator;
}
public String getTranslatorFullName(){
return this.getTranslator().getFirstName() + " " + this.getTranslator().getLastName();
}
public String getText() {
return this.text;
}
public void setText(String text) {
this.text = text;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDisplayTitle(){
return getTitle() + " Author: " + this.getAuthor().getFirstName() + " " + this.getAuthor().getLastName();
}
}
主类:
public class Main {
public static void main(String[] args){
Book mybook = new Book();
mybook.setTitle("Fight Club");
mybook.setText("First rule of Fight Club, you do not talk about Fight Club");
Book.Author author = mybook.new Author("John","Doe");
Book.Translator translator = mybook.new Translator("Alan","Rickman");
System.out.println(mybook.getDisplayTitle());
System.out.println(mybook.getAuthor().getLastName());
System.out.println(mybook.getAuthor().getFirstName());
System.out.println(mybook.getTranslatorFullName());
System.out.println(mybook.getTranslator().getFirstName());
}
}
它必须通过这些测试,我的问题是什么?
不要告诉我让译者实施IPerson,这是我的老师要求延长
答案 0 :(得分:1)
如果Translator
实施IPerson
而不是扩展Author
,那会更有意义。
答案 1 :(得分:1)
你可以这样解决:
public Translator(String f, String l) {
super(f, l, false);
translator = this;
}
if (tsvn_creds.size() == 1)
{
std::map<CStringA,Creds>::iterator it = tsvn_creds.begin();
if (tsvn_creds.find(it->first) != tsvn_creds.end())
{
Creds cr = tsvn_creds[it->first];
user_name = cr.GetUsername();
}
}
答案 2 :(得分:0)
您正在super(f,l)
的构造函数中进行Translator
调用。 Translator
正在更改Author
字段,因为Translator
的超类是Author
。