读书人

java例程练习题(关于重写[overwrite/

发布时间: 2012-08-21 13:00:21 作者: rapoo

java例程练习(关于重写[overwrite/override])

  1. public?class?Test?{??
  2. ????public?static?void?main(String[]?args)?{??
  3. ????????Student?student?=?new?Student();??
  4. ????????Person?person?=?new?Person();??
  5. ??????????
  6. ????????person.setName("Tom");??
  7. ????????person.setAge(18);??
  8. ??????????
  9. ????????student.setName("John");??
  10. ????????student.setAge(19);??
  11. ????????student.setSchool("AHU");??
  12. ??????????
  13. ????????System.out.println(person.getInfo());??
  14. ????????System.out.println(student.getInfo());??
  15. ??????????
  16. ????}??
  17. }??
  18. ??
  19. ??
  20. class?Person?{??
  21. ????private?String?name;??
  22. ????private?int?age;??
  23. ??????
  24. ????public?void?setName(String?name)?{??
  25. ????????this.name?=?name;??
  26. ????}??
  27. ??????
  28. ????public?String?getName()?{??
  29. ????????return?name;??
  30. ????}??
  31. ??
  32. ????public?void?setAge(int?age)?{??
  33. ????????this.age?=?age;??
  34. ????}??
  35. ??
  36. ????public?int?getAge()?{??
  37. ????????return?age;??
  38. ????}??
  39. ??????
  40. ????public?String?getInfo()?{??
  41. ????????return?"Name:?"?+?getName()?+?"\n"?+?"Age:?"?+?getAge();??
  42. ????}?????
  43. }??
  44. ??
  45. class?Student?extends?Person?{??
  46. ????private?String?school;??
  47. ??
  48. ????public?void?setSchool(String?school)?{??
  49. ????????this.school?=?school;??
  50. ????}??
  51. ??
  52. ????public?String?getSchool()?{??
  53. ????????return?school;??
  54. ????}??
  55. ??????
  56. ????public?String?getInfo()?{??
  57. ????????return?"Name:?"?+?getName()?+?"\n"?+?"Age:?"?+?getAge()??
  58. ????????????+?"\nSchool:?"?+?getSchool();??
  59. ????}??
  60. }??

读书人网 >编程

热点推荐