Super()关键字...可以用于调用一个以上的超类构造函数吗? [英] Super() keyword... Can it be used to call a constructor more than one superclass up?

查看:122
本文介绍了Super()关键字...可以用于调用一个以上的超类构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我缺少某些东西,或者某些东西没有为我点击.我正在读一本关于Java的书,而我刚刚了解了super().当用来获取构造函数时,它是从一个超类中获取构造函数的,对吗?如果您想上两个超类怎么办?

有些事情不在我的脑海中,我希望这个问题能帮助我将各个部分放在一起.

解决方案

您不能再上两个级别.您只能决定给您的父母打电话.然后,该类负责依次调用其父级.

请注意,所有构造函数(Object中的根构造函数除外)都调用超级构造函数.如果您没有明确指定它,或者如@PaulBellora所添加的那样,第一个语句是对您的类的另一个构造函数的this()调用,则编译器会在每个构造函数中插入一个super()调用作为第一个语句.

I think I'm missing something, or something isn't clicking for me. I'm reading a book on java, and I just learned about super(). When used to get a constructor, it gets the constructor from one superclass up, correct? What if you wanted to go two superclasses up, how would that be done?

Something's just not sitting right in my brain, and I'm hoping this question will help me put the pieces together.

解决方案

You can't go two levels up. You can only decide to call your parent. That class is then responsible for calling its parent in turn.

Note that all constructors (except for the root constructors in Object) call a super constructor. If you don't specify it explicitly, or, as @PaulBellora adds, the first statement is a this() call to another constructor of your class, the compiler inserts a super() call as the first statement in every constructor.

这篇关于Super()关键字...可以用于调用一个以上的超类构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