Java中的this关键字和super关键字的用法

tihs

this知识点

1、this是一个关键字,是一个引用,保存内存地址指向自身。

2、this可以使用在实例方法中,也可以使用在构造方法中。

3、this出现在实例方法中其实代表的是当前对象。

4、this不能使用在静态方法中。

5、this. 大部分情况下可以省略,但是用来区分局部变量和实例变量的时候不能省略。

6、this() 这种语法只能出现在构造方法第一行,表示当前构造方法调用本类其他的
构造方法,目的是代码复用。

this的用法

(1)this.方法名称
用来访问本类的成员方法
例:

public class Student{
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;// 用来访问本类的成员方法
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

(2)this();
访问本类的构造方法
()中可以有参数的 如果有参数 就是调用指定的有参构造
例:

		public Student() {
            this("wang", 18);//访问本类的构造方法
        }

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

super

super知识点

1、super能出现在实例方法和构造方法中。

2、super的语法是:“super.”、“super()”

3、super不能使用在静态方法中。

4、super. 大部分情况下是可以省略的。
super.什么时候不能省略呢?
父类和子类中有同名属性,或者说有同样的方法,
想在子类中访问父类的,super. 不能省略。

5、super() 只能出现在构造方法第一行,通过当前的构造方法去调用“父类”中
的构造方法,目的是:创建子类对象的时候,先初始化父类型特征。

super的用法

(1)super.属性名 【访问父类的属性】
(2)super.方法名(实参) 【访问父类的方法】
(3)super(实参) 【调用父类的构造方法】

例:

public static class A {
        private String id;
        public A() {
            System.out.println("A类的无参构造方法");
        }

        public A(String id) {
            this.id = id;
            System.out.println("A类的有参构造方法");
        }
        public int add(int x, int y){
            return x + y;
        }
    }

    public static class B extends A{
        public B() {
            super();//调用父类的构造方法
            System.out.println("B类的无参构造方法");
        }

        public B(String id) {
            super(id);//调用父类的构造方法
            System.out.println("B类的有参构造方法");
        }
        public void shopping(){
            System.out.println(super.id + "在购物");//访问父类的属性
        }
        public int badd(int a, int b){
            return super.add(a, b);//访问父类的方法
        }
    }
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        A a1 = new A("a");
        B b1 = new B("b");
        b1.shopping();`在这里插入代码片`
        int result = b1.badd(1, 2);
        System.out.println(result);
    }

result:
在这里插入图片描述

总代码:

public class This_and_Supper {
    public static class Student{
        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Student() {
            this("wang", 18);
        }

        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public void print(){
            System.out.println("学生的姓名为:" + this.name +", 学生的年龄为:" + this.age);
        }
    }

    public static class A {
        private String id;
        public A() {
            System.out.println("A类的无参构造方法");
        }

        public A(String id) {
            this.id = id;
            System.out.println("A类的有参构造方法");
        }
        public int add(int x, int y){
            return x + y;
        }
    }

    public static class B extends A{
        public B() {
            super();
            System.out.println("B类的无参构造方法");
        }

        public B(String id) {
            super(id);
            System.out.println("B类的有参构造方法");
        }
        public void shopping(){
            System.out.println(super.id + "在购物");
        }
        public int badd(int a, int b){
            return super.add(a, b);
        }
    }
    public static void main(String[] args) {
        Student student = new Student();
        student.print();
        A a = new A();
        B b = new B();
        A a1 = new A("a");
        B b1 = new B("b");
        b1.shopping();
        int result = b1.badd(1, 2);
        System.out.println(result);
    }
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值