首页编程java编程java中this 什么意思,java里this是什么意思

java中this 什么意思,java里this是什么意思

编程之家2023-10-1292次浏览

今天给各位分享java中this 什么意思的知识,其中也会对java里this是什么意思进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

java中this 什么意思,java里this是什么意思

java里this是什么意思

Java关键字this只能用于方法方法体内。

个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 this。因此,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this,这在“Java关键字static、fina l使用总结”一文中给出了明确解释。并且this只和特定的对象关联,而不和类关联,同一个类的不同对象有不同的this。

java中this的含义

1、在一个类中,this可以表示该类的当前实例;例如:

java中this 什么意思,java里this是什么意思

public class Leaf{

private int i= 0;

Leaf increment(){

java中this 什么意思,java里this是什么意思

i++;

return this;

}

void print(){

System.out.println("i="+ i);

}

public static void main(String[] args){

Leaf x= new Leaf();

x.increment().increment().increment().print();

}

}///:~

2、若为一个类写了多个构造器,那么经常都需要在一个构造器里调用另一个构造器,以避免写重复的代码。这时可以使用this,例如:

//: Flower.java

// Calling constructors with"this"

public class Flower{

private int petalCount= 0;

private String s= new String("null");

Flower(int petals){

petalCount= petals;

System.out.println(

"Constructor w/ int arg only, petalCount="

+ petalCount);

}

Flower(String ss){

System.out.println(

"Constructor w/ String arg only, s="+ ss);

s= ss;

}

Flower(String s, int petals){

this(petals);

//! this(s);// Can''t call two!

this.s= s;// Another use of"this"

System.out.println("String& int args");

}

Flower(){

this("hi", 47);

System.out.println(

"default constructor(no args)");

}

void print(){

//! this(11);// Not inside non-constructor!

System.out.println(

"petalCount="+ petalCount+" s="+ s);

}

public static void main(String[] args){

Flower x= new Flower();

x.print();

}

}///:~

而需要注意的是:在一个类A的内部类B中使用this它表示的并非是A.B的当前实例,而是A的当前实例;

java中 this的定义

this指当前对象。记住,是“对象”,不是“类”或其它什么。

一个类要有对象,那就肯定是用 new关键字创建出来的,所以 this一定是出现在可以 new的类里面(不管是类外边 new还是自己内部静态方法里面 new)。

静态方法里面是不会出现 this,因为静态方法不许要对象,只要类名就能使用了。

再举个简单的例子:有一个“学生”类,创建这个类的时候需要参数“姓名”,

public学生(String姓名)

然后你 new了两个对象:学生1= new学生(“蜘蛛侠”);学生2= new学生(“蝙蝠侠”);

你在“学生”类里面都有个获取名字的方法“get姓名()”,里面有个打印 this.姓名,

学生1.get姓名();

学生2.get姓名();

执行这两句之后,得到的打印结果是:

蜘蛛侠

蝙蝠侠

也就是说,一个地方的“this”,在这两个学生对象里面,就分别代表这两个具体的超人学生。

java中this的作用

在Java中,this关键字表示当前对象的引用,它是一个引用类型的变量。this关键字只能在非静态方法中使用,因为静态方法没有this关键字。this关键字指的是当前对象,因此只有在创建对象之后才能使用this关键字。同时,使用this关键字需要遵守Java的变量作用域规则,避免产生歧义和错误。

在Java中,this关键字具有以下作用:

1、区分局部变量和实例变量

当局部变量和实例变量同名时,使用this关键字可以明确指定使用实例变量,而非局部变量。例如:

public class Person{   private String name;   public void setName(String name){       this.name= name;   }}

在上面的代码中,使用this.name表示实例变量,而name表示方法参数。

2、调用当前对象的方法

在一个对象的方法中,可以使用this关键字调用该对象的其他方法。这种方式可以提高代码的可读性和重用性。例如:

public class Person{   private String name;   public void setName(String name){       this.name= name;   }   public void printName(){       System.out.println("My name is"+ this.name);   }}

在上面的代码中,使用this.printName()调用了对象的printName()方法。

3、在构造函数中调用其他构造函数

当一个类有多个构造函数时,可以使用this关键字调用其他构造函数,简化构造函数的代码。在构造函数中使用this关键字调用其他构造函数时,必须放在构造函数的第一行。例如:

public class Person{   private String name;   private int age;   public Person(String name){       this(name, 0);   }   public Person(String name, int age){       this.name= name;       this.age= age;   }}

在上面的代码中,使用this(name, 0)调用了另一个构造函数。

4、作为返回值返回当前对象的引用

在一个对象的方法中,可以使用this关键字返回当前对象的引用。这种方式可以支持方法链式调用,提高代码的简洁性和可读性。例如:

public class Person{   private String name;   private int age;   public Person setName(String name){       this.name= name;       return this;   }   public Person setAge(int age){       this.age= age;       return this;   }}

在上面的代码中,setName()和setAge()方法都返回当前对象的引用,支持链式调用。

综上所述,this关键字可以区分局部变量和实例变量,调用当前对象的方法,简化构造函数的代码,以及作为返回值返回当前对象的引用。

好了,本文到此结束,如果可以帮助到大家,还望关注本站哦!

什么是java异步通信 java中同步和异步有什么异同叶子萱萱(叶萱的样子)