Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
库模型的javaoop概念_Java_Oop_Inheritance - Fatal编程技术网

库模型的javaoop概念

库模型的javaoop概念,java,oop,inheritance,Java,Oop,Inheritance,我是Java新手,我需要一些帮助,为类似于库模型(租车模型)的项目实现OOP规则 我有两门课: 公共课堂教材 公共类客户 简单地说,我的课程是这样的: public class Customer { public String fullName; public String address; /* Constructor */ public Customer(String fullName, String address) { this.fu

我是Java新手,我需要一些帮助,为类似于库模型(租车模型)的项目实现OOP规则

我有两门课:

  • 公共课堂教材
  • 公共类客户
简单地说,我的课程是这样的:

public class Customer
{
    public String fullName;
    public String address;

    /* Constructor */
    public Customer(String fullName, String address) {
        this.fullName = fullName;
        this.address = address;
    }}
// Constructor in Class Book:
public Book(String bookName, int pageNumber, Customer customer) {
    this.bookName = bookName;
    this.pageNumber = pageNumber;
    this.customer = customer;  //I think this needs to be fixed
}
Customer mike = new Customer("Mike Malony", "N13BJ London");
Book book1 = new Book("Harry Potter", 500, mike);

Customer类的每个实例都代表一个借书人。现在,我正在努力实现的是:如何将Customer类的实例作为Book类的构造函数的参数传递?我希望这样:

public class Customer
{
    public String fullName;
    public String address;

    /* Constructor */
    public Customer(String fullName, String address) {
        this.fullName = fullName;
        this.address = address;
    }}
// Constructor in Class Book:
public Book(String bookName, int pageNumber, Customer customer) {
    this.bookName = bookName;
    this.pageNumber = pageNumber;
    this.customer = customer;  //I think this needs to be fixed
}
Customer mike = new Customer("Mike Malony", "N13BJ London");
Book book1 = new Book("Harry Potter", 500, mike);
另一个问题是,我如何为居住在伦敦的莎拉·康纳(Sarah Connor)出版的一本书制作唱片?这个实现正确吗book1=新书(“霍比特人”,300,“莎拉·康纳”,“伦敦”)?

基本解决方案 基本上,您的方法是完全正确的:

 public Book(String bookName, int pageNumber, Customer customer) {
        this.bookName = bookName;
        this.pageNumber = pageNumber;
        this.customer = customer;
 }
然而,正如您的评论所示,您仍然缺乏对Java的一些非常基本的理解。让我们回顾一下您的尝试:

Book book1 = new Book("Harry Potter", 500, "Mike", "London");
为什么这行不通?
首先,传入4个参数,而不是3个。但让我们假设这是一个打字错误,忽略“伦敦”。尽管如此,还是有一个问题:
“Mike”
是一个
字符串
,但是您的构造函数需要一个
客户
。因此,您的代码应该如下所示:

public class Customer
{
    public String fullName;
    public String address;

    /* Constructor */
    public Customer(String fullName, String address) {
        this.fullName = fullName;
        this.address = address;
    }}
// Constructor in Class Book:
public Book(String bookName, int pageNumber, Customer customer) {
    this.bookName = bookName;
    this.pageNumber = pageNumber;
    this.customer = customer;  //I think this needs to be fixed
}
Customer mike = new Customer("Mike Malony", "N13BJ London");
Book book1 = new Book("Harry Potter", 500, mike);
由于您在评论中提出了问题,因此也可以在一行中完成:

Book book1 = new Book("Harry Potter", 500, new Customer("Mike", "London"));
进一步问题和建议 现在您的问题似乎已解决,但您的代码仍然不是很。。。多才多艺的原因是您的模型受到了严重的限制:如果您传入了
客户
,则只能实例化
书籍
。但我所知道的大多数图书馆都有目前尚未借阅的书籍。另外,无论你的
页码看起来代表什么(可能是书签?),我假设一本未接触过的书不需要这些信息。因此,我建议:

public Book(String bookName) {
    this.bookName = bookName;
    this.pageNumber = 0;      // 0    => no page bookmarked
    this.customer = null;     // null => book is available
}
现在,您需要一种向客户分发图书并检查其状态的方法。我建议:

public void lend(Customer customer) {
    this.customer = customer;
}

public void receiveBack() {
    customer = null;
    pageNumber = 0;
}

public boolean isAvailable() {
    return (customer == null);
}
以及为页面添加书签:

public setBookmark(int pageNumber) {
     this.pageNumber = pageNumber;
}
当然,这还远远不够理想。如果客户想借几本书怎么办?这些书真的应该引用客户吗?有几种方法可以做到这一点。一个是建立一个专门的类,将客户与书籍联系起来。另一种方法是在
书籍
客户
中使用某种集合。但是,这可能超出了您的任务范围,因此我将把它留在上面



如果要像这样声明实例

Book book1 = new Book("The Hobbit", 300, "Sarah Connor", "London")
Book book1 = new Book("The Hobbit", 300, new Customer("Sarah Connor", "London"));
您需要像这样定义Book类的构造函数:

public Book(String bookName, int pageNumber, String fullname, String address) {
    this.bookName = bookName;
    this.pageNumber = pageNumber;
    this.customer = new Customer(fullname, address);  //I think this needs to be fixed
}
或者另一个选择是这样声明它

Book book1 = new Book("The Hobbit", 300, "Sarah Connor", "London")
Book book1 = new Book("The Hobbit", 300, new Customer("Sarah Connor", "London"));

“我认为这需要修正”-为什么?除了一个错误的名字(车而不是书)之外,你的第二个构造器是正确的。你已经拥有的东西有什么问题吗?实际上,在我看来,你拥有的东西是违反直觉的。你的类意味着一本书只有在客户借了它的情况下才存在。我更希望有一些
类引用一本借的
和一个借的
客户
。谢谢你,我认为这可以在一行代码中完成。它可以:
book book1=新书(“哈利波特”,500,新客户(“迈克”,“伦敦”))
这些书真的应该引用客户吗?
但是你的例子似乎鼓励他这么做。由于Alexandra似乎是一个学习者,我认为最有用的方法是先解决手头最突出的问题,然后就其他问题给出一些建议。这很可能是大学作业或tu专注于特定学习目标的托里尔任务,我不认为现在通过介绍太多其他方面来压倒她/他会有帮助。但是,我基本上同意你的观点,这就是为什么我在最后添加了注释。很好的解释性回答。不是吹毛求疵,而是我会使用“借出”而不是“借出”和“收回”(或类似的东西)而不是“回馈”,因为它们都是书中的方法。很好,你提到了一种我没有提到的可能性。但是,有两个问题:
新客户(…
应该是
新客户(…
)。完成后,你可以删除
//我认为这需要修复
注释:)