j2se——this关键字

2025-12-22 09:08:46

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("两个对象不一样");

}

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:62
  • 阅读量:163
  • 阅读量:149
  • 阅读量:67
  • 阅读量:88
  • 猜你喜欢