黑马程序员技术交流社区

标题: 类的继承03 [打印本页]

作者: _J2EE_LiXiZhen    时间: 2017-11-8 00:07
标题: 类的继承03
请使用代码描述:  
工资为8000元的30岁的王小平老师,会吃饭(吃工作餐)和讲课.
成绩为90分的15岁的李小乐学生,会吃饭(吃学生餐)和学习.
提示: 把老师和学生的共性抽取人类中,人类不使用抽象类

代码实现:
  1. /*        1.定义Person类
  2.                 i.成员变量(私有):  名称(name),年龄(age)
  3.                 ii.成员方法:  吃饭(void eat())
  4.                 1.输出格式:  30岁的王小平在吃饭
  5.                 iii.提供空参和带参构造方法
  6.                 iv.提供setXxx和getXxx方法*/

  7. public class Person {
  8.         private String name;
  9.         private int age;

  10.         // get/set
  11.         public String getName() {
  12.                 return name;
  13.         }

  14.         public void setName(String name) {
  15.                 this.name = name;
  16.         }

  17.         public int getAge() {
  18.                 return age;
  19.         }

  20.         public void setAge(int age) {
  21.                 this.age = age;
  22.         }

  23.         // 空参
  24.         public Person() {
  25.                 // TODO Auto-generated constructor stub
  26.         }

  27.         // 带参
  28.         public Person(String name, int age) {
  29.                 super();
  30.                 this.name = name;
  31.                 this.age = age;
  32.         }

  33.         // 吃饭方法
  34.         public void eat() {
  35.                 System.out.println(this.age + "的" + this.name + "在吃饭");
  36.         }
  37. }

  38. /*3.定义学生类(Student),继承Person类
  39.                 i.成员变量: score(成绩)
  40.                 ii.成员方法:
  41.                 1. 重写父类的 eat()方法
  42.                 a)输出格式:: 成绩为90分的15岁的李小乐学生在吃学生餐
  43.                 2. 特有方法: study() 学习方法
  44.                 a)输出格式:: 成绩为90分的15岁的李小乐学生在学习
  45.                 iii.提供空参和带参构造方法
  46.                 iv.提供setXxx和getXxx方法*/

  47. public class Student extends Person{
  48.         private int score;

  49.         public int getScore() {
  50.                 return score;
  51.         }

  52.         public void setScore(int score) {
  53.                 this.score = score;
  54.         }
  55.        
  56.         public Student() {
  57.                 // TODO Auto-generated constructor stub
  58.         }
  59.        
  60.         public Student(String name,int age,int score) {
  61.                 super(name,age);
  62.                 this.score = score;
  63.         }
  64.        
  65.         //吃饭方法
  66.         public void eat() {
  67.                 System.out.println("成绩为"+this.score+"分的"+this.getAge()+"岁的"+this.getName()+"学生正在吃学生餐");
  68.         }
  69.        
  70.         //学习方法
  71.         public void study() {
  72.                 System.out.println("成绩为"+this.score+"分的"+this.getAge()+"岁的"+this.getName()+"学生正在学习");
  73.         }
  74. }

  75. /*        2.定义老师类(Teacher),继承Person类
  76.                 i.成员变量:  salary(工资)
  77.                 ii.成员方法:
  78.                 1. 重写父类的 eat()方法
  79.                 a)输出格式:: 工资为8000元的30岁的王小平老师在吃工作餐
  80.                 2. 特有方法:  lecture() 讲课方法
  81.                 a)输出格式:: 工资为8000元的30岁的王小平老师在讲课
  82.                 iii.提供空参和带参构造方法
  83.                 iv.提供setXxx和getXxx方法*/

  84. public class Teacher extends Person{
  85.         private double salary;

  86.         public double getSalary() {
  87.                 return salary;
  88.         }

  89.         public void setSalary(double salary) {
  90.                 this.salary = salary;
  91.         }
  92.        
  93.         public void eat() {
  94.                 System.out.println("工资为"+this.salary+"元的"+this.getAge()+"岁的"+this.getName()+"老师正在吃工作餐");
  95.         }
  96.        
  97.         public void lecture() {
  98.                 System.out.println("工资为"+this.salary+"元的"+this.getAge()+"岁的"+this.getName()+"老师正在讲课");
  99.         }
  100.        
  101.         public Teacher() {
  102.                 // TODO Auto-generated constructor stub
  103.         }
  104.        
  105.         public Teacher(String name,int age,double salary) {
  106.                 super(name,age);
  107.                 this.salary = salary;
  108.         }
  109. }

  110. /*
  111. *                工资为8000元的30岁的王小平老师,会吃饭(吃工作餐)和讲课.
  112. *                成绩为90分的15岁的李小乐学生,会吃饭(吃学生餐)和学习.
  113. *                提示: 把老师和学生的共性抽取人类中,人类不使用抽象类
  114. * */

  115.           /*a)提供main方法
  116.                 b)在main方法中
  117.                 i.创建老师对象t,并把名称赋值为”王小平”,年龄赋值为30,工资赋值为8000
  118.                 ii.调用老师对象t的吃饭方法
  119.                 iii.调用老师对象t的讲解方法
  120.                 iv.创建学生对象 s,并把名称赋值为”李小乐”,年龄赋值为14,成绩赋值为90分.
  121.                 v.调用学生对象 s 的吃饭方法
  122.                 vi.调用学生对象 s 的学习方法*/
  123. public class Test {
  124.         public static void main(String[] args) {
  125.                 Teacher t = new Teacher("王小平", 30, 8000);
  126.                 t.eat();
  127.                 t.lecture();
  128.                
  129.                 Student s = new Student("李小乐", 14, 90);
  130.                 s.eat();
  131.                 s.study();
  132.         }
  133. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2