Java?this關(guān)鍵字的使用案例詳解
通過一個小案例來學(xué)習(xí)、理解一下this關(guān)鍵字的使用~~~
Boy類
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+ " 也想娶:" + girl.getName()); } public void shout() { if(this.age >= 22) { System.out.println("可以了"); }else { System.out.println("不可以"); } } }
Girl類
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 + " 想嫁給:" + boy.getName()); boy.marry(this); } public void compare(Girl girl) { if(this.age > girl.getAge()) { System.out.println(this.getName() + "比" + girl.getName() + "大"); }else if(this.age < girl.getAge()) { System.out.println(this.getName() + "比" + girl.getName() + "小"); }else { System.out.println(this.getName() + "和" + girl.getName() + "一樣大"); } } }
BoyGirlTest類
package myjava1; public class BoyGirlTest { public static void main(String[] args) { Boy boy = new Boy("羅密歐",21); boy.shout(); Girl girl = new Girl("朱麗葉",18); girl.marry(boy); Girl girl2 = new Girl("祝英臺",19); girl2.compare(girl); girl.compare(girl2); girl2.compare(girl2); } }
運(yùn)行結(jié)果
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Springboot 整合RabbitMq(用心看完這一篇就夠了)
這篇文章主要介紹了Springboot 整合RabbitMq(用心看完這一篇就夠了),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12CAT分布式實(shí)時監(jiān)控系統(tǒng)使用詳解
這篇文章主要為大家介紹了CAT分布式實(shí)時監(jiān)控系統(tǒng)介紹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Mybatis 動態(tài)SQL的幾種實(shí)現(xiàn)方法
這篇文章主要介紹了Mybatis 動態(tài)SQL的幾種實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11使用SpringBoot和JPA實(shí)現(xiàn)批量處理新增、修改
最近項目需要在JPA中使用ID進(jìn)行批量更新,所以下面這篇文章主要給大家介紹了關(guān)于使用SpringBoot和JPA實(shí)現(xiàn)批量處理新增、修改的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06ShardingSphere結(jié)合MySQL實(shí)現(xiàn)分庫分表的項目實(shí)踐
在實(shí)際開發(fā)中,如果表的數(shù)據(jù)過大我們需要把一張表拆分成多張表,本文主要介紹了使用ShardingSphere實(shí)現(xiàn)MySQL分庫分表,具有一定的參考價值,感興趣的可以了解一下2024-03-03Kotlin傳遞可變長參數(shù)給Java可變參數(shù)實(shí)例代碼
這篇文章主要介紹了Kotlin傳遞可變長參數(shù)給Java可變參數(shù)實(shí)例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01