简体   繁体   English

在不初始化的情况下扩展Java类

[英]Extending a class in Java without initializing

public class LinkedListStructs {
    public LinkedList l1 = new LinkedList();

    public LinkedListStructs(){
        ListNode h1 = new ListNode(4);
        ListNode h2 = new ListNode(3);
        ListNode h3 = new ListNode(12);
        ListNode h4 = new ListNode(9);
        ListNode h5 = new ListNode(9);
        ListNode h6 = new ListNode(4);
        l1.head = h1;
        h1.next = h2;
        h2.next = h3;
        h3.next = h4;
        h4.next = h5;
        h5.next = h6;
    }

}

Then I extend this in my other class: 然后在另一堂课中扩展它:

public class Tester extends LinkedListStructs{

    public void removeDupsTest(){
      l1.printList();
      l1.removeDups();
      l1.printList();
      }

    }

I didn't initialize a new instance of LinkedListStructs . 我没有初始化LinkedListStructs的新实例。 My question is, when I extend a class, is an instance automatically created? 我的问题是,当我扩展类时,是否会自动创建一个实例?

I am confused because I used LinkedList l1 in my tester class, but the object itself needs to be initialized with a constructor as you see with public LinkedListStructs(){} 我很困惑,因为我在测试器类中使用了LinkedList l1 ,但是对象本身需要使用构造函数进行初始化 ,就像在public LinkedListStructs(){}看到的那样

So, how does it work? 那么它是怎样工作的? If I don't create an instance, initialize properties of linked list then how is it possible to use it? 如果我不创建实例,请初始化链接列表的属性,然后如何使用它?

Thanks 谢谢

To use your Tester , you have to create an instance of it. 要使用Tester ,您必须创建一个实例。 When you create it, you are calling its constructor. 创建它时,您正在调用它的构造函数。

Since you haven't specified a constructor, the compiler creates one for you. 由于您尚未指定构造函数,因此编译器会为您创建一个构造函数。 Your empty constructor will call the superclass constructor. 空的构造函数将调用超类构造函数。

To test this, add an empty constructor to your Tester class and printLns in both the Tester and LinkedListStructs constructor. 要对此进行测试,请将一个空的构造函数添加到Tester类,并在TesterLinkedListStructs构造函数中添加printLns。 You will see the tester constructor called, which in turn will call the superclass constructor. 您将看到tester构造函数被调用,该构造函数又将调用超类构造函数。

Note that your empty constructor must call super(), which calls the superclass constructor. 请注意,您的空构造函数必须调用super(),后者将调用超类构造函数。

Java figures it out for you: when you extend a class that has a parameterless constructor, and do not define a constructor, your derived class automatically gets a default constructor: Java为您解决了这一问题:当扩展具有无参数构造函数的类并且不定义构造函数时,派生类将自动获得默认的构造函数:

JLS section 8.8.9: JLS第8.8.9节:

If a class contains no constructor declarations, then a default constructor with no parameters is automatically provided. 如果类不包含构造函数声明,则会自动提供不带参数的默认构造函数。

If the class being declared is the primordial class Object, then the default constructor has an empty body. 如果要声明的类是原始类Object,则默认构造函数的主体为空。 Otherwise, the default constructor simply invokes the superclass constructor with no arguments. 否则,默认构造函数将简单地调用不带参数的超类构造函数。

When you construct an instance of Tester , the constructor of its base class LinkedListStructs gets called, initializing the list l1 . 在构造Tester的实例时,将调用其基类LinkedListStructs的构造函数,从而初始化列表l1

To answer your doubt: 回答您的疑问:

Since you have extended the LinkedListStructs and created a new class Tester (meaning of 'public class Tester extends LinkedListStructs') 由于您已经扩展了LinkedListStructs并创建了一个新的类Tester(“公共类Tester扩展了LinkedListStructs”的意思)

All the behaviour will be available in the derived(or extended) class. 所有行为将在派生(或扩展)类中可用。 This is the fundamental of Inheritance (and hence reusability). 这是继承的基础(因此是可重用性)。 You may choose to override it as well. 您也可以选择覆盖它。 This is for polymorphism (you can have 'implements' to achive this). 这是针对多态的(您可以使用“实现”来实现)。

Since, there is no explicit constructor in Tester class. 因为,Tester类中没有显式的构造函数。 The default constructor will be called, which in turn will call all its super classes constructor. 默认构造函数将被调用,该构造函数将依次调用其所有超类构造函数。 Default constructor documentation can be found here. 可以在此处找到默认的构造函数文档。

I guess that's the direct answer to your question. 我想那是您问题的直接答案。

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

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