【问题标题】:inheretance and using main method in its own class/package继承并在自己的类/包中使用 main 方法
【发布时间】:2018-10-02 23:37:18
【问题描述】:

所以我有一个带有受保护变量和自己的包的论文发布类。

然后我有一个扩展纸质出版的书籍类。 这个书本类然后有 3 个类扩展书本类(所有三个相同的包)

图书类具有受保护的变量,扩展图书类具有私有包。

现在,当我在具有自己的包的新类中创建驱动程序时 我不能直接调用任何变量

package paperPublication;

public class PaperPublication {

protected String title;
protected double price;



package book;

import paperPublication.PaperPublication;

public class Book extends PaperPublication {
protected long ISBN;
protected int issueYear;


package book;

public class ChildrenBook extends Book {

int minimumAge;

然后我的驱动程序类把它搞砸了......我明白了为什么,因为它在它自己的包/类中。但是....我似乎无法理解如何让它正常工作

package driver;

import book.*;
import paperPublication.PaperPublication;

public class Driver{
public static void main(String[] args) {
    // TODO Auto-generated method stub
    Book a = new Book();
    System.out.println(a.ISBN);
    System.out.println(a.title);

isbn 和标题无法检索...公开。 ;(这不是我想要的。 我也已经有了所有的构造函数。

我的项目的目的是使用受保护的、公共的、私有的、封装私有整数、字符串等...使用固有性并通过创建对象并从存储的变量中调用 book.title 或 book.isbn 等直接变量来测试隐私在 main 方法的构造函数中

【问题讨论】:

  • 如果您真的希望将 Book 用作具体类,您可能应该为 Book 中的这些受保护值设置一些公共 getter。

标签: java inheritance protected


【解决方案1】:

您不能直接从Driver 类访问privateprotected 变量,如isbntitle。它根本不会编译。访问超出其预期隐私范围的变量或方法将无法编译。因此,那里涵盖了您正在寻找的隐私测试,并作为访问修饰符有效的证据。

如果您确实希望能够从预期隐私之外访问privateprotected 变量,那么您将使用下面提到的设计模式:

有很多方法(您可以使用设计模式)来解决这个问题。传统的方法是向类添加 getter 和 setter 以访问其属性。

PaperPublication 类:

package paperPublication;

public class PaperPublication {

   protected String title;
   protected double price;

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public double getPrice() {
      return price;
   }

   public void setPrice(double price) {
      this.price = price;
   }
}

书籍类:

package book;

import paperPublication.PaperPublication;

public class Book extends PaperPublication {
   protected long isbn;
   protected int issueYear;

   public long getIsbn() {
      return isbn;
   }

   public void setIsbn(long isbn) {
      this.isbn = isbn;
   }

   public int getIssueYear() {
      return issueYear;
   }

   public void setIssueYear(int issueYear) {
      this.issueYear = issueYear;
   }
}

驱动类:

package driver;

import book.*;
import paperPublication.PaperPublication;

public class Driver {
   public static void main(String[] args) {
      Book a = new Book();
      a.setIsbn(123456789L);
      a.setTitle("Best book ever!");
      System.out.println(a.getIsbn());
      System.out.println(a.getTitle());
   }
}

另一种设计模式是只有 getter,并且在创建对象时设置一次值:

PaperPublication 类:

package paperPublication;

public class PaperPublication {

   protected String title;
   protected double price;

   public PaperPublication(String title, double price) {
       this.title = title;
       this.price = price;
   }

   public String getTitle() {
      return title;
   }

   public double getPrice() {
      return price;
   }
}

书籍类:

package book;

import paperPublication.PaperPublication;

public class Book extends PaperPublication {
   protected long isbn;
   protected int issueYear;

   public Book(long isbn, int issueYear, String title, double price) {
      this.super(title, price);
      this.isbn = isbn;
      this.issueYear = issueYear;
   }

   public long getIsbn() {
      return isbn;
   }

   public int getIssueYear() {
      return issueYear;
   }
}

驱动类:

package driver;

import book.*;
import paperPublication.PaperPublication;

public class Driver {
   public static void main(String[] args) {
      // things are set in the constructor, and then you can only read, not change the values.
      Book a = new Book(123456789L, 2018, "Best book ever!", 19.99);
      System.out.println(a.getIsbn());
      System.out.println(a.getTitle());
   }
}

还有更多方法,这完全取决于您的需求。

【讨论】:

  • 我的项目的目的是使用受保护的、公共的、私有的、打包私有整数、字符串等...使用固有性并通过创建对象和调用诸如 book.title 或 book 等直接变量来测试隐私.isbn 来自存储在 main 方法中的构造函数中的变量。
  • 嘿,由于某种原因,即使我已经制作了 getter 和 setter,我也一直坚持不使用驱动方法中的 getter 和 setter.... 也许将驱动程序放在特定的包中以获得直接访问受保护的变量,但现在很清楚了!
猜你喜欢
  • 1970-01-01
  • 2020-10-21
  • 2019-06-13
  • 2021-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
相关资源
最近更新 更多