this 关键字和继承 - Java

发布于 2024-10-19 20:10:10 字数 464 浏览 1 评论 0原文

好吧,这一直困扰着我。 (双关语?)

忽略变量类型,因为这不是问题

假设您有一个父类,例如带有变量 ISBN 的书籍类。构造函数使用 this.ISBN = bla 设置 ISBN。

现在有儿童班。它有一个构造函数,可以调用其中的父构造函数。首先,构造函数是如何形成的?像这样? :

public kidsBook(ISBN, kidVariable) {
   super(ISBN);
   this.kidVariable = kidVariable;
}

这样的做法正确吗?如果是这样,就会带来第二个问题:来自父类的 this.ISBN,当在子构造函数中调用父类的构造函数时,this 关键字是否引用子版本的 ISBN?

这真的让我很困惑,我敢打赌我写它的方式表明了这种困惑。

编辑:修复了与问题无关的代码错误。

Ok, so this has been messing with me. (double entendre?)

ignoring variable types since that's not the issue

Lets say you have a parent class, for example a book class, with variable ISBN. The constructor sets ISBN using this.ISBN = bla.

Now there's a child class. It has a constructor that calls the parent one inside it. First, how is the contructor formed? Like this? :

public kidsBook(ISBN, kidVariable) {
   super(ISBN);
   this.kidVariable = kidVariable;
}

Is that the right way to do it? If so that brings up the second question: the this.ISBN from the parent class, when the constructor from the parent is called in the child constructor does the this keyword refer to the child's version of ISBN?

It's really been confusing me and I bet the way I wrote it shows that confusion in spades.

Edit: Fixed code mistakes not related to the question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸 2024-10-26 20:10:10

你走在正确的轨道上。你的问题的答案是肯定的。

这是使用正确语法的完整代码的样子

class Book {
    String ISBN;

    Book(String ISBN) {
        this.ISBN = ISBN;
    }
}

class KidsBook extends Book {
    String kidsVariable;
    KidsBook(String ISBN, String kidsVariable) {
        super(ISBN);
        this.kidsVariable = kidsVariable;
    }
}

You are on the right track. The answers to your questions are yes and yes.

Here's what the complete code would look like with right syntax

class Book {
    String ISBN;

    Book(String ISBN) {
        this.ISBN = ISBN;
    }
}

class KidsBook extends Book {
    String kidsVariable;
    KidsBook(String ISBN, String kidsVariable) {
        super(ISBN);
        this.kidsVariable = kidsVariable;
    }
}
一桥轻雨一伞开 2024-10-26 20:10:10

构造函数将是:

public KidsBook(ISBN isbn, Foo kidVariable) {
   super(isbn);
   this.kidVariable = kidVariable;
}

现在,您将 isbn 参数从 KidsBook 的构造函数传递到其超类的构造函数。该超类的构造函数是:

public Book(ISBN isbn) {
   this.isbn = isbn;
}

最后,isbn 实例变量(相当于 this.isbn)等于传入的原始 isbn KidsBook 构造函数。

The constructor would be:

public KidsBook(ISBN isbn, Foo kidVariable) {
   super(isbn);
   this.kidVariable = kidVariable;
}

Now, you are passing in the isbn parameter from KidsBook's constructor to the constructor of its superclass. The constructor of that superclass is:

public Book(ISBN isbn) {
   this.isbn = isbn;
}

In the end the isbn instance variable (equivalently, this.isbn) is equal to the original isbn passed into the KidsBook constructor.

尴尬癌患者 2024-10-26 20:10:10

这里是另一个例子,希望能够澄清 java 继承的一些机制。

public class Book
{
  protected String isbn;

  public Book(String isbn)
  {
    this.isbn = isbn;
  }

  public Book()
  {
    // isbn not set
  }
}

class DoubleIsbnBook extends Book
{
  private String isbn;

  public DoubleIsbnBook(String isbn)
  {
    super(); // could be commented out, since super class' constructor is implicitly called anyway
    super.isbn = isbn; // since Book's isbn is protected, it's visible at this point
    this.isbn = isbn.toUpperCase(); // here we set DoubleIsbnBook's isbn
  }

  public DoubleIsbnBook(String isbn1, String isbn2) {
    super(isbn1); // if Book's isbn was private this would be the only way to set Book's isbn
    this.isbn = isbn2;
  }
}

Here another example that hopefully clarifies some mechanics of java inheritance.

public class Book
{
  protected String isbn;

  public Book(String isbn)
  {
    this.isbn = isbn;
  }

  public Book()
  {
    // isbn not set
  }
}

class DoubleIsbnBook extends Book
{
  private String isbn;

  public DoubleIsbnBook(String isbn)
  {
    super(); // could be commented out, since super class' constructor is implicitly called anyway
    super.isbn = isbn; // since Book's isbn is protected, it's visible at this point
    this.isbn = isbn.toUpperCase(); // here we set DoubleIsbnBook's isbn
  }

  public DoubleIsbnBook(String isbn1, String isbn2) {
    super(isbn1); // if Book's isbn was private this would be the only way to set Book's isbn
    this.isbn = isbn2;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文