2021-04-26

目录

thissuper的三种使用方式

 


this:

1.局部变量调用成员变量this.变量名

2.构造调用构造,不可无参、含参同时进行this.()/this("你好",12)

3.方法调用方法(本类里调用)this.方法();

super:

1.调用父类的成员变量 super.变量名

2.调用父类的构造,不可无参、含参同时进行super()/super("你好",12)

3.方法的调用super.方法名();

父类的创建:

blic class Teacher {
    String name = "America";//定义属性赋值
    int a=600;

    public Teacher() {
        System.out.println("父类无参");
    }

    public Teacher(String n) {
        this.name = n;
        System.out.println("父类有参" + n);
    }

    public void play() {
        System.out.println("语文");
    }

}
 

子类的创建:继承父类

package text;

public class Student extends Teacher {


    int a = 10;//成员变量

    public Student() {//1.创建无参
        super("66");
        System.out.println("无参");
    }

    public void listen() {//方法二
        
        this.print();//本类的普通方法之间的调用
        System.out.println("Listening.");

    }

    public Student(String name) {//1.创建含参
         this();//本类的无参构造之间的调用
        //super("China");
        System.out.println("有参");
    }

    public void print() {//方法一
        super.play();//调用父类的普通方法
        int a=20;//局部变量

         System.out.println("姓名");
        System.out.println(this.a);
        System.out.println(super.a);//获取父类的成员变量
        //this.listen();

    }

    public static void main(String[] args) {
        Student s = new Student();
        Student s2 = new Student("白");
        s.print();
        //System.out.println("name的值为:" + s.name);

    }

}

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值