j2se——this关键字

 时间:2026-02-18 03:37:49

1、this关键字:

·this可以调用当前类的属性

·this可以调用当前类的方法

·this可以调用当前类的构造方法

·this 还可以表示当前的对象

注意:

在使用this调用当前类的构造方法的时候,this必须写在构造方法的首行。

2、public Animal(String name,int age){

     this.name = name;

     this.age = age;

}

public Animal(){

}

public Animal(String name){

     this.name = name;

}

public void run(){

    System.out.println("跑步");

}

}

1.调用当前类属性,方法

public void say(){

    System.out.println("姓名"+ this.name);

3、2.调用当前类的构造方法

public Animal(String name){

    this();

    this.name = name;

}

4、3.this表示当前对象

public void compare(Animal a){

if( this == a){

    System.out.println("两个对象一样");

}else{

     System.out.println("两个对象不一样");

}

}

  • j2se——super关键字
  • j2se——final关键字
  • j2se——static关键字
  • static关键字
  • j2se入门教程
  • 热门搜索
    张澜简介 姜姒虐渣攻略 视频英文怎么写 许慎简介 永久山地车怎么样 怎样有效减肥 散光轴位怎么测 上古卷轴5怎么全屏 双星物语攻略 张碧晨简介