捷凌网安JAVA频道
捷凌IT教育
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME 开源技术 其他技术 论坛
 您现在的位置: 捷凌网安 >> Java >> 新手入门 >> 正文
java中this与super用法

作者:佚名 责任编辑:左决 点击数: 更新时间:2008-5-11 3:07:16

this用法

    this的用法在java中大体可以分为3种:

 //1.普通的直接引用
class test {
   private int x,y;
   public test(int x,int y) {
       setX(x);//也可以写为this.setX(x);这种情况下this可以省略.
   }
}
//2.方法中的某个形参名与当前对象的某个成员有相同的名字.为了不混淆,使用this区分有this引用的是成员,没有this是形参

class test {
   private int x,y;
   public test(int x,int y) {
       setX(x);//也可以写为this.setX(x);这种情况下this可以省略.
   }
   setX(int x){
       this.x = x;//this.x是引用的对象,x是setX(int x)中的行参x
   }
}
//3.引用构造函数
class test.{
    public int x,y;
    public test(int x,int y){
        this.x = x;
        this.y = y;
    }
    public test(){
         this.(2,0)//构造函数调用其他构造函数的方法,这个例子调用上面那个复用构造函数的方法
    }
}


    super用法

    super 用在构造函数时要放在第一行,相当于调用super就刷新了构造函数

    super.g() 调用的成员 它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时)实例说明
   

package test;

public class ThisTest {

    private int i=0;

    //第一个构造器:有一个int型形参

    ThisTest(int i){

       this.i=i+1;//此时this表示引用成员变量i,而非函数参数i

       System.out.println("Int constructor i——this.i:  "+i+"——"+this.i);

       System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));

       //从两个输出结果充分证明了i和this.i是不一样的!

    }

    //  第二个构造器:有一个String型形参

    ThisTest(String s){

       System.out.println("String constructor:  "+s);

    }

    //  第三个构造器:有一个int型形参和一个String型形参

    ThisTest(int i,String s){

       this(s);//this调用第二个构造器

       //this(i);

       /*此处不能用,因为其他任何方法都不能调用构造器,只有构造方法能调用他。

       但是必须注意:就算是构造方法调用构造器,也必须为于其第一行,构造方法也只能调

       用一个且仅一次构造器!*/

       this.i=i++;//this以引用该类的成员变量

       System.out.println("Int constructor:  "+i+" "+"String constructor:  "+s);

    }

    public ThisTest increment(){

       this.i++;

       return this;//返回的是当前的对象,该对象属于(ThisTest)

    }

    public static void main(String[] args){

       ThisTest tt0=new ThisTest(10);

       ThisTest tt1=new ThisTest("ok");

       ThisTest tt2=new ThisTest(20,"ok again!");

      

       System.out.println(tt0.increment().increment().increment().i);

       //tt0.increment()返回一个在tt0基础上i++的ThisTest对象,

       //接着又返回在上面返回的对象基础上i++的ThisTest对象!

    }

}

运行结果:
 

Int constructor i——this.i:  10——11

String constructor:  ok

String constructor:  ok again!

Int constructor:  21

String constructor:  ok again!

14


    注意:this不能用在static方法中!所以甚至有人给static方法的定义就是:没有this的方法!虽然夸张,但是却充分说明this不能在static方法中使用!

 //1.super运用在构造函数中
class test1 extend test{
    public test1()...{
         super();//调用父类的构造函数
    }
    public test1(int x){
         super(x);//调用父类的构造函数,因为带形参所以super 也要带行参
    }
}
//2.调用父类中的成员
class test1 extend test{
    public test1(){}
    public f(){//假如父类里有一个成员叫g();
         super.g();//super引用当前对象的直接父类中的成员
    }
}

  • 上一篇文章:

  • 下一篇文章:
  •  
     最进更新
    普通文章J2SE实现windows读取网卡的物05-11
    普通文章Scala—Java的避难所之main(05-11
    普通文章Spring数据源的灵活配置巧应05-11
    普通文章Spring 与 Log4J 进行动态日05-11
    普通文章Heritrix的多线程ToeThread和05-11
    普通文章应用spring示例开发网站构思05-11
    普通文章Spring 数据源配置与应用05-11
    普通文章Spring中的四种声明式事务的05-11
    普通文章Hibernate+Spring搞定Clob、05-11
    普通文章Hibernate的映射关联关系05-11
     
     推荐文章
    推荐文章Java技术开源搜索引擎04-30
    推荐文章Eclipse中建立自己的JUnit测04-30
    推荐文章Eclipse 3.3上安装jadclipse04-30
    推荐文章spring 编程入门十大问题解答04-30
    推荐文章Java编程中Spring的一些负面04-30
    推荐文章Java应用中Hibernate对多表关04-30
    推荐文章关于hibernate的缓存和CRUD04-30
    推荐文章基于Struts1.2的动态多文件上04-30
    推荐文章演示Struts2实现简单上传代码04-30
    推荐文章J2EE学习笔记--Struts初步认04-30
     
     热点文章
    普通文章J2SE实现windows读取网卡的物05-11
    普通文章Scala—Java的避难所之main(05-11
    普通文章Spring数据源的灵活配置巧应05-11
    普通文章Spring 与 Log4J 进行动态日05-11
    普通文章Heritrix的多线程ToeThread和05-11
    普通文章应用spring示例开发网站构思05-11
    普通文章Spring 数据源配置与应用05-11
    普通文章Spring中的四种声明式事务的05-11
    普通文章Hibernate+Spring搞定Clob、05-11
    普通文章Hibernate的映射关联关系05-11

    | 设为首页 | 加入收藏 | 联系站长 | 广告服务 | 友情链接 | 版权申明 | 网站地图 |

    在线交流 捷凌网安主群:51649627
    Copyright 2007-2008 © 捷凌网安. All rights reserved.
    备案序号:蜀ICP备08001812号