super和this的区别

 
参考博客:https://www.cnblogs.com/hasse/p/5023392.html 

                  http://blog.csdn.net/zuoyang1990/article/details/53471494

this

this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针。

this的用法在java中大体可以分为3种:

 1、普通的直接引用

          

/**
 * 当在一个类中明确指出使用对象自己的变量或函数时需加上this的引用
 *
 */
class Hello{
	String str = "hello";
	public Hello(String s){
		System.out.println("s = " + s);
		System.out.println("1 -> this.s = " + this.str);
		// 当在一个类中明确指出使用对象自己的变量或函数时需加上this的引用
		this.str = s;
		System.out.println("2 -> this.s = " + this.str);
	}
}

public class test1{
	
	public static void main(String[] args) {

		Hello h = new Hello("word");
		
		
	}

}

运行结果为:

s = word
1 -> this.s = hello
2 -> this.s = word


 2、形参与成员名字重名,用this来区分

class Hello1{
	String name = "hello";
	public void setName(String name){
		this.name = name;
	}
	
	public String getName(){
		return this.name;
	}
}


public class test2 {

	public static void main(String[] args) {

		Hello1 h = new Hello1();
		
		h.setName("word");
		System.out.println(h.getName());	
	}
}


运行结果为:

word

3、引用构造函数

这个和super放在一起讲

super

super可以理解为指向自己超(父)类对象的一个指针,而这个超类指的是离自己最近的一个父类。

super也有三种用法

1、普通的直接引用

与this相似,super相当于是指向当前对象的父类,这样就可以用super.xxx来引用父类的成员

class Animal{
	private String member1 = "member1";
	protected String member2 = "member2";
	public String member3 = "member3";
	String member4 = "member4";
	
	public Animal(){
		
	}
}

class Cat extends Animal{
	public Cat(){
		// System.out.println(super.member1); 不能访问标为private的成员
		System.out.println(super.member2);
		System.out.println(super.member3);
		System.out.println(super.member4);//如果子类与父类在同一包内,可以访问默认(无修饰符)的成员变量与方法。
	}
}

public class test3 {
	public static void main(String[] args) {
		Cat c = new Cat();
	}
}




运行结果为:

member2member3member4

2、子类中的成员变量或方法与父类中的成员变量或方法重名

class Animal{
	
	String name = "animals";
	void value(){
		name = "Animal";
	}
}

class Dog extends Animal{
	
	String name;
	void value(){
		name = "Dog";
		super.value();
		System.out.println(name);
		System.out.println(super.name);
	}
	
}
public class test4 {
	public static void main(String[] args) {
		Dog d = new Dog();
		d.value();
	}
}

运行结果:

Dog

Animal

可以看到,这里既调用了父类的方法,也调用了子类的变量。若不调用父类方法value(),只调用父类变量name的话,则父类的name值为animals


3、引用构造函数

super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句)

this(参数):调用本类中另一种形式的构造函数(应该为构造函数中的第一条语句)

/**
 * super()函数在子类构造函数中调用父类的构造函数时使用,而且必须要在构造函数的第一行
 * 调用父类的方法
 */
class Animal{
	public Animal(){
		System.out.println("父类无参构造函数:An animal");
	}

	public Animal(String name){
		System.out.println("父类有参构造函数:"+name);
	}

	void getName(String name){
		System.out.println("父类:this is " + name);
	}
}
class Dog extends Animal{

	public Dog(){
		// super()函数在子类构造函数中调用父类的构造函数时使用,而且必须要在构造函数的第一行
		// 如果子类构造函数中没有写super()函数,编译器会自动帮我们添加一个无参数的super()
		
		// 1、2、3、4只能选其一,且必须在构造函数第一行
		super();//1:调用父类无参构造函数
//		super("animals");//2:调用父类相应有参构造函数
//		
//		this();//3:调用子类本身的无参构造函数
//		this("animals");//4:调用子类本身相应的有参构造函数
		
		//super.getName("animals");//调用父类的方法
		this.getName("animals");//若子类重写了此方法则调用子类本身的方法,若子类未重写,则调用父类的方法
		System.out.println("A Dog");
	}
	
	void getName(String name){
		System.out.println("子类:this is " + name);
	}
}

public class Main {
	
	public static void main(String[] args) {
		Dog dog = new Dog();
	}

}

输出结果为

父类无参构造函数:An animal
子类:this is animals
A Dog


super和this的异同:

1、super(参数):调用基类中的另一个构造函数(应该为构造函数的第一条语句)

2、this(参数):调用本类中另一种形式的构造函数(应该为构造函数的第一条语句)

3、super:它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时:super.变量名,super.成员函数数据名(实参))

4、this:它代表当前对象名(在程序中易产生二义性之处,应使用this来指明当前对象;如果函数的形参与类中的成员数据同名,这时需用this来指明成员变量名)

5、调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在遍历的时候就会报错

6、super()和this()类似,区别是,super()从子类中调用父类的构造方法,this()在同一类内调用其它方法

7、super()和this()均需放在构造方法内第一行

8、尽管可以用this调用一个构造器,但却不能调用两个

9、this和super不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有super语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过

10、this()和super()都指的是对象,所以,均不可以在static环境中使用。包括static变量、static方法、static语句块

11、从本质上讲,this是一个指向本对象的指针,然而super是一个Java关键字





  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值