简体   繁体   English

Java继承和了解接口类

[英]Java Inheritance and Understanding Interface Classes

I am working on a short java assignment that I have been set. 我正在编写一个我已经设置的简短java分配。

The question is as follows: 问题如下:

Design and write classes to model different types of Publications in a Library. 设计和编写类以模拟库中不同类型的出版物。 Consider carefully the different types of publications, eg Books and Magazines. 仔细考虑不同类型的出版物,例如书籍和杂志。 Put all attributes and methods which are common to all types of Publications in a super-class, and then extend this super-class appropriately to create a set of sub-classes. 将所有类型的Publications所共有的所有属性和方法放在超类中,然后适当地扩展这个超类以创建一组子类。

Make sure that you include appropriate constructor, getter, setter and customised methods in your classes. 确保在类中包含适当的构造函数,getter,setter和自定义方法。 Use method overloading and overriding where appropriate. 在适当的地方使用方法重载和覆盖。

Make maximum use of inheritance in your design and class code. 在您的设计和类代码中最大限度地使用继承。

Implement the following Interface Class in your design and coding: 在您的设计和编码中实现以下接口类:

+ getPublisher() : String
+ getPublicationTitle() : String
+ getPrice : float
+ setPublication(publisherIn: String, titleIn:String, priceIn:float) : void

So I have answered it as best I can, please could anybody read it and check that I am on the right track and understand what I am meant to be doing, it seems fat to simple to be correct? 所以我尽可能地回答,请任何人阅读并检查我是否在正确的轨道上并了解我本来应该做的事情,这对于简单而言似乎很难正确吗? Oh and javadocs are not finished yet [= 哦,javadocs尚未完成[=

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();        
      }

}

This looks fine save that you don't need the interface at all. 这看起来很好,除了你根本不需要接口。 I didn't see it mentioned in the homework, and it's certainly not necessary for subclassing. 我没有在作业中看到它,并且它当然没有必要进行子类化。

Interfaces are for common methods implemented by a set of classes that are otherwise not related (specifically not part of a class hierarchy). 接口用于由一组类实现的常用方法,否则这些类不相关(特别是不是类层次结构的一部分)。

Since your classes all descend from the parent Publications class, there is not need for something like the PublicationsInterface in this case. 由于您的类都来自父Publications类,因此在这种情况下不需要像PublicationsInterface这样的东西。 The super class fills that role nicely. 超级课程很好地填补了这个角色。

Publication p = new Book();
p.setPublisher("Acme Books");

Your design is not unreasonable, although your naming convention is a little redundant (you don't need to name an interface with the Interface suffix). 虽然您的命名约定有点多余(您不需要使用Interface后缀命名Interface ),但您的设计并非不合理。 Also, stick with singular nouns for the class names instead of switching from Publications to Book . 此外,坚持使用单数名词作为类名,而不是从Publications切换到Book

Here is an example of using abstract classes. 以下是使用抽象类的示例。

    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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM