1 super
1.1 super概述
1、super是一个关键字,全部小写。
2、super和this对比着学习。
this:
this能够出现在实例方法中和构造方法中。
this的语法是:“this.”、“this()”
this不能使用在静态方法中。
this.大部分情况下是可以省略的。
this.什么时候不能省略呢?在区分变量和实例变量的时候不能省略。
public void setName(String name){
this.name = name;
}
this()只能出现在构造方法第一行,通过当前的构造方法去调用“本类”中其它的构造方法,目的是:代码复用。
super:
super能出现在实例方法和构造方法中。
super的语法是:“super.”、“super()”
super也不能使用在静态方法当中。
super大部分情况下也是可以省略的。
super什么情况下不能省略呢?
super()只能出现在构造方法第一行,通过当前的构造方法去调用“父类”中的构造方法,目的是:创建子类对象的时候,先初始化父类特征。
3、super()
表示通过子类的构造方法调用父类的构造方法。
模拟现实世界中的这种场景:要想有儿子,需要先有父亲。
4、重要结论:
当一个构造方法既没有this()又没有super()的话,默认会有一个super();
表示通过当前子类的构造方法调用父类的无参数构造方法。
所以必须保证父类的无参数构造方法是存在的。
5、注意:
this()和super()不能共存,它们都是只能出现在构造方法第一行。
public class SuperTest01{
public static void main(String[] args){
new B();
}
}
class A{
public A (int i){
System.out.println("A类的有参数构造方法(int)");
}
}
class B extends A{
public B(){
this("zhangsan");
System.out.println("B类的无参构造方法");
}
public B(String name){
super();
System.out.println("B类的有参数构造方法(String)");
}
}
1.2 子类构造执行时必然调用父类的构造方法
public class SuperTest02{
public static void main(String[] args){
new C();
}
}
class A{
public A(){
System.out.println("A类的无参数构造执行");
}
}
class B extends A{
public B(){
super();
System.out.println("B类的无参数构造执行");
}
public B(String name){
System.out.println("B类的有参数构造执行(String)");
}
}
class C extends B{
public C(){
this("zhangsan");
System.out.println("C类的无参数构造执行");
}
public C(String name){
this(name, 20);
System.out.println("C类的有参数构造执行(String)");
}
public C(String name, int age){
super(name);
System.out.println("C类的有参数构造执行(String, int)");
}
}
1.3 super(实参)的用法1
public class SuperTest03{
public static void main(String[] args){
CreditAccount ca1 = new CreditAccount();
System.out.println(ca1.getActno() + "," + ca1.getBalance() + "," + ca1.getCredit());
CreditAccount ca2 = new CreditAccount("111111", 10000000.0, 0.99);
System.out.println(ca2.getActno() + "," + ca2.getBalance() + "," + ca2.getCredit());
}
}
class Account{
private String actno;
private double balance;
public Account(){}
public Account(String actno, double balance){
this.actno = actno;
this.balance = balance;
}
public void setActno(String actno){
this.actno = actno;
}
public String getActno(){
return actno;
}
public void setBalance(double balance){
this.balance = balance;
}
public double getBalance(){
return balance;
}
}
class CreditAccount extends Account{
private double credit;
public CreditAccount(){
}
public CreditAccount(String actno, double balance, double credit){
super(actno, balance);
this.credit = credit;
}
public void setCredit(double credit){
this.credit = credit;
}
public double getCredit(){
return credit;
}
}
![]()
1.4 super(实参)的用法2
public class SuperTest04{
public static void main(String[] args){
Vip v = new Vip("张三");
v.shopping();
}
}
class Customer{
String name;
public Customer(){}
public Customer(String name){
this.name = name;
}
}
class Vip extends Customer{
public Vip(){}
public Vip(String name){
super(name);
}
public void shopping(){
System.out.println(this.name + "正在购物");
System.out.println(super.name + "正在购物");
System.out.println(name + "正在购物");
}
}
![]()
1.5 "super."什么时候不能省略
public class SuperTest04{
public static void main(String[] args){
Vip v = new Vip("张三");
v.shopping();
}
}
class Customer{
String name;
public Customer(){}
public Customer(String name){
this.name = name;
}
public void doSome(){
System.out.println(this.name + "do some");
System.out.println(name + "do some");
}
}
class Vip extends Customer{
String name;
public Vip(){}
public Vip(String name){
super(name);
}
public void shopping(){
System.out.println(this.name + "正在购物");
System.out.println(super.name + "正在购物");
System.out.println(name + "正在购物");
}
}
![]()
1.6 super使用时后面必须有个点
public class SuperTest06{
public void doSome(){
System.out.println(this);
}
public static void doOther(){
System.out.println(this);
System.out.println(super.xxx);
}
public static void main(String[] args){
SuperTest06 st = new SuperTest06();
st.doSome();
}
}
1.7 使用super调用父类的方法
public class SuperTest07{
public static void main(String[] args){
Cat c = new Cat();
c.yiDong();
}
}
class Animal{
public void move(){
System.out.println("Animal move!");
}
}
class Cat extends Animal{
public void move(){
System.out.println("Cat move!");
}
public void yiDong(){
this.move();
move();
super.move();
}
}
1.8 super关键字
super能出现在实例方法和构造方法中。
super的语法是:“super.”、“super()”
super不能使用在静态方法中。
super. 大部分情况下是可以省略的。
父类和子类中有同名属性,或者说有同样的方法,
想在子类中访问父类的,super. 不能省略。
super() 只能出现在构造方法第一行,通过当前的构造方法去调用“父类”中的构造方法,目的是:创建子类对象的时候,先初始化父类型特征。
super的使用:
super.属性名【访问父类的属性】
super.方法名(实参) 【访问父类的方法】
super(实参)【调用父类的构造方法】
2 day16作业