面向对象基础——this和super关键字(笔记)

this 关键字

public class test {
    public static void main(String[] args) {
        Dog dog1 = new Dog("大壮", 3);
        System.out.println("dog1 的 hashcode=" + dog1.hashCode());
//dog1 调用了 info()方法dog1.info();
        System.out.println("============");
        Dog dog2 = new Dog("大黄", 2);
        System.out.println("dog2 的 hashcode=" + dog2.hashCode());
        dog2.info();
    }
}
class Dog { //类
    String name;
    int age;
// public Dog(String dName, int	dAge){//构造器
//	name = dName;
//	age = dAge;
// }
//如果我们构造器的形参,能够直接写成属性名,就更好了
//但是出现了一个问题,根据变量的作用域原则
//构造器的 name 是局部变量,而不是属性
//构造器的 age	是局部变量,而不是属性
//==> 引出 this 关键字来解决
    public Dog(String name, int age) {//构造器
//this.name 就是当前对象的属性name
     this.name = name;
//this.age 就是当前对象的属性age
     this.age = age;
     System.out.println("this.hashCode=" + this.hashCode());
    }

    public void info() {//成员方法,输出属性 x 信息
        System.out.println("this.hashCode=" + this.hashCode());
        System.out.println(name + "\t" + age + "\t");
    }
}

深入理解this

在这里插入图片描述
在这里插入图片描述

this 的注意事项和使用细节

1)this 关键字可以用来访问本类的属性、方法、构造器
2)this 用于区分当前类的属性和局部变量
3)访问成员方法的语法:this.方法名(参数列表);
4)访问构造器语法:this(参数列表); 注意只能在构造器中使用(即只能在构造器中访问另外一个构造器, 必须放在第一条语句)
5)this 不能在类定义的外部使用,只能在类定义的方法中使用。

super 关键字

基本介绍

super 代表父类的引用,用于访问父类的属性、方法、构造器

基本语法

在这里插入图片描述

super 给编程带来的便利/细节

在这里插入图片描述
在这里插入图片描述
super 和 this 的比较
在这里插入图片描述

public class test {
    public static void main(String[] args) {
        B b = new B();//子类对象
        //b.sum();
        b.test();
    }
}

class B extends A {
    public int n1 = 888;
    //编写测试方法
    public void test() {
        //super的访问不限于直接父类,如果爷爷类和本类中有同名的成员,也可以使用super去访问爷爷类的成员;
        // 如果多个基类(上级类)中都有同名的成员,使用super访问遵循就近原则。A->B->C
        System.out.println("super.n1=" + super.n1);
        super.cal();
    }
    //访问父类的属性 , 但不能访问父类的private属性 [案例]super.属性名
    public void hi() {
        System.out.println(super.n1 + " " + super.n2 + " " + super.n3 );
    }
    public void cal() {
        System.out.println("B类的cal() 方法...");
    }
    public void sum() {
        System.out.println("B类的sum()");
        //希望调用父类-A 的cal方法
        //这时,因为子类B没有cal方法,因此我可以使用下面三种方式

        //找cal方法时(cal() 和 this.cal()),顺序是:
        // (1)先找本类,如果有,则调用
        // (2)如果没有,则找父类(如果有,并可以调用,则调用)
        // (3)如果父类没有,则继续找父类的父类,整个规则,就是一样的,直到 Object类
        // 提示:如果查找方法的过程中,找到了,但是不能访问, 则报错, cannot access
        //      如果查找方法的过程中,没有找到,则提示方法不存在
//        cal();
        this.cal(); //等价 cal

        //找cal方法(super.call()) 的顺序是直接查找父类,其他的规则一样
        super.cal();

        //演示访问属性的规则
        //n1 和 this.n1 查找的规则是
        //(1) 先找本类,如果有,则调用
        //(2) 如果没有,则找父类(如果有,并可以调用,则调用)
        //(3) 如果父类没有,则继续找父类的父类,整个规则,就是一样的,直到 Object类
        // 提示:如果查找属性的过程中,找到了,但是不能访问, 则报错, cannot access
        //      如果查找属性的过程中,没有找到,则提示属性不存在
        System.out.println(n1);
        System.out.println(this.n1);

        //找n1 (super.n1) 的顺序是直接查找父类属性,其他的规则一样
        System.out.println(super.n1);

    }
    //访问父类的方法,不能访问父类的private方法 super.方法名(参数列表);
    public void ok() {
        super.test100();
        super.test200();
        super.test300();
        //super.test400();//不能访问父类private方法
    }
    //访问父类的构造器:super(参数列表);只能放在构造器的第一句,只能出现一句!
    public  B() {
        super("jack");
    }
}

class A extends Base{
    //4个属性
    //public int n1 = 100;
    protected int n2 = 200;
    int n3 = 300;
    private int n4 = 400;

    public A() {}
    public A(String name) {}
    public A(String name, int age) {}

//    public void cal() {
//        System.out.println("A类的cal() 方法...");
//    }

    public void test100() {
    }

    protected void test200() {
    }

    void test300() {
    }

    private void test400() {
    }
}
class Base { //父类是Object

    public int n1 = 999;
    public int age = 111;
    public void cal() {
        System.out.println("Base类的cal() 方法...");
    }
    public void eat() {
        System.out.println("Base类的eat().....");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值