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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

首先让我们来看以下this和super都有什么功能:

this:
        访问本类对象成员变量                                this.变量名
        调用本类普通方法                                        this.方法名(参数)
        本类构造方法调用本类其他构造                本类构造方法第一行this(参数)
       
super:
                访问本类对象当中的父类对象成员变量        super.变量名
                调用本类对象当中的父类普通方法                super.方法名()
本类构造方法调用父类构造                        本类构造方法第一行super(参数)
然后我们将super和this对比着来验证:
验证this可以访问本类对象成员
新建一个Animal类书写如下代码
  1. public class Animal {
  2.         public  String name="咪咪";
  3.         public  int age=4;
  4. }
复制代码

新建一个Cat类继承Animal类,并且在该类中定义一个showNameAge()的方法书写代码如下:
  1. public class Cat extends Animal {

  2.         private  String name="丫丫";
  3.         private  int age=2;

  4.         public void showNameAge(){
  5.                 System.out.println(this.name+"今年"+this.age+"岁了!");
  6. }
  7. }
复制代码

然后新建测试类Test,创建cat对象,并调用showNameAge()方法,运行
  1. public class Test {
  2.        
  3.         public static void main(String[] args) {
  4. Cat cat=new Cat();               
  5.                 cat.showNameAge();

  6. }
  7. }
复制代码

运行后我们发现输出为:丫丫今年2岁了!,所以this调用的是本类对象的成员变量
2.验证super可以访问本类对象当中的父类对象成员变量
将上述cat类的showNameAge()方法中的this改为super,运行程序,输出结果为:咪咪今年四岁了!故而验证了super可以访问本类对象当中的父类对象成员变量
3.this可以调用本类普通方法
在验证1中的cat类中,加入一个方法名为testThis()的方法,代码如下
  1. public class Cat extends Animal {

  2.         private  String name="丫丫";
  3.         private  int age=2;

  4.         public void showNameAge(){
  5.                 System.out.println(this.name+"今年"+this.age+"岁了!");
  6. }
  7. public void testThis(){
  8.         this.showNameAge();
  9. }
  10. }
复制代码

然后在Test类中,将无用代码注释,创建cat对象,并调用testThis方法,代码如下
  1. public class Test {
  2.        
  3.         public static void main(String[] args) {
  4. Cat cat=new Cat();               
  5.                 cat.testThis();
  6. }
  7. }
复制代码

运行输出结果为丫丫今年2岁了!进而验证了this可以调用本类普通方法
4. super调用本类对象当中的父类普通方法
针对验证3中的cat类,将showNameAge()方法改为如下
  1. public void showNameAge(){
  2.                 System.out.println(super.getName()+"今年"+super.getAge()+"岁了!");
  3.         }
复制代码

并运行结果为:咪咪今年4岁了!我们通过super调用了Animal类的getName()方法,所以验证了super调用本类对象当中的父类普通方法
5. this可以通过本类构造方法调用本类其他构造
在cat类中写两个构造方法,代码如下
  1. public Cat(){               
  2.                 System.out.println("利用this调用了本类的空参构造方法");
  3.         }
  4.         public Cat(String name, int age) {               
  5.                 this();               
  6.         }
复制代码

将其他代码注释,然后在Test类中,将其余代码注释,书写代码如下:
  1. Cat cat2=new Cat("豆豆",4);
复制代码

运行结果为:利用this调用了本类的空参构造方法
可以验证this可以通过本类构造方法调用本类其他构造
6super可以通过本类构造方法调用父类构造
将cat类修改如下:
  1. public class Cat extends Animal {

  2. private static  String name;
  3.         private static  int age;
  4.        
  5.         public Cat(){               
  6.                 super(name,age);
  7.         }
  8.         public Cat(String name, int age) {               
  9.                 super();               
  10. }
  11. }
复制代码

将Animal的构造方法书写如下:
  1. public Animal(){
  2.                 System.out.println("父类的空参构造方法被调用了!");
  3.         }
  4.        
  5.         public Animal(String name,int age){
  6.                 System.out.println("父类的满参构造方法被调用了!");
  7.                 this.name=name;
  8.                 this.age=age;
  9.         }

复制代码

在Test类中书写代码如下:
  1. Cat cat2=new Cat("豆豆",4);
  2.         Cat cat=new Cat();
复制代码

运行可以看到两条信息: 父类的空参构造方法被调用了!
父类的满参构造方法被调用了!
因此验证了super可以通过本类构造方法调用父类构造

0 个回复

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