简体   繁体   English

Java中子类的实现

[英]Implementation of Subclasses in Java

I am completing a homework assignment for a Java programming course and I am having trouble understanding the concept of a subclcass.我正在完成 Java 编程课程的家庭作业,但我无法理解 subclcass 的概念。

Here is the question:这是问题:

Create a class named Book that contains data fields for the title and number of pages.创建一个名为 Book 的类,其中包含标题和页数的数据字段。 Include get and set methods for these fields.包括这些字段的 get 和 set 方法。 Next, create a subclass named Textbook, which contains an additional field that holds a grade level for the Textbook and additional methods to get and set the grade level field.接下来,创建一个名为 Textbook 的子类,其中包含一个附加字段,该字段保存教科书的年级水平以及获取和设置年级水平字段的其他方法。 Write an application that demonstrates using objects of each class.编写一个应用程序来演示如何使用每个类的对象。 Save the files as Book.java, Textbook.java, and DemoBook.java.将文件保存为 Book.java、Textbook.java 和 DemoBook.java。

Here is my code for Book.java:这是我的 Book.java 代码:

public class Book
{
    String bookTitle;
    int numPages;

    private void setBTitle(String title)
    {
        bookTitle = title;
    }

    private void setBPages(int pages)
    {
        numPages = pages;
    }

    private String getBTitle()
    {
        return bookTitle;
    }

    private int getBPages()
    {
        return numPages;
    }

    public void displayBookInfo()
    {
        System.out.println("The book's title is: " + bookTitle + ".");
        System.out.println("The number of pages is: " + numPages + ".");
    }
}

Here is my code for Texbook.java:这是我的 Texbook.java 代码:

public class Textbook extends Book
{
    int gradeLevel;

    public int getGLevel()
    {
        return gradeLevel;
    }

    public void setGLevel(int level)
    {
        gradeLevel = level;
    }
    }

If I do in fact have those two parts correct, how would I implement this in a DemoBook.java file?如果我确实有这两个部分正确,我将如何在 DemoBook.java 文件中实现它?

Any help or direction would be appreciated.任何帮助或方向将不胜感激。

Here is my code for the DemoBook.java file:这是 DemoBook.java 文件的代码:

import java.util.Scanner;

public class DemoBook
{
    public static void main(String[] args)
    {
        String BTitle;
        int BPages;
        int BLevel;

        Book b = new Book();
        Textbook t = new Textbook();
        Book bt = new Textbook();

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the title of your book: ");
        BTitle = input.nextLine();
        System.out.println("Please enter the number of pages: ");
        BPages = input.nextInt();
        System.out.println("Please enter the grade level: ");
        BLevel = input.nextInt();

        b.setBTitle(BTitle);
        b.setBPages(BPages);
        t.setGLevel(BLevel);

        b.displayBookInfo();

    }
}

I changed the variables to private, and this is the compiler error I get:我将变量更改为私有,这是我得到的编译器错误:

DemoBook.java:33: error: setBTitle(String) has private access in Book
        b.setBTitle(BTitle);
         ^
DemoBook.java:34: error: setBPages(int) has private access in Book
        b.setBPages(BPages);
         ^
2 errors

I am still not grasping this.我仍然没有掌握这一点。 The chapter is titled Introduction to Inheritance.这一章的标题是继承介绍。

Code V1:代码 V1:

I believe it's just asking you to make a DemoBook.java with a main() function that creates one of each type, and does some sets and gets.我相信它只是要求您使用 main() 函数创建一个 DemoBook.java,该函数创建每种类型中的一种,并执行一些设置和获取。 The code itself seems fine to me!代码本身对我来说似乎很好!

Code V2:代码 V2:

You have to have your getters and setters all stay public!你必须让你的 getter 和 setter 都保持公开! What Tomas and other folks meant was make your variables themselves private, the String and two ints. Tomas 和其他人的意思是让你的变量本身是私有的,即字符串和两个整数。 You usually want all of your member variables to be private, and your getters and setters to be public.您通常希望所有成员变量都是私有的,而您的 getter 和 setter 是公开的。 Also, if you make your getters and setters private, you can't even access them in derived classes!此外,如果您将 getter 和 setter 设为私有,您甚至无法在派生类中访问它们!

