this关键字解析
- this 关键字用来表示
当前对象本身,或当前类的一个实例,通过 this 可以调用本对象的所有方法和属性。
public class ThisDemo {
private static final int x = 10;
private static final int y = 10;
private void sum() {
// 通过 this 点取成员变量
int z = this.x + this.y;
System.out.println("x + y = " + z);
}
public static void main(String[] args) {
ThisDemo obj = new ThisDemo();
obj.sum();
}
}
打印结果:
x + y = 20
上面的程序中,obj 是 ThisDemo 类的一个实例,this 与 obj 等价,执行 int z = this.x + this.y;,就相当于执行 int z = obj.x + obj.y;。
注意:this 只有在类实例化后才有意义。
- this.属性名称,指的是访问类中的成员变量,用来区分成员变量和局部变量(重名问题)
package com.base;
public class Man {
private String name;
private int age;
private String gender;
public Man() {
}
public Man(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void setName(String name) {
name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Man{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
public static void main(String[] args) {
//调用无参构造函数,
Man p1 = new Man();
p1.setAge(20);
p1.setName("张三");
p1.setGender("男");
System.out.println("" + p1.getName() + " 今年" + p1.getAge() + "岁 性别为:" + p1.getGender());
}
}
打印结果:
null 今年20岁 性别为:男
打印的结果返现名字是null,没有赋值,是因为传入的参数变量名与类中属性变量名重复,如下图:

name传入的参数变量名与类中属性变量名重复,其余两个在set方法和有参构造方法中加上了this.类属性名称,这样就可以完成对 对象变量的赋值。
- this(),访问本类的构造方法,()中可以有参数的 如果有参数 就是调用指定的有参构造。(作为方法名来初始化对象)
public class Man {
private String name;
private int age;
private String gender;
public Man() {
this("刘亦菲", 35, "女");
}
public void say() {
System.out.println("明星的名字是" + name + ",今年已经" + age + "岁" + "性别是 " + gender);
}
public Man(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public static void main(String[] args) {
Man obj = new Man();
obj.say();
}
}
this使用注意事项:
- this的语法是“this.”和“this()”;
- this() 不能使用在普通方法中 只能写在构造方法中
- this()只能出现在构造方法的第一行,通过当前的构造方法去调用“本类”中的对应的构造方法,目的是:代码复用。
- 在一个构造方法内只能调用一个构造方法。
- this不能出现在静态方法中;
- this大部分情况下是可以省略的;
- this能出现在实例方法和构造方法中;
- this.什么时候不能省略呢?
在区分局部变量和实例变量时不能省略。例如:
Public void setName(String name){
this.name = name;
}
super关键字
1、super有什么用?
super关键字用于父类的引用,作用是调用父类的构造方法。
- 当子类中构造方法第一行没有super时,会默认第一行有一个super(),它的作用是调用父类的无参构造。
public class Father {
public Father() {
System.out.println("执行父类的构造方法");
}
}
public class Son extends Father {
public Son() {
//这里会有一个默认的super(),会调用父类的无参构造。
System.out.println("执行子类的构造方法");
}
public static void main(String[] args) {
Son son = new Son();
}
}
打印结果:

- (2)如果想要调用父类中的有参构造,可以在子类构造方法第一行加super(形式参数列表),这里的形式参数列表与父类中想要调用的有参构造的形式参数列表相对应。
public class Father {
//无参构造
public Father() {
System.out.println("执行父类无参的构造方法");
}
//有参构造
public Father(int a) {
System.out.println("执行父类有参的构造方法");
}
}
public class Son extends Father {
public Son() {
super(66);//调用有参构造方法
System.out.println("执行子类的构造方法");
}
public static void main(String[] args) {
Son son = new Son();
}
}
执行结果:

- (3)子类中可以通过super.xxx的方式,调用父类型特征(实例变量);通过==super.xxx()==的方式,调用父类型方法(实例方法)。
public class Father {
String name;
public Father(String name) {
this.name = name;
}
}
public class Son extends Father {
public Son(String name) {
super(name);
}
public void shopping(){
System.out.println(super.name + "正在购物!");
}
public static void main(String[] args) {
Son son = new Son("刘亦菲");
son.shopping();
}
}
打印结果:

2、super什么时候不能省略?
super和this很相似,很多时候都可以省略。Java中允许子类中出现和父类一样的同名变量,如果想在子类中访问父类中的同名特征,那么super就不能省略。
public class Father {
String name;
public Father(){
name = "刘亦菲";
}
}
public class Son extends Father {
String name;
public Son(){
name = "李四";
}
public void dosome(){
System.out.println(this.name + "dosome !");
//系统会自动将 name 看作 this.name
System.out.println(name + "dosome !");
System.out.println(super.name + "dosome !");
}
public static void main(String[] args) {
Son son = new Son();
son.dosome();
}
}
打印结果:

3、super使用时的注意事项
- super()表示通过子类的构造方法调用父类的构造方法。模拟现实中这种场景:想要有儿子,必须先有父亲。
- 当一个构造方法第一行既没有this(),又没有super()的话,会默认会有一个super();表示通过当前子类构造方法调用父类的无参数构造方法。所以必须保证父类的无参数构造方法是存在的。
- this()和super()不能共存,都是只能出现在构造方法第一行。
- 父类的构造方法是一定会执行的。
- 在Java语言中,无论new什么对象,Object类中的无参构造一定会执行,并且是处于栈顶(最后被调用,但是最先执行结束,后进先出)。
1671

被折叠的 条评论
为什么被折叠?



