Java将实例类型更改为已创建的类类型

时间:2018-02-27 19:46:25

标签: java constructor

我是Java的新手,目前我正在学习构造函数。所以我有一个class Person { private String name; private double height; Person(String name, double height) { this.name = name; this.height = height; } }

Book

public class Book { private String author; private String title; public Raamat(String author, String title) { this.author = author; this.title = title; } }

Test

public class TestBook { public static void main(String[] args) { Book harryp = new Book("Rouling", "Harry Potter"); } }

Book

我的任务是更改private String author --> private Person author 类,以便作者是Person-type而不是String-type,如下所示:

Book

所以public class Book { private Person author; private String title; public Book(String author, String title) { this.author = author; this.title = title; } } 类看起来像这样:

Person's

所以基本上它应该从Test构造函数中获取作者的名字?或者它是如何工作的?我应该如何修改any()类才能使它工作?

5 个答案:

答案 0 :(得分:2)

您必须先初始化Person对象。

Person author = new Person("foo", 100d);

然后您可以更改Book课程,如下所示:

public class Book {

    private Person author;
    private String title;

    public Book(Person author, final String title) {
        this.author = author;
        this.title = title;
    }
}

最后按如下方式初始化Book对象:

Person author = new Person("foo", 100d);
Book b = new Book(author, "My Title");

答案 1 :(得分:1)

  

所以基本上应该从Person的构造函数中获取作者的名字?

不一定。您可以通过直接在Book构造函数中创建Person来保持客户端代码的简单。

public class Book {

    private Person author;
    private String title;

    public Book(String authorName, double height, String title) {
        this.author = new Person(authorName, height);
        this.title = title;
    }
}

你会称之为:

Book book = new Book("author", 170, "book title");

Yon也可以在构造函数中接受Person作为参数:

public Book(Person author, String title) {
    this.author = author;
    this.title = title;
}

在这种情况下,客户端代码应该传递Person而不是Stringdouble

Person author = new Person("author", 170)
Book book = new Book(author, "book title");

你甚至可以通过重载构造函数来提出这两个建议 实际上,您应该使用的方式是对类客户端最实用的方式。

答案 2 :(得分:1)

解决方案1:

public class Book {
    private Person author;
    private String title;

    public Book(String author, String title) {
        this.author = new Person(author);
        this.title = title;
    }
}

class Person {

    private String name;
    private double height;
    public static final double DEFAULT_HEIGHT = 0.0;

    Person(String name, double height) {
        this.name = name;
        this.height = height;
    }

    Person(String name) {
        this(name, DEFAULT_HEIGHT);
    }
}

您必须在Person课程中再添加一个构造函数,并在将Person字符串author传递给Book

时立即生成新的Book b = new Book("Mike", "Book Title");
person

或者,将getName()实例传递到Book并通过public class Book { private Person author; private String title; public Book(Person author, String title) { this.author = author.getName(); this.title = title; } } 访问名称:

create table Ressource (
id_ressource serial primary key, 
nom_r text not null, 
url text not null,  
id_personne int not null, 
foreign key(id_personne) references Personne(primary_key_from_personne));
            ^^^^^^^^^^^^^^^^ 
            (you missed it)

答案 3 :(得分:0)

author应该是新的Person,其名称来自name构造函数的Book字段。

答案 4 :(得分:0)

有两种方法可以解决这个问题:

  • 通过将构造函数的参数类型更改为Person:

    public class Book {
    
        private Person author;
        private String title;
    
        public Raamat(Person author, String title) {
            this.author = author;
            this.title = title;
        }
    }
    
    • 通过在构造函数上初始化作者:

      public class Book {
      
          private Person author;
          private String title;
      
          public Raamat(String author, String title) {
              this.author = new Person(author);
              this.title = title;
          }
      }