java三大特性之一:继承(什么是super)
2022/1/7 20:03:58
本文主要是介绍java三大特性之一:继承(什么是super),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
super注意点: 1.super调用父类的构造方法,必须在构造方法的第一个 2.super 必须只能出现在子类的方法或者构造方法中 3.super和this不能同时调用构造方法 VS this: 代表的对象不同: this:本身调用者这个对象 super:代表分类对象的应用 前提:this:有没有继承都可以使用 super:只能在继承条件才可以使用 构造方法: this():本类的构造 super():父类的构造 Person类: public class Person {
String name="我是父类"; public void study(){ System.out.println("我是父类"); } public Person() { System.out.println("我是父类的构造方法"); } } Student类:
public class Student extends Person{ public Student() { System.out.println("我是当前类的构造方法"); } String name="我是子类"; public void study(){ System.out.println("我是当前类"); } public void test(String name){ System.out.println(name);// 小明 System.out.println(this.name);//我是子类 System.out.println(super.name);//我是父类 } } Application类:
public class Application { public static void main(String[] args) { Student student = new Student(); // student.test("小明"); } }
这篇关于java三大特性之一:继承(什么是super)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28微服务架构中API版本控制的实践
- 2024-09-28AI给的和自己写的Python代码,都无法改变输入框的内容,替换也不行
- 2024-09-27Sentinel配置限流资料:新手入门教程
- 2024-09-27Sentinel配置限流资料详解
- 2024-09-27Sentinel限流资料:新手入门教程
- 2024-09-26Sentinel限流资料入门详解
- 2024-09-26Springboot框架资料:初学者入门教程
- 2024-09-26Springboot框架资料详解:新手入门教程
- 2024-09-26Springboot企业级开发资料:新手入门指南
- 2024-09-26SpringBoot企业级开发资料新手指南