Java >> Tutoriel Java >  >> Java

Java - cette référence

cet objet

ceci est un objet qui contient la référence d'un autre objet qui invoque la fonction membre.

Considérez le programme :

import java.util.Scanner;
class ThisKeyword
{
	private int a;
	private int b;
	void getData(inta,int b)
	{
	a=a;
	b=b;
	}
	void showData()
	{
	System.out.println("Value of Variable A is:"+a);
	System.out.println("Value of Variable B is:"+b);
	}
}
class ThisKeywordExample
{
	public static void main(String args[])
	{
	ThisKeyword T=new ThisKeyword();
	T.getData(4,5);
	T.showData();
	}
}

Sortie

Value of Variable A is:0
Value of Variable B is:0

Explication de la sortie.

le corps de la méthode getData(), le compilateur ne sait pas s'il doit donner la priorité aux variables d'instance ou à Variables locales et c'est pourquoi dans la méthode showData() , le compilateur donne la priorité aux variables d'instance et donne une sortie égale à zéro.

Nous pouvons éviter cela en utilisant cette variable de référence dans la méthode getData() comme suit :

this.a=a;
this.b=b;

Lorsque l'objet T invoque la méthode getData(), cette référence est remplacée par la référence de l'objet T de sorte que :

T.a=a;
T.b=b;

Ainsi, T.a est les variables d'instance et a est la variable locale telle que définie dans le paramètre de la méthode getData().

Publicité

Considérez le programme :

import java.util.Scanner;
class ThisKeyword
{
	private int a;
	private int b;
	void getData(int a,int b)
	{
	this.a=a;
	this.b=b;
	}
	void showData()
	{
	System.out.println("Value of Variable A is:"+a);
	System.out.println("Value of Variable B is:"+b);
	}
}
class ThisKeywordExample
{
	public static void main(String args[])
	{
	ThisKeyword T=new ThisKeyword();
	T.getData(4,5);
	T.showData();
	}
}

Sortie

Value of Variable A is:4
Value of Variable B is:5

Considérons un autre exemple où nous utiliserons ceci mot-clé différemment.

Notre objectif principal dans le programme suivant est de savoir qui est l'aîné parmi deux personnes, nous mettrons en œuvre ce programme à l'aide de ce mot-clé .

Publicité
import java.util.Scanner;

class Person
{
	private String name;
	private int age;
	Scanner KB=new Scanner(System.in);

	void getPerson()
	{
		System.out.println("Enter the Name of the Person:");
		name=KB.nextLine();
		System.out.println("Enter the Age of the Person:");
		age=KB.nextInt();
	}

	void putPerson()
	{
		System.out.println("Name: "+name);
		System.out.println("Age: "+age);
	}

	Person WhoIsElder(Person P)
	{
		if(P.age>age)
		{
		return P;
		}
		else if(P.age==age)
		{
		return null;
		}
		else
		{
		return this;
		}
	}
}

class ElderPerson
{
	public static void main(String args[])
	{
		Person P1=new Person();
		P1.getPerson();
		Person P2=new Person();
		P2.getPerson();
		Person ReferenceHolder;
		ReferenceHolder=P1.WhoIsElder(P2);
		if(ReferenceHolder==null)
		{
		System.out.println("Both the Persons have Same Age");
		}
		else
		{
		System.out.println("Elder Person :");
		ReferenceHolder.putPerson();
		}
	}
}

Sortie

Enter the Name of the Person:
Mayank Singh
Enter the Age of the Person:
18
Enter the Name of the Person:
Amit Shukla
Enter the Age of the Person:
17
Elder Person :
Name :Mayank Singh
Age :18

Si les deux personnes ont le même âge, sortie :

Enter the Name of the Person:
Mayank Singh
Enter the Age of the Person:
18
Enter the Name of the Person:
Amit Shukla
Enter the Age of the Person:
18
Both the Person have Same Age

Balise Java