Python 官方文档:入门教程 => 点击学习
目录Boy类Girl类BoyGirlTest类总结通过一个小案例来学习、理解一下this关键字的使用~~~ Boy类 package myjava1; public class B
通过一个小案例来学习、理解一下this关键字的使用~~~
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("不可以");
}
}
}
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() + "一样大");
}
}
}
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);
}
}
运行结果
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!
--结束END--
本文标题: Java this关键字的使用案例详解
本文链接: https://lsjlt.com/news/162434.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0