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

}

}

相关推荐
  • 阅读量:71
  • 阅读量:48
  • 阅读量:165
  • 阅读量:55
  • 阅读量:73
  • 猜你喜欢