Java继承与理解接口类

3
我正在完成一个简短的Java作业。
问题如下:
设计并编写类来模拟图书馆中不同类型的出版物。仔细考虑不同类型的出版物,例如书籍和杂志。将所有通用于所有出版物的属性和方法放在超类中,然后适当地扩展该超类以创建一组子类。
确保在您的类中包含适当的构造函数、getter、setter和自定义方法。在适当的情况下使用方法重载和覆盖。
在您的设计和编码中最大限度地利用继承。
在您的设计和类代码中实现以下接口类:
+ getPublisher() : String
+ getPublicationTitle() : String
+ getPrice : float
+ setPublication(publisherIn: String, titleIn:String, priceIn:float) : void

所以我已经尽力回答了,希望有人能读一下并检查我是否正确理解了我应该做什么,似乎太简单了不可能正确吧? 哦,javadocs还没有完成[=


所以我已经尽力回答了,希望有人能读一下并检查我是否正确理解了我应该做什么,似乎太简单了不可能正确吧?哦,javadoc还没有完成。
public interface PublicationInterface
{
    /**
     * Returns the book publisher name (as a String) 
     */
    public String getPublisher();

    /**
     * Returns the book publication title (as a String)
     */
    public String getPublicationTitle();

    /**
     * Returns the book price (as a float)
     */
    public float getPrice();

    /**
     * Sets the book publication details.
     * 
     * @param publisherIn   The Book Publisher (as a String)
     * @param titleIn       The Book Title (as a String)
     * @param priceIn       The Book Price (as a float)
     */
    public void setPublication(String publisherIn, String publicationTitleIn, float priceIn);
}

abstract public class Publications implements PublicationInterface
{
   // Attributes
  protected String publisher;
  protected String publicationTitle;
  protected float price;

        public Publications(String publisherIn, String publicationTitleIn, float priceIn)
            {
                publisher = publisherIn;
                publicationTitle = publicationTitleIn;
                price = priceIn;
            }

        public String getPublisher()
            {
                return (publisher);
            }

        public String getPublicationTitle()
            {
                return (publicationTitle);
            }

        public float getPrice()
            {
                return (price);
            }

        public void setPublication(String publisherIn, String publicationTitleIn, float priceIn)
            {
                publisher = publisherIn;
                publicationTitle = publicationTitleIn;
                price = priceIn;
           }

}

public class Magazine extends Publications
{
    String editor;
    String date;

    public Magazine(String publisherIn , String publicationTitleIn, float priceIn, String editorIn, String dateIn)
        {
            super (publisherIn , publicationTitleIn, priceIn);

            editor = editorIn;
            date = dateIn;
        }

    public void setPublication(String publisherIn, String publicationTitleIn, float priceIn)
        {
            publisherIn = publisher;
            publicationTitleIn = publicationTitle;
            priceIn = price;
        }

    public String getEditor()
        {
            System.out.println("The editor of this magazine is " + editor);
            return (editor);
        }

    public String getDate()
        {
            System.out.println("The publication date of this magazine is " + date);
            return (date);
        }

    public String getPublisher()
        {
            System.out.println("The publisher of this magazine is " + publisher);
            return (publisher);
        }

    public String getPublicationTitle()
        {
            System.out.println("The publication title of this magazine is " + publicationTitle);
            return (publicationTitle);
        }

    public float getPrice()
        {
            System.out.println("The price of this magazine is £" + price);
            return (price);
        }

}

public class ReferenceMaterial extends Publications
{

    String genre;
    String subject;

    public ReferenceMaterial(String publisherIn , String publicationTitleIn, float priceIn,     String genreIn, String subjectIn)
        {
            super (publisherIn , publicationTitleIn, priceIn);            

            genre = genreIn;
            subject = subjectIn;
        }

    public String getGenre()
        {
            System.out.println("The genre of this material is " + genre);
            return (genre);
        }

    public String getSubject()
        {
            System.out.println("The subject of this material is " + subject);
            return (subject);
        }

    public String getPublisher()
        {
            System.out.println("The publisher of this material is " + publisher);
            return (publisher);
        }

    public String getPublicationTitle()
        {
            System.out.println("The publication title of this material is " + publicationTitle);
            return (publicationTitle);
        }

    public float getPrice()
        {
            System.out.println("The price of this material is £" + price);
            return (price);
        }
}


public class Book extends Publications
{
    int pageNumber;
    String author;

    public Book(String publisherIn , String publicationTitleIn, float priceIn, int pageNumberIn,     String authorIn)
        {
            super (publisherIn , publicationTitleIn, priceIn);

            pageNumber = pageNumberIn;
            author = authorIn;

        }

    public int getPageNumber()
        {
            System.out.println("The number of pages in this book are " + pageNumber);
            return (pageNumber);
        }

    public String getAuthor()
        {
            System.out.println("The author of this book is " + author);
            return (author);
        }

    public String getPublisher()
        {
            System.out.println("The publisher of this book is " + publisher);
            return (publisher);
        }

    public String getPublicationTitle()
        {
            System.out.println("The publication title of this book is " + publicationTitle);
            return (publicationTitle);
        }

    public float getPrice()
        {
            System.out.println("The price of this book is £" + price);
            return (price);
        }

}

public class TestLibrary
{

    public static void main()
      {     
        Magazine magazine1 = new Magazine ("SanYonic Publishing", "Ayup Magazine", 99, "Yeshumenku Suni", "12/09/2011");

        System.out.println();
        magazine1.getEditor();
        magazine1.getDate();
        magazine1.getPublisher();
        magazine1.getPublicationTitle();
        magazine1.getPrice();
        System.out.println();

        ReferenceMaterial referenceMaterial1 = new ReferenceMaterial ("Dorling kindesy", "killer Sharks In The Solent", 200, "Nature", "Sharks");

        referenceMaterial1.getGenre();
        referenceMaterial1.getSubject();
        referenceMaterial1.getPublisher();
        referenceMaterial1.getPublicationTitle();
        referenceMaterial1.getPrice();
        System.out.println();

        Book Book1 = new Book ("Hodder & Soughton", "One Day", 75, 1105, "David Nicholls");

        Book1.getPageNumber();
        Book1.getAuthor();
        Book1.getPublisher();
        Book1.getPublicationTitle();
        Book1.getPrice();
        System.out.println();        
      }

}

2
如果这是一项作业,请使用作业标签。 - Miserable Variable
@MiserableVariable 作业标签?你是指底部的标签吗?好的,我能编辑这些标签吗?抱歉。 - Phil
阅读完整个问题后,我意识到这样的开放式问题并不适合在stackoverflow上提问。请参阅常见问题解答。但是我会告诉你,类“Publications”应该被称为“Publication”,而且你缩进方法体两次。这不是一个非常复杂的任务,只需继续提交并等待更困难的问题在这里提问。 - Miserable Variable
3个回答

2
这段话看起来不错,只是你根本不需要接口。 我在作业中没有看到提到它,而且对于子类化来说也绝对不是必需品。
接口是由一组在其他方面没有关联(特别是不是类层次结构的一部分)的类实现的公共方法。
由于您的所有类都是从父Publications类继承而来的,因此在这种情况下不需要像PublicationsInterface这样的东西。 超类很好地填补了这个角色。
Publication p = new Book();
p.setPublisher("Acme Books");

根据楼主的描述,我认为硬件需要使用一个接口,但是我同意。 - Abdullah Jibaly
@Will 谢谢你。关于接口类的问题,最后一部分确实说“在你的设计和编码中实现以下接口类”。这让我感到困惑,为什么不必要时还要这样做?或者我可以用不同的方式来利用接口类吗?! - Phil
1
我建议你保持现状,有些教练对他们的要求非常苛刻,这样做你就比较“安全”。在现实世界中,像Will提到的那样,你实际上不需要一个接口。 - Abdullah Jibaly

1

你的设计并不是不合理的,虽然你的命名约定有点冗余(你不需要用Interface后缀来命名接口)。此外,在类名上坚持使用单数名词,而不是从Publications切换到Book


1

这是使用抽象类的示例。

    public abstract class Publication 
    {
      private String _ISBN;
      private String _Title;
      private String _Publication;
      private float _Price;

      public String getISBN() { return _ISBN;}
      public void setISBN(String isbn)
      {
        _ISBN = isbn;
      }

      public String getTitle() { return _Title;}
      public void setTitle(String title)
      {
        _Title = title;
      }

      public String getTitle() { return _Title;}
      public void setTitle(String title)
      {
        _Title = title;
      } 

      public String getPublisher() { return _Publication;}
      public void setPublisher(String publication)
      {
        _Publication= publication;
      } 

       public float getPrice() { return _Price;}
       public void setPrice(float price)
       {
           _Price= price;
       } 
    }

    public class Book extends Publication
    {

    }  

    public class Magazine extends Publication
    {

    }  

//using the class
Book book = new Book();
book.getPrice();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接