this关键字和super关键字的使用

this关键字

       一、概念

                 this: 存储的“当前对象”的引用
                 this可以访问:本类的成员属性,成员方法,构造方法

        二、this关键字的用法

                1、this访问本类的成员变量

class Student{
    //成员属性
    String name;
    int age;

    //成员方法
    public void method(String name){
        // this访问成员变量:主要是用来区分同名的成员变量和局部变量
        System.out.println("成员变量name的值:" + this.name);
        System.out.println("局部变量name的值:" + name);
    }
}

public class Tests {
    public static void main (String[] args) {
        //创建Student类对象
        Student s = new Student();
        s.name = "张三";
        s.age = 19;

        // 实参赋值给方法中的形参,是该方法的局部参数
        s.method("小张");
    }
}
输出结果
成员变量name的值:张三
局部变量name的值:小张

                   2、this访问本类的成员方法


class Student{
    public void method(){
        System.out.println("Student method");
    }
    public void show(){
        System.out.println("Student show");
        this.method(); //调用本类中的method()方法
        method();
    }
}

public class Tests {
    public static void main (String[] args) {
        // 创建Student类的对象
        Student s = new Student();
        // 调用show方法
        s.show();
    }
}
输出结果:
Student show
Student method
Student method

                3、this访问本类的构造方法

class Student{
    // 成员属性
    String name;
    int age;

    //无参构造方法
    public Student(){
        //调用有参构造方法
        this("小张",18);
        System.out.println("无参构造方法");
    }

    //有参构造方法
    public Student(String name, int age){
        //调用无参
        //this();
        System.out.println("有参构造方法");
        this.name = name;
        this.age = age;
    }
}

public class Tests {
    public static void main (String[] args) {       
        Student s = new Student();
    }
}
输出结果:
有参构造方法
无参构造方法

        总结:

        1.this访问本类的成员变量:就是为了区分成员变量和局部变量

        2.this访问本类的成员方法:这个基本不会使用,因为this本身就代表的是当前类,没有意义

        3.this访问构造本类的方法:

                在构造方法中调用其他构造方法,必须放在构造方法的第一行

                使用this访问本类的构造方法,只能再本类的构造方法中使用

                本类构造方法之间不能同时互相调用

super关键字       

        一、概念

                    存储的“父类对象”的引用
                    super可以访问:父类的成员属性,成员方法,构造方法 

        二、super关键字的用法

                   1.super关键字访问父类成员变量

class Fu{
    int num = 10;
}

class Zi extends Fu{
    int num = 20;
    public void show(){
        int num = 30;
        System.out.println("局部变量" + num); //30
        System.out.println("本类成员变量" + this.num);//20
        System.out.println("父类成员变量" + super.num);//10
    }
}

public class Test {
    public static void main (String[] args) {
        Zi zi = new Zi();
        zi.show();
    }
}
输出结果:
局部变量30
本类成员变量20
父类成员变量10

                2.super关键字访问父类成员方法

class Fu{
    public void method(){
        System.out.println("Fu method");
    }
}

class Zi extends Fu{

    @Override
    public void method () {
        System.out.println("Zi method");
    }
    public void show(){
        //访问本类的method方法
        method();
        // 访问父类的method方法
        super.method();
    }
}

public class Test {
    public static void main (String[] args) {
        Zi zi = new Zi();
        zi.show();
    }
}
输出结果:
Zi method
Fu method

             3、super关键字访问父类构造方法

class Fu{
    private String name;
    private int age;
    public Fu(){
        System.out.println("Fu 无参构造方法");
    }
    public Fu(String name, int age){
        System.out.println("Fu 带参构造方法");
    }

}

class Zi extends Fu{
    public Zi(){
        System.out.println("Zi 无参构造方法");
    }
    public Zi(String name,int age){
        super(name,age);
        System.out.println("Zi 带参构造方法");
    }
}

public class Test {
    public static void main (String[] args) {
        Zi zi1 = new Zi();

        Zi zi2 = new Zi("张三",18);
    }
}
输出结果:
Fu 无参构造方法
Zi 无参构造方法
Fu 带参构造方法
Zi 带参构造方法

             注意:

              1.只能在子类的构造方法中访问父类的构造方法

              2.在子类的构造方法中访问父类的构造方法必须放在第一行

              3.在子类的构造方法中访问父类的构造方法就是为了给从父类继承过来的属性进行初始化

              4.子类的构造方法默认会调用父类的无参构造方法


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值