Java 小案列 this关键字使用+构造器 +方法+调用
1 package com.bytezero.thistest; 2 3 public class Boy 4 { 5 private String name; 6 private int age; 7 8 //构造器 9 public Boy() 10 { 11 12 } 13 14 public Boy(String name) 15 { 16 17 this.name = name; 18 } 19 public Boy(int age) 20 { 21 this.age = age; 22 } 23 public Boy(String name,int age) 24 { 25 this.name = name; 26 this.age = age; 27 } 28 29 30 31 32 //set get 33 public void setName(String name) 34 { 35 this.name = name; 36 } 37 public String getName() 38 { 39 return name; 40 } 41 42 public void setAge(int age) 43 { 44 this.age = age; 45 } 46 public int getAge() 47 { 48 return age; 49 } 50 51 //方法 52 public void marry(Girl girl) 53 { 54 System.out.println("我想娶"+girl.getName()); 55 } 56 57 public void shout() 58 { 59 if(this.age >= 22) 60 { 61 System.out.println("可以登记 结婚"); 62 } 63 else 64 { 65 System.out.println("不可以登记"); 66 } 67 } 68 69 70 71 }
1 package com.bytezero.thistest; 2 3 public class Girl 4 { 5 private String name; 6 private int age; 7 8 //构造器 9 public Girl() 10 { 11 12 } 13 public Girl(String name, int age) 14 { 15 16 this.name = name; 17 this.age = age; 18 } 19 20 21 //方法 22 public void setName(String name) 23 { 24 this.name = name; 25 } 26 27 28 29 public String getName() 30 { 31 return name; 32 } 33 34 public void marry(Boy boy) 35 { 36 System.out.println("我想嫁给"+boy.getName()); 37 boy.marry(this); 38 39 } 40 41 42 /** 43 * 44 * @Description 比较两个对象的大小 45 * @author Bytezero·zhenglei! 46 * @date 2021年9月15日下午7:20:11 47 * @param girl 48 * @return 整数:当前对象大 49 * 负数:当前对象小 50 * 0 :当前对象与形参对象相等 51 * 52 */ 53 public int compare(Girl girl) 54 { 55 56 // if(this.age > girl.age) 57 // { 58 // return 1; 59 // } 60 // else if(this.age < girl.age) 61 // { 62 // return -1; 63 // } 64 // else 65 // { 66 // return 0; 67 // } 68 // } 69 70 return this.age - girl.age; 71 } 72 73 74 }
1 package com.bytezero.thistest; 2 /** 3 * 4 * @Description 5 * @author Bytezero·zhenglei! Email:420498246@qq.com 6 * @version 7 * @date 2021年9月15日下午7:40:17 8 * @ 9 * 10 */ 11 public class BoyGirlTest 12 { 13 public static void main(String[] args) 14 { 15 Boy boy = new Boy("罗密欧",21); 16 boy.shout(); 17 18 Girl girl = new Girl("朱丽叶",18); 19 girl.marry(boy); 20 21 22 Girl girl1 = new Girl("祝英台",19); 23 int compare =girl.compare(girl1); 24 25 if(compare > 0) 26 { 27 System.out.println(girl.getName()+"大"); 28 } 29 else if (compare < 0) 30 { 31 System.err.println(girl1.getName()+"大"); 32 } 33 else 34 { 35 System.out.println("一样大"); 36 } 37 38 } 39 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15284147.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)