super关键字

super关键字

**super.属性名			【访问父类的属性】
  super.方法名(实参)	【访问父类的方法】
  super(实参)			【调用父类的构造方法】

1)、super能出现在实例方法和构造方法当中。
2)、super的语法是:“super()”、“super.”
3)、super不能使用在静态方法当中。
4)、super大部分情况下是可以省略的。

5)、super.什么时候不能省略?
父中有,子中又有,如果想在子中访问“父的特征”,super不能省略。

6)、super()只能出现在构造方法第一行,通过当前的构造方法去调用“父类”中的构造方法,目的是:创建子类对象的时候,先初始化父类型特征。
例:

package java核心技术;

public class SuperTest02 {

	public static void main(String [] args) {
		CreditAccount c1 = new CreditAccount();
		System.out.println(c1.getActno() + "," + c1.getBalance() + "," + c1.getCredit() );
		CreditAccount c2 = new CreditAccount("1111", 10000.0, 99);
		System.out.println(c2.getActno() + "," + c2.getBalance() + "," + c2.getCredit() );
		
	}
}

class Account{
	//账号
	private String actno;
	//余额
	private double balance;
	
	public Account() {
		
	}
	
	public Account(String actno, double balance) {
		this.actno = actno;
		this.balance = balance;
	}

	public String getActno() {
		return actno;
	}

	public void setActno(String actno) {
		this.actno = actno;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = 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 double getCredit() {
		return credit;
	}

	public void setCredit(double credit) {
		this.credit = credit;
	}
	
}
执行结果:
null,0.0,0.0
1111,10000.0,99.0

7)、当一个构造方法第一行,既没有this()有没有super()的话,默认有一个super方法。所以必须保证父类的无参构造方法存在。

8)、this()和super()不能共存,它们都是只能出现在构造方法第一行

关于this()与super()在程序中的使用,用以下这个例子作为说明:

package java核心技术;

public class SuperTest01 {

	public static void main(String [] args) {
		new C();
	}
}

class A{
	public A() {
		System.out.println("A类的无参构造方法执行!");
	}
}

class B extends A {
	public B() {
		System.out.println("B类的无参构造方法执行!");
	}
	
	public B(String name) {
		super();
		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)");
	}
}
执行结果:
A类的无参构造方法执行!
B类的有参构造执行(String)
C的有参构造执行(String, int)
C的有参构造执行(String)
C的无参构造方法执行!

9)、无论怎样,父类的构造方法一定会执行。

10)、super不是引用,super不保存内存地址,也不指向任何对象。只代表当 前对象内部的那一块父类型的特征。

System.out.println(super);//编译错误,需要“.”
System.out.println(this);//编译通过,输出一个地址

11)、super.不仅可以访问属性,也可以访问方法

package java核心技术;

public class SuperTest05 {

	public static void main(String [] args) {
		Cat1 c = new Cat1();
		c.yiDong();
	}
}

class Animal{
	public void move() {
		System.out.println("Animal move!");
	}
}

class Cat1 extends Animal{
	public void move() {
		System.out.println("Cat move!");
	}
	
	public void yiDong() {
		this.move();
		move();
		super.move();
	}
}
运行结果:
Cat move!
Cat move!
Animal move!

关于调用super.与this.的输出情况
请仔细对比阅读以下两个例子:

package java核心技术;

public class SuperTest03 {

	public static void main(String [] args) {
		Vip v = new Vip("zhangsan");
		v.shopping();
	}
}

class Customer{
	String name;
	
	public Customer() {
		
	}
	
	public Customer(String name) {
		super();
		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);
	}
}
执行结果:
zhangsan
zhangsan
zhangsan
package java核心技术;

public class SuperTest04 {
	public static void main(String [] args) {
		Vip1 v = new Vip1("zhangsan");
		v.shopping();
	}
}

class Customer1{
	String name;
	
	public Customer1() {
		
	}
	
	public Customer1(String name) {
		super();
		this.name = name;
	}
}

class Vip1 extends Customer1{
	String name;
	
	public Vip1() {
	}
	
	public Vip1(String name) {
		super(name);
	}
	
	/*
	 * java是怎么区分子类和父类的同名属性的?
	 * 		this.name:当前对象的name属性
	 * 		super.name:当前对象的父类型特征中的name属性
	 */
	
	public void shopping() {
		System.out.println(this.name);
		System.out.println(super.name);
		System.out.println(name);
	}
}
执行结果:
null
zhangsan
null
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值