java关键字——this与super的对比学习

223 阅读5分钟

一、概述

this表示当前类所指对象,super表示父类对象的引用

 

this关键字
this当前类所指对象
this.字段名 表示当前类字段,此处this可以省略
this.方法名()  调用当前类方法,此处this可以省略

 

super关键字
super()调用父类无参构造函数,仅在子类构造函数中出现,可以省略,父类无参构造函数可以隐式调用
super(参数)调用父类指定参数构造函数,仅在子类构造函数中出现,注意不可省略,父类带参构造函数无法隐式调用
super.字段名子类中访问父类字段,用super.避免与子类同名字段混淆
super.方法名()子类中调用父类函数,用super.避免与子类相同函数签名的函数混淆

二、代码分析

2.1 父类构造函数和子类构造函数 

代码示意:

public class Test_thisAndSuper {

	public static void main(String[] args) {
		System.out.println("=====无参构造函数=====");
		new BallGame();
		System.out.println("=====带参构造函数=====");
		new BallGame("basketBall");
	}

}

class Game {
	public Game() {
		System.out.println("Game non-parameter constructor");
	}

	public Game(String name) {
		System.out.println("Game parameter constructor: " + name);
	}

}

class BallGame extends Game {
	public BallGame() {
		super();// 此行代码 可有可无 Java中,子类构造函数可以隐式调用父类无参构造函数
		System.out.println("BallGame non-parameter constructor");
	}

	public BallGame(String name) {
		super(name);
		System.out.println("BallGame parameter constructor: " + name);
	}
}

 输出结果:

=====无参构造函数=====
Game non-parameter constructor
BallGame non-parameter constructor
=====带参构造函数=====
Game parameter constructor: basketBall
BallGame parameter constructor: basketBall

 2.2 父类字段和子类字段

代码示意:

public class Test_thisAndSuper {

	public static void main(String[] args) {
		System.out.println("=====无参构造函数=====");
		BallGame _ballGame_noParameter=new BallGame();
		System.out.println("=====带参构造函数=====");
		new BallGame("basketBall");
		System.out.println("=====打印字段=====");
		_ballGame_noParameter.displayField();
	}

}

class Game {
	protected String _description="This is an interesting game";
	public Game() {
		System.out.println("Game non-parameter constructor");
	}

	public Game(String name) {
		System.out.println("Game parameter constructor: " + name);
	}

}

class BallGame extends Game {
	protected String _description="This is an interesting ballgame";
	public BallGame() {
		super();// 此行代码 可有可无 Java中,子类构造函数可以隐式调用父类无参构造函数
		System.out.println("BallGame non-parameter constructor");
	}

	public BallGame(String name) {
		super(name);
		System.out.println("BallGame parameter constructor: " + name);
	}
	
	public void displayField(){
		System.out.println("父类字段: "+super._description);
		System.out.println("子类字段: "+this._description);
	}
}

输出结果:

=====无参构造函数=====
Game non-parameter constructor
BallGame non-parameter constructor
=====带参构造函数=====
Game parameter constructor: basketBall
BallGame parameter constructor: basketBall
=====打印字段=====
父类字段: This is an interesting game
子类字段: This is an interesting ballgame

2.3 父类方法和子类方法

代码示意:

public class Test_thisAndSuper {

	public static void main(String[] args) {
		System.out.println("=====无参构造函数=====");
		BallGame _ballGame_noParameter=new BallGame();
		System.out.println("=====带参构造函数=====");
		new BallGame("basketBall");
		System.out.println("=====打印字段=====");
		_ballGame_noParameter.displayField();
		System.out.println("=====打印方法调用=====");
		_ballGame_noParameter.displayFunction();
	}

}

class Game {
	protected String _description="This is an interesting game";
	public Game() {
		System.out.println("Game non-parameter constructor");
	}

	public Game(String name) {
		System.out.println("Game parameter constructor: " + name);
	}
protected void  playing() {
	System.out.println("This game is so interesting");
}
}

class BallGame extends Game {
	protected String _description="This is an interesting ballgame";
	public BallGame() {
		super();// 此行代码 可有可无 Java中,子类构造函数可以隐式调用父类无参构造函数
		System.out.println("BallGame non-parameter constructor");
	}

	public BallGame(String name) {
		super(name);
		System.out.println("BallGame parameter constructor: " + name);
	}
	
	public void displayField(){
		System.out.println("父类字段: "+super._description);
		System.out.println("子类字段: "+this._description);//此处this可以省略
	}
	
	protected void  playing() {
		System.out.println("This ballgame is so interesting");
	}
	
	public void displayFunction(){
		System.out.print("父类方法: ");
		super.playing();
		System.out.print("子类方法: ");
		this.playing();//此处this可以省略
	}
}

输出结果:

=====无参构造函数=====
Game non-parameter constructor
BallGame non-parameter constructor
=====带参构造函数=====
Game parameter constructor: basketBall
BallGame parameter constructor: basketBall
=====打印字段=====
父类字段: This is an interesting game
子类字段: This is an interesting ballgame
=====打印方法调用=====
父类方法: This game is so interesting
子类方法: This ballgame is so interesting

2.4 this和super同时出现

关于子类构造函数中可以同时出现super和this关键字,但是super和this不能同时调用构造函数:

代码示意:

public class Test_thisAndSuper {

	public static void main(String[] args) {
		System.out.println("=====无参构造函数=====");
		BallGame _ballGame_noParameter=new BallGame();
		System.out.println("=====带参构造函数=====");
		new BallGame("basketBall");
		System.out.println("=====打印字段=====");
		_ballGame_noParameter.displayField();
		System.out.println("=====打印方法调用=====");
		_ballGame_noParameter.displayFunction();
		
	}

}

class Game {
	protected String _description="This is an interesting game";
	public Game() {
		System.out.println("Game non-parameter constructor");
	}

	public Game(String name) {
		System.out.println("Game parameter constructor: " + name);
	}
	

protected void  playing() {
	System.out.println("This game is so interesting");
}
}

class BallGame extends Game {
	protected String _description="This is an interesting ballgame";
	public BallGame() {
		super();// 此行代码 可有可无 Java中,子类构造函数可以隐式调用父类无参构造函数
		System.out.println("BallGame non-parameter constructor");
	}

	public BallGame(String name) {
		super(name);
		System.out.println("BallGame parameter constructor: " + name);
	}
	public BallGame(String test1,String test2){
		//super(test1);
		//this(test1);  super()和this()都要放在第一行  自相矛盾  编译时报错
		//小结:super和this可以同时出现,即使是在子类构造方法中也可以同时出现,但是不同同时调用构造方法,
		
		//理论解释:因为在一个构造方法中,只能访问一次其它的构造方法(不管是父类的还是子类的)所以此时不能同时用super和this关键字来调用构造方法,只能根据需求选其一。
		//简单理解:调用构造函数时,两个都要放第一,自相矛盾
	}
	public void displayField(){
		System.out.println("父类字段: "+super._description);
		System.out.println("子类字段: "+this._description);//此处this可以省略
	}
	
	protected void  playing() {
		System.out.println("This ballgame is so interesting");
	}
	
	public void displayFunction(){
		System.out.print("父类方法: ");
		super.playing();
		System.out.print("子类方法: ");
		this.playing();//此处this可以省略
	}
}

输出结果:

因不可同时调用父类构造函数和子类构造函数,无输出结果

小结:

1、super和this可以同时出现,即使是在子类构造方法中也可以同时出现,但是不同同时调用构造方法,
2、理论解释:因为在一个构造方法中,只能访问一次其它的构造方法(不管是父类的还是子类的)所以此时不能同时用super和this关键字来调用构造方法,只能根据需求选其一。
3、简单理解:调用构造函数时,两个都要放第一,自相矛盾