Java中super关键字

定义一个员工类,一个继承员工类的经理类。在主方法中创建一个经理对象,并调用经理类当中的成员方法。

class Employee{
	public void work() {   // 成员方法
		System.out.println("员工在工作");
	}
}

class Manager extends Employee{
	
	//子类重写父类中的work方法
	public void work() {
		System.out.println("经理在工作");
	}
	public void method1() {  // 成员方法
		//this.work();
		work();		
		super.work();
	}
}

public class Test01{
	public static void main(String[] args) {
	Manager m=new Manager();
	m.method1();
}
}

运行结果为:
经理在工作
员工在工作

现在在员工类里创建一个字符串类型名字:

class Employee{
	String name="张三";
	public void work() {   // 成员方法
		System.out.println("员工在工作");
	}
}

class Manager extends Employee{
	//String name="李四";
	//子类重写父类中的work方法
	public void work() {
		System.out.println("经理在工作");
	}
	public void method1() {  // 成员方法
		//this.work();
		work();
		
		super.work();
		
		System.out.println(this.name);
		System.out.println(name);
		System.out.println(super.name);
		
	}
}

public class Test01{
	public static void main(String[] args) {
	Manager m=new Manager();
	m.method1();
}
}

运行结果为:
经理在工作
员工在工作
张三
张三
张三

再在经理类中创建一个字符串类型名字:

class Employee{
	String name="张三";
	public void work() {   // 成员方法
		System.out.println("员工在工作");
	}
}

class Manager extends Employee{
	String name="李四";
	//子类重写父类中的work方法
	public void work() {
		System.out.println("经理在工作");
	}
	public void method1() {  // 成员方法
		//this.work();
		work();
		
		super.work();
		
		System.out.println(this.name);
		System.out.println(name);
		System.out.println(super.name);
		
	}
}

public class Test01{
	public static void main(String[] args) {
	Manager m=new Manager();
	m.method1();
}
}

运行结果为:
经理在工作
员工在工作
李四
李四
张三

1.super不是引用类型,super中存储的不是内存地址,this是。super指向的不是父类对象。
2.super代表的是当前子类对象中父类型特征,如果子类没有显示地又定义了一个这样的特征(属性方法),用不用super也无所谓了(如博客中实例2)。
在这里插入图片描述
3.什么时候使用super?
子类和父类中都有某个数据,例如,子类和父类中都有name这个属性
如果要在子类中访问父类中的name属性,需要使用super.

4.super可以用在什么地方?
第一:可以用在成员方法
第二:可以用在构造方法

5.this和super相同,都不能用在静态方法中

下面我们看一个代码实例,注释中会给出注意事项:

/*
   super关键字用在构造方法中:
   语法:super(实参);
   作用:通过子类的构造方法去调用父类的构造方法。
   语法规则:一个构造方法第一行如果没有this(...),也没有显式调用super(...)
   系统会默认调用super()
   注意:super()的调用只能放在构造方法的第一行
   super()和this()不能共存
   super()调用了父类中的构造方法,但是并不会创建父类对象
   在Java语言中只要是创建Java对象,那么Object中的无参构造方法一定会执行
 */
class Account{
	//Field
	private String actno;
	private double balance;
	
	//Constructor
	public Account() {
		//super();这里其实也有,因为Account继承了Object类
		System.out.println("Account的无参构造方法执行");
	}
	public Account(String acton,double balance) {
		this.actno=acton;
		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 DebitAccount extends Account{
	//Field
	//独特属性
	private double debit;
	
	//Constructor
	public DebitAccount() {
		//super();
	}
	public DebitAccount(String acton,double balance,double debit) {
		//通过子类的构造方法去调用父类的构造方法,作用是:给当前子类对象中的父类型特征赋值
		super(acton,balance);
		//构造方法执行不一定创建对象
		this.debit=debit;
	}
	
    //setter and getter
	public double getDebit() {
		return debit;
	}

	public void setDebit(double debit) {
		this.debit = debit;
	}
		
}
public class Test01{
	public static void main(String[] args) {
		DebitAccount da=new DebitAccount();
	}
}

运行结果是:
Account的无参构造方法执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值