Java 关于继承小练习2
1 package com.bytezero.inherit2; 2 3 4 public class KidsTest 5 { 6 public static void main(String[] args) 7 { 8 Kids someKid = new Kids(12); 9 someKid.printAge(); 10 11 someKid.setSalary(0); 12 someKid.setSex(1); 13 someKid.employeed(); 14 someKid.manOrWoman(); 15 } 16 }
1 package com.bytezero.inherit2; 2 3 /* 4 * 定义一个ManKind类,包括成员变量int sex和int salary; 5 * 方法 void manOrWoman():根据sex的值显示"man"(sex == 1)或者”woman“(sex ==0) 6 * 方法 void employeed(); 根据salary的值吓显示”no job“(salary ==0)或者”job“(salary != 0) 7 * 8 */ 9 public class ManKind 10 { 11 private int sex; //性别 12 private int salary; //薪资 13 14 15 16 17 18 public ManKind() 19 { 20 21 } 22 23 public ManKind(int sex, int salary) 24 { 25 26 this.sex = sex; 27 this.salary = salary; 28 } 29 30 31 32 33 34 public void manOrWoman() 35 { 36 if(sex == 1) 37 { 38 System.out.println("man"); 39 } 40 else if(sex ==0) 41 { 42 System.out.println("woman"); 43 } 44 } 45 46 public void employeed() 47 { 48 // if(salary == 0) 49 // { 50 // System.out.println("no job"); 51 // } 52 // else 53 // { 54 // System.out.println("job"); 55 // } 56 //或 57 String jobInfo = (salary == 0)?"no job" :"job"; 58 System.out.println(jobInfo); 59 } 60 61 public int getSex() { 62 return sex; 63 } 64 65 public void setSex(int sex) { 66 this.sex = sex; 67 } 68 69 public int getSalary() { 70 return salary; 71 } 72 73 public void setSalary(int salary) { 74 this.salary = salary; 75 } 76 77 78 79 80 81 }
1 package com.bytezero.inherit2; 2 3 /* 4 * 定义kids类继承ManKid,并包括 成员变量int yearsOld; 5 * 方法printAge() 打印 yearsOld的值 6 * 7 */ 8 public class Kids extends ManKind 9 { 10 private int yearsOld; 11 12 public Kids() 13 { 14 15 } 16 17 18 public Kids(int yearsOld) 19 { 20 21 this.yearsOld = yearsOld; 22 } 23 24 25 26 public void printAge() 27 { 28 System.out.println(yearsOld); 29 } 30 31 32 public int getYearsOld() 33 { 34 return yearsOld; 35 } 36 37 public void setYearsOld(int yearsOld) 38 { 39 this.yearsOld = yearsOld; 40 } 41 42 43 44 45 46 47 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15307081.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)