黑马程序员——super和this的区别和应用
来源:互联网 发布:matlab数据导入 编辑:程序博客网 时间:2024/06/10 01:27
------- android培训、java培训、期待与您交流! ----------
在面向对象中super关键字和this关键字的功能有些相似,那么他们之间的区别又是什么
this关键字:代表当前对象的引用, 谁来调用我,我就代表谁
super关键字:代表当前对象父类的引用
this和super的使用区别
1.调用成员变量:
this.成员变量 调用本类的成员变量,也可以调用父类的成员变量
super.成员变量 调用父类的成员变量
2.调用构造方法
this(...) 调用本类的构造方法
super(...) 调用父类的构造方法
3.调用成员方法
this.成员方法 调用本类的成员方法,也可以调用父类的方法
this.成员方法 调用父类的成员方法
this.成员变量 调用本类的成员变量,也可以调用父类的成员变量
super.成员变量 调用父类的成员变量
2.调用构造方法
this(...) 调用本类的构造方法
super(...) 调用父类的构造方法
3.调用成员方法
this.成员方法 调用本类的成员方法,也可以调用父类的方法
this.成员方法 调用父类的成员方法
class Demo {int i;public static void main(String[] args) {Zi z = new Zi();z.test();}}class Fu {int a = 10;int b = 30;public Fu(){System.out.println("父类无参数构造");}public Fu(int i) {System.out.println("父类中有参构造" + i);}public void print(int i) {System.out.println("父类print方法" + i);}public void method() {System.out.println("父类method方法");}}class Zi extends Fu {int a = 20;public Zi() {super();//调用父类当中的无参构造System.out.println("子类无参构造");}public Zi(int i) {super(i);//调用父类当中的有参构造System.out.println("子类有参构造" + i);}public void print(int i) {System.out.println("子类print方法" + i);}public void test() {this.print(this.a);//调用本类当中的print()方法传入的为本类的成员变量a值为20this.print(super.a);//调用本类当中的print()方法传的参数为父类的变量a值为10this.print(this.b);//调用本类当中的print()方法传入的参数为父类的变量b值为30super.print(this.a);//调用父类当中的print()方法传入的参数为本类的变量a值为20this.method();//因为本类当中没有method方法所以调用的是父类当中的method方法super.method();//调用父类的method方法}}
0 0
- 黑马程序员——super和this的区别和应用
- 黑马程序员——JAVA基础之final this.和super.的区别
- 黑马程序员------this和super
- 黑马程序员——Java基础---this和super关键字
- 黑马程序员-super和this关键字
- 黑马程序员--this和super关键字
- 黑马程序员 ---- this 和 super 的属性调用
- 黑马程序员 ---- this 和 super 的方法调用
- super和this的应用
- Super和this的应用
- this和super的区别
- super和this的区别
- super和this的区别
- this和super的区别
- Super和This的区别
- this和super的区别
- this和super的区别
- this和super的区别
- linux进程和线程之间通信方法和同步方法总结
- The Euler function(hdoj2824)(快速求欧拉函数)
- 快速理解RequireJs中的config
- oks3c6410开发板 linux-3.0.1内核 ZC301P摄像头 构成视频监控系统时内核oops解决办法
- 第三十三课——循环语句
- 黑马程序员——super和this的区别和应用
- codeforces Police Recruits
- HDU 4349 && POJ 3219 Lucas有趣应用c
- 数据结构 总结
- POJ 1789 Kruskal
- Linux下Tomcat的启动、关闭
- KMP算法程序
- 51nod贪心例子独木舟问题
- BestCoder Round #65 1001 ZYB's Biology HDU5590