Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the ways to use the Java this keyword

Shulou Source: shulou.com Published: 2022-06-02 02:36:53 10月02日 Update

This article mainly introduces the relevant knowledge of "what is the use of the Java this keyword?". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what is the use of the Java this keyword?" can help you solve the problem.

Boy class package myjava1;public class Boy {private String name; private int age; public Boy () {} public Boy (String name,int age) {this.name = name; this.age = age;} public String getName () {return name } public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age } public void marry (Girl girl) {System.out.println (this.name+ "also wants to marry:" + girl.getName ());} public void shout () {if (this.age > = 22) {System.out.println ("OK") } else {System.out.println ("cannot");} Girl class package myjava1;public class Girl {private String name; private int age; public Girl () {} public Girl (String name,int age) {this.name = name This.age = age;} public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age } public void marry (Boy boy) {System.out.println (this.name + "want to marry:" + boy.getName ()); boy.marry (this) } public void compare (Girl girl) {if (this.age > girl.getAge ()) {System.out.println (this.getName () + "than" + girl.getName () + "large") } else if (this.age < girl.getAge ()) {System.out.println (this.getName () + "than" + girl.getName () + "smaller");} else {System.out.println (this.getName () + "as big as" + girl.getName () + ")) } BoyGirlTest class package myjava1;public class BoyGirlTest {public static void main (String [] args) {Boy boy = new Boy ("Romeo", 21); boy.shout (); Girl girl = new Girl ("Juliet", 18); girl.marry (boy) Girl girl2 = new Girl (Zhu Yingtai, 19); girl2.compare (girl); girl.compare (girl2); girl2.compare (girl2);}}

Running result

This is the end of the content about "what are the ways to use Java this keywords". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Methods methods keywords keywords knowledge industries different practical content practical practical articles more cases knowledge points articles results information channels processes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Linux Apple vpn