I would also do a bit more getting and setting between your books.我还会在你的书之间做更多的获取和设置。 Try getting from one and setting into another, print out all three, go wild!尝试从一个开始并设置到另一个,打印出所有三个,疯狂! =) =)

As Danalog said, the classes are fine, now use them!正如 Danalog 所说,这些类很好,现在使用它们! Try to do an example for everything you can think of trying.试着为你能想到的一切尝试做一个例子。

I would suggest making some Book objects, Book b = new Book();我建议制作一些 Book 对象, Book b = new Book(); and calling some methods on them, make some TextBooks, TextBook t = new TextBook();并在它们上调用一些方法,制作一些教科书, TextBook t = new TextBook(); . .
And finally Book bt = new TextBook();最后Book bt = new TextBook(); . .

Play around with them all and print out some results.与他们一起玩并打印出一些结果。

EDIT: Great suggestion by @Tomas above, make the variables private as you are using getters and setters.编辑:上面@Tomas 的好建议,在使用 getter 和 setter 时将变量设为私有。

Book.java图书.java

public class Book
{
    private String bookTitle;
    private int bookPages;

    public void setBookTitle(String bookTitle)
    {
        this.bookTitle = bookTitle;
    }
    public void setBookPages(int bookPages)
    {
        this.bookPages = bookPages;
    }
    public String getBookTitle()
    {
        return this.bookTitle;
    }
    public int getBookPages()
    {
        return this.bookPages;
    }
    public void displayBookInfo()
    {
        System.out.println("The book's title is: " + this.bookTitle + ".");
        System.out.println("The number of pages is: " + this.bookPages + ".");
    }
}

Textbook.java教科书.java

public class Textbook extends Book
{
    private int gradeLevel;

    public int getGradeLevel()
    {
        return this.gradeLevel;
    }

    public void setGradeLevel(int gradeLevel)
    {
        this.gradeLevel = gradeLevel;
    }
    }

DemoBook.java DemoBook.java

DemoBook.java

import java.util.Scanner;

public class DemoBook
{
    public static void main(String[] args)
    {
        String BTitle;
        int BPages;
        int BLevel;

        Book b = new Book();
        Textbook t = new Textbook();
        Book bt = new Textbook();

        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the title of your book: ");
        BTitle = input.nextLine();
        System.out.println("Please enter the number of pages: ");
        BPages = input.nextInt();
        System.out.println("Please enter the grade level: ");
        BLevel = input.nextInt();

        b.setBookTitle(BTitle);
        b.setBookPages(BPages);
        t.setGradeLevel(BLevel);

        b.displayBookInfo();

    }
}

This assignment is trying to make you familiar with the concept of Polymorphism.这个作业试图让你熟悉多态的概念。 There's a good overview here 这里有一个很好的概述

Include get and set methods for these fields.包括这些字段的 get 和 set 方法。

This generally means that your fields (ie. member variables) should be private , encapsulating them and requiring the use of the get and set methods as the only way to access and modify those variables.这通常意味着您的字段(即成员变量)应该是private的,封装它们并要求使用getset方法作为访问和修改这些变量的唯一方法。

If I do in fact have those two parts correct, how would I implement this in a DemoBook.java file?如果我确实有这两个部分正确,我将如何在 DemoBook.java 文件中实现它?

This is the simplest class of all, it's just a test class to see if you know how to create objects of the base and derived type, and to use them properly.这是所有类中最简单的一个,它只是一个测试类,看看您是否知道如何创建基类和派生类型的对象,以及如何正确使用它们。 This is the entry point of the application, so you'll have a public class DemoBook and your main method inside there.这是应用程序的入口点,因此您将在其中拥有一个public class DemoBook和您的main方法。

You should make some test cases to ensure that the functionality of the two classes is working properly, and perhaps print out the "fields" with the get methods after you've assigned values to them with the set methods.您应该制作一些测试用例以确保这两个类的功能正常工作,并且可能在您使用set方法为它们分配值后使用get方法打印出“字段”

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

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