gpt4 book ai didi

Java继承和理解接口(interface)类

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:45 24 4
gpt4 key购买 nike

我正在做一个简短的 java 作业,我已经布置好了。

题目如下:

Design and write classes to model different types of Publications in a Library. Consider carefully the different types of publications, e.g. 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.

确保在类中包含适当的构造函数、getter、setter 和自定义方法。在适当的地方使用方法重载和覆盖。

在您的设计和类代码中最大限度地利用继承。

在您的设计和编码中实现以下接口(interface)类:

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

所以我已经尽我所能回答了它,请任何人阅读它并检查我是否走在正确的轨道上并理解我打算做什么,正确似乎很简单?哦,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();
}

}

最佳答案

这看起来不错,但您根本不需要界面。作业上没看到有提到,子类化肯定没必要。

接口(interface)用于由一组不相关(特别是不属于类层次结构的一部分)的类实现的通用方法。

由于您的类都来自父 Publications 类,因此在这种情况下不需要像 PublicationsInterface 这样的东西。父类(super class)很好地填补了这个角色。

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

关于Java继承和理解接口(interface)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8903474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com