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
浙公网安备 33010602011771号