一个 java 应用程序,显示您可以创建小说和非小说书籍,并显示它们的字段。 Java 中无法正确获取 main 方法

发布于 2025-01-10 05:24:28 字数 1444 浏览 0 评论 0原文

创建一个名为 Book 的抽象类。包括一个用于书名的字符串字段和一个用于书的价格的双字段。在该类中,包含一个需要书名的构造函数,并添加两个 get 方法 - 一个返回书名,一个返回价格。包括一个名为 setPrice() 的抽象方法。创建 Book 的两个子类:Fiction 和 NonFiction。每个图书都必须包含一个 setPrice() 方法,该方法将所有小说图书的价格设置为 24.99 美元,将所有非小说图书的价格设置为 37.99 美元。为每个子类编写一个构造函数,并在每个子类中包含对 setPrice() 的调用。编写一个应用程序,演示您可以创建小说书籍和非小说书籍,并显示它们的字段。 无法正确获取 main 方法。 书籍 -

public abstract class Book {
    String mBookTitle;
    double mPrice;      
    
    public Book(String title ){
        mBookTitle=title;
    }
    public String gettitle(){
        return mBookTitle;
    }
    public double getPrice(){
        return mPrice;
    }
    public abstract void setPrice();
}

小说 -

public class Fiction extends Book{

    public Fiction(String title) {
        super(title);
        setPrice();
    }
    
    public void setPrice(){
        super.mPrice=24.99;
    }
}

非小说 -

public class NonFiction extends Book{

    public NonFiction(String title) {
        super(title);
        setPrice();
    }
    
    public void setPrice(){
        super.mPrice=37.99;
    }
}

UseBook -

public class UseBook {
    public static void main(String[] args){
        Book books;
        books=new Fiction("A wrinkle in Time");
        System.out.println(books.gettitle());
        books=new NonFiction("The art of Programming");
        System.out.println(books);
    }
}

Create an abstract class named Book. Include a String field for the book’s title and a double field for the book’s price. Within the class, include a constructor that requires the book title, and add two get methods—one that returns the title and one that returns the price. Include an abstract method named setPrice(). Create two child classes of Book: Fiction and NonFiction. Each must include a setPrice() method that sets the price for all Fiction Books to $24.99 and for all NonFiction Books to $37.99. Write a constructor for each subclass, and include a call to setPrice() within each. Write an application demonstrating that you can create both a Fiction and a NonFiction Book, and display their fields.
Can't get the main method right.
Book -

public abstract class Book {
    String mBookTitle;
    double mPrice;      
    
    public Book(String title ){
        mBookTitle=title;
    }
    public String gettitle(){
        return mBookTitle;
    }
    public double getPrice(){
        return mPrice;
    }
    public abstract void setPrice();
}

Fiction -

public class Fiction extends Book{

    public Fiction(String title) {
        super(title);
        setPrice();
    }
    
    public void setPrice(){
        super.mPrice=24.99;
    }
}

NonFiction -

public class NonFiction extends Book{

    public NonFiction(String title) {
        super(title);
        setPrice();
    }
    
    public void setPrice(){
        super.mPrice=37.99;
    }
}

UseBook -

public class UseBook {
    public static void main(String[] args){
        Book books;
        books=new Fiction("A wrinkle in Time");
        System.out.println(books.gettitle());
        books=new NonFiction("The art of Programming");
        System.out.println(books);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笔一画续写前缘 2025-01-17 05:24:28

我假设您的问题是打印一些随机字符串(例如:NonFiction@15db9742)。这是因为您在 main 方法的最后一行打印 NonFiction (书籍)的对象,而不是其属性。您应该将最后一行更改为 System.out.println(books.gettitle()); 我认为这只是一些粗心的错误。

I'm assuming your issue is with printing some random string (for example: NonFiction@15db9742). This is because you are printing the object of NonFiction (books) in your last line in main method, instead of its attribute. Your should change your last line to System.out.println(books.gettitle()); This is just some careless mistake I believe.

豆芽 2025-01-17 05:24:28

了解更多关于工厂的信息
设计模式以及如何在 Java 中实现它。

https://www.baeldung.com/creational-design-patterns

工厂是创造性的设计模式,它可以学习你如何处理这些情况。

此外,要查看正常打印的对象,您应该重写 toSrting 方法,如下所示:

@Override
public String toString() {
    return String.format(
            "Book (mBookTitle=%s, mPrice=%s)", this.mBookTitle, this.mPrice);
}

Read more about Factory
design pattern and how to implement it in Java.

https://www.baeldung.com/creational-design-patterns

Factory is creational design pattern and it can learn you how to deal with these cases.

In addition, to see the object printed normally you should override the toSrting method like this:

@Override
public String toString() {
    return String.format(
            "Book (mBookTitle=%s, mPrice=%s)", this.mBookTitle, this.mPrice);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文