设计一个用于人事管理的“人员”类。由于考虑到通用性,这里只抽象出所有类型人员都具有的属性:编号、性别、出生日期、身份证号等。其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。
1 import java.util.Scanner; 2 //日期类 3 class Date{ 4 private int year; 5 private int month; 6 private int day; 7 public Date() { 8 } 9 public Date(int year,int month,int day) { 10 this.year=year; 11 this.month=month; 12 this.day=day; 13 } 14 public void set() { 15 Scanner in=new Scanner(System.in); 16 year=in.nextInt(); 17 month=in.nextInt(); 18 day=in.nextInt(); 19 } 20 public void display() { 21 System.out.println(year+"."+month+"."+day); 22 } 23 } 24 //人员类 25 public class People { 26 private int num; //编号 27 private String sex; //性别 28 private Date date=new Date(); //出生日期 29 private String ID; //身份证号 30 public People() { 31 } 32 public People(int num, String sex, Date date, String iD) { 33 super(); 34 this.num = num; 35 this.sex = sex; 36 this.date = date; 37 ID = iD; 38 } 39 public void input() { 40 Scanner in=new Scanner(System.in); 41 System.out.println("请输入人员信息:"); 42 System.out.print("编号:"); 43 num=in.nextInt(); 44 System.out.print("性别:"); 45 sex=in.next(); 46 System.out.print("出生日期:"); 47 date.set(); 48 System.out.print("身份证号:"); 49 ID=in.next(); 50 } 51 public void output() { 52 System.out.println("编号:"+num); 53 System.out.println("性别:"+sex); 54 System.out.println("出生日期:"); 55 date.display(); 56 System.out.println("身份证号:"+ID); 57 } 58 public static void main(String[] args) { 59 People p=new People(); 60 p.input(); 61 p.output(); 62 } 63 }