两个 类 创建相同的实例?? java
Two classes making same instance?? java
所以我在大学里参加了这个考试,就是要创作一本书 class,里面会有作者、书名、内容和译者。
主要要求是仅创建 1 个 class 并在其中创建内部 classes(作者和译者)
和一个 Main class 来测试它。
问题是当我创建作者和译者对象并尝试更改他们的名字时,他们都会更改名字。
这是我的代码:
图书Class:
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();
}
}
主要Class:
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());
}
}
它必须通过这些测试,我的问题是什么?
别告诉我让Translator实现IPerson,是我老师要求扩展的
您正在 Translator
的构造函数中进行 super(f,l)
调用。 Translator
正在更改 Author
字段,因为 Translator
的超类是 Author
。
如果 Translator
实现 IPerson
而不是扩展 Author
会更有意义。
你可以这样修复:
public Author(String f, String l, boolean authorCheck){
firstName = f;
lastName = l;
if(authorCheck)
{
author = this;
}
}
public Author(String f, String l){
firstName = f;
lastName = l;
author = this;
}
public Translator(String f, String l) {
super(f, l, false);
translator = this;
}
所以我在大学里参加了这个考试,就是要创作一本书 class,里面会有作者、书名、内容和译者。
主要要求是仅创建 1 个 class 并在其中创建内部 classes(作者和译者)
和一个 Main class 来测试它。
问题是当我创建作者和译者对象并尝试更改他们的名字时,他们都会更改名字。
这是我的代码:
图书Class:
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();
}
}
主要Class:
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());
}
}
它必须通过这些测试,我的问题是什么?
别告诉我让Translator实现IPerson,是我老师要求扩展的
您正在 Translator
的构造函数中进行 super(f,l)
调用。 Translator
正在更改 Author
字段,因为 Translator
的超类是 Author
。
如果 Translator
实现 IPerson
而不是扩展 Author
会更有意义。
你可以这样修复:
public Author(String f, String l, boolean authorCheck){
firstName = f;
lastName = l;
if(authorCheck)
{
author = this;
}
}
public Author(String f, String l){
firstName = f;
lastName = l;
author = this;
}
public Translator(String f, String l) {
super(f, l, false);
translator = this;
}