A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 岳星球 初级黑马   /  2018-12-16 12:50  /  397 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

super和this的含义:
super :代表父类的存储空间标识(可以理解为父亲的引用)。
this :代表当前对象的引用(谁调用就代表谁)。
super和this的用法:
1. 访问成员
this.成员变量    ‐‐    本类的     
super.成员变量    ‐‐    父类的   
this.成员方法名()  ‐‐    本类的     
super.成员方法名()   ‐‐    父类的
用法演示,代码如下:
Class Animal{
       Publicvoid eat(){
              System.out.println(“animal:eat”);
       }
}
class Cat exstends Animal{
       publicvoid eat() {
              System.out.println(“cat:eat”);
       }
       Publicvoid eatTest(){
              this.eat();      //this   调用本类的方法
              Super.eat();    //super 调用父类的方法
       }
}
Public class ExtendsDemo08{
       Publicstatic void nain (String[] args) {
              Animala=new Animal();
a.     eat();
Cat c=new Cat();
b.     eatTest();
}
}
输出结果是:
       animal:eat
       cat:eat
       animal:eat
2. 访问构造方法
  this(...)         本类的构造方法  
   super(...)          父类的构造

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马