In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use super keyword in Java". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
What's the use of super?
(1)When there is no super in the first line of the constructor method in the subclass, there will be a super() in the first line by default, which is used to call the parameterless constructor of the parent class.
Example:
class A{ public A(){ System.out.println("Parent constructor execution! "); }}public class superTest extends A{ public superTest(){ //There will be a default super() that calls the parameterless constructor of the parent class. System.out.println("subclass constructor execution! "); } public static void main(String[] args){ superTest s = new superTest(); }}
The results are as follows:
(2)如果想要调用父类中的有参构造,可以在子类构造方法第一行加super(形式参数列表),这里的形式参数列表与父类中想要调用的有参构造的形式参数列表相对应。
例:
class A{ //无参构造 public A(){ System.out.println("父类无参构造方法执行!"); } //有参构造 public A(int a){ System.out.println("父类有参构造方法执行!"); }}public class superTest extends A{ public superTest(){ super(100); System.out.println("子类构造方法执行!"); } public static void main(String[] args){ superTest s = new superTest(); }}
执行结果如下:
(3)子类中可以通过super.xxx的方式,调用父类型特征(实例变量);通过==super.xxx()==的方式,调用父类型方法(实例方法)。
class A{ String name; public A(String name){ this.name = name; }}public class superTest extends A{ public superTest(String name){ super(name); } public void shopping(){ System.out.println(super.name + "正在购物!"); } public static void main(String[] args){ superTest s = new superTest("张三"); s.shopping(); }}
运行结果如下:
super什么时候不可以省略呢?
super和this很相似,很多时候都可以省略。Java中允许子类中出现和父类一样的同名变量,如果想在子类中访问父类中的同名特征,那么super就不能省略。
class A{ String name; public A(){ name = "张三"; }}public class superTest extends A{ String name; public superTest(){ name = "李四"; } public void dosome(){ System.out.println(this.name + "dosome !"); //系统会自动将 name 看作 this.name System.out.println(name + "dosome !"); System.out.println(super.name + "dosome !"); } public static void main(String[] args){ superTest s = new superTest(); s.dosome(); }}
运行结果如下:
super在内存图中是如何存在的呢?
以上面的代码为例,画出了大概的内存图,如下:
super使用时的注意事项
(1)super()表示通过子类的构造方法调用父类的构造方法。模拟现实中这种场景:想要有儿子,必须先有父亲。
(2)当一个构造方法第一行:
既没有this(),又没有super()的话,会默认会有一个super();
表示通过当前子类构造方法调用父类的无参数构造方法。
所以必须保证父类的无参数构造方法是存在的。
(3)this()和super()不能共存,都是只能出现在构造方法第一行。
(4)父类的构造方法是一定会执行的。
(5)在Java语言中,无论new什么对象,Object类中的无参构造一定会执行,并且是处于栈顶(最后被调用,但是最先执行结束,后进先出)。
"Java中super关键字怎么用"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.