super的使用方法与使用范围
如果你了解,用this是调用一个类里面的变量或者对象方法。那么super你可以理解为调用多态或者继承类中的构造方法和对象方法。在super调用构造方法时,只能调用带参的构造方法,这也是唯一调用其他类里面的构造方法的显性调用。不带参的父类构造方法可以在执行实例化子类时直接被隐性调用。
例子1:
class cam0
{
double size=10;
void printsize()
{
System.out.println(size);
}
}
class cam1 extends cam0
{
double size=20;
void printsize()
{
System.out.println(size);
System.out.println(super.size);
super.printsize();
}
}
public class cam2
{
public static void main(String args[])
{
cam1 m=new cam1();
m.printsize();
}
}
结果:
输出为
20.0
10.0
10.0
super调用m的父类cam0的成员和方法
super可以调用当前对象的父类对象的构造函数
注意: 子类调用父类的构造方法时,super语句必须是子类构造方法的第一句
例子2:
[java] view plain copy
class cam0
{
public cam0(int a,int b)
{
System.out.println("CS");
}
}
class cam1 extends cam0
{
public cam1()
{
super(1,1);
System.out.println("bbb");
}
public cam1(int a)
{
super(1,1);
System.out.println("ccc");
}
}
public class cam2
{
public static void main(String args[])
{
cam1 a=new cam1();
cam1 b=new cam1(1);
}
}
输出为
CS
bbb
CS
ccc
对象a,b分别调用了父类cam0的构造函数
super可以把当前对象的父类对象的引用作为参数传递给其他方法
这个用法和this把当前对象的引用作为参数传递给其他方法相似,这里就不赘述了
super的使用方法与使用范围的更多相关文章
- python中super的使用方法
说白了,super的使用就是要子类要调用父类的方法,我们就用super,那你要有调用的规范,我们明白这个规范就可以了. 在python2和python3中,调用方法不同,注意就是了.Python3.x ...
- Python中super()和__init__()方法
采用新式类,要求最顶层的父类一定要继承于object,这样就可以利用super()函数来调用父类的init()等函数, 每个父类都执行且执行一次,并不会出现重复调用的情况.而且在子类的实现中,不用到处 ...
- super.getClass()方法调用
下面程序的输出结果是多少?import java.util.Date;public class Test extends Date{public static void main(String[] a ...
- super.getClass()方法
下面程序的输出结果是多少? importjava.util.Date; public class Test extends Date{ public static void main(String[] ...
- 【Java面试题】14 super.getClass()方法调用
下面程序的输出结果是多少? import java.util.Date; public class Test extends Date{ public static void main(String[ ...
- es6 class 中 constructor 方法 和 super
首先,ES6 的 class 属于一种“语法糖”,所以只是写法更加优雅,更加像面对对象的编程,其思想和 ES5 是一致的. <1>constructor function Point(x, ...
- ES6之class 中 constructor 方法 和 super 的作用
首先,ES6 的 class 属于一种“语法糖”,所以只是写法更加优雅,更加像面对对象的编程,其思想和 ES5 是一致的. function Point(x, y) { this.x = x; thi ...
- Java中super的几种使用方法并与this的差别
1. 子类的构造函数假设要引用super的话,必须把super放在函数的首位. class Base { Base() { System.out.println("Base" ...
- 子类中执行父类的方法(引出super()与mro列表)
1. 我们先想一下在python中如果子类方法中想执行父类的方法,有什么方式?大概有三种: Parent.__init__(self, name) # 通过父类的名字,指定调用父类的方法 super( ...
随机推荐
- Shell中怎么获取当前日期和时间
转载自:https://zhidao.baidu.com/question/627912810044012524.html 获得当天的日期 [root@master ~]# date +%Y-%m-% ...
- 三重DEC加密在java中的实现
代码可以直接拷走使用,一些约定例如向量可以自行变动 引言 如今手机app五彩缤纷,确保手机用户的数据安全是开发人员必须掌握的技巧,下面通过实例介绍DES在android.ios.java平台 ...
- 文件上传失败 -nginx报错 client intended to send too large body: 1331696 bytes
location / { root /data/fastdfs/data; include gzip.conf; ngx_fastdfs_module; client_max_body_size 10 ...
- WyBox 7620a 启用第二个串口
要修改的文件有两个 mt7620a.dtsi MT7620a.dts 1.进入target/linux/ramips/dts/ mt7620a.dtsi 把”disabled”改为”ok”,添加两行 ...
- datax 数据同步迁移
https://github.com/alibaba/DataX/blob/master/mysqlwriter/doc/mysqlwriter.md https://github.com/aliba ...
- export,import 的用法
摘自https://blog.csdn.net/pcaxb/article/details/53670097: 谢谢!
- PowerPoint’s Menu is Too Big
转自: http://jdav.is/2016/08/31/powerpoints-menu-is-too-big/ It seems that when Microsoft deployed the ...
- 【git】之修改git仓库地址
方法1 git remote set-url origin <url> 方法2 git remote rm origin git remote add origin [url] 方法三 直 ...
- 51nod 1132 覆盖数字的数量 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1132 题意是给定a,b,l,r求[l,r]内有几个整数可以表示成ax+b ...
- 其他类想使用unittest的断言方法,就import unittest的框架,继承他,使用他里面的方法
在断言层 也可以同样用这个方法