码迷,mamicode.com
首页 > 其他好文 > 详细

56 面向对象this和super的区别和应用

时间:2017-01-27 00:19:23      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:对象   print   new   end   dog   成员   this   面向对象   xtend   

this.成员变量 可以调用本类和父类的成员变量,当本类没有情况下调用父类

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

 

 1 class Demo1_Extends{
 2     public static void main(String[] args) {
 3         Dog d = new Dog();
 4         d.print();
 5     }
 6 }
 7 
 8 class Animals{
 9      int num1 = 10;
10      int num2 = 20;
11 }
12 
13 class Dog extends Animals{
14     int num2 = 30;
15     
16     public void print(){
17         System.out.println(this.num2);
18         System.out.println(this.num1);
19         System.out.println(super.num2);
20     }
21 
22 }

 

56 面向对象this和super的区别和应用

标签:对象   print   new   end   dog   成员   this   面向对象   xtend   

原文地址:http://www.cnblogs.com/panw3i/p/6352248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!