沈阳工业大学软件学院Java程序设计课件 第4章.pptVIP

  • 1
  • 0
  • 约2.08万字
  • 约 125页
  • 2019-05-12 发布于广东
  • 举报

沈阳工业大学软件学院Java程序设计课件 第4章.ppt

第四章 类的重用;目录;4.1 类的继承;基类(base class) 也称超类(superclass) 是被直接或间接继承的类 派生类(derived-class) 也称子类 (subclass) 继承其他类而得到的类 继承所有祖先的状态和行为 派生类可以增加变量和方法 派生类也可以覆盖(override)继承的方法;子类对象与父类对象存在“IS A”(或“is kind of”)的关系 ;动物类层次举例;举例;派生类产生的对象 从外部来看,它应该包括 与基类相同的接口 可以具有更多的方法和数据成员 其内包含着一个基类类型的子对象;4.1.2 继承的语法;在一个公司中,有普通员工(Employees)及管理人员(Magagers)两类人员 职员对象(Employees)可能有的属性信息包括 员工号(employeeNumber) 姓名(name) 地址(address) 电话号码(phoneNumber) 管理人员(Managers)除具有普通员工的属性外,还可能具有下面的属性 职责(responsibilities) 所管理的职员(listOfEmployees);Employee与Manager的类图;//父类Employee class Employee { int employeeNumbe ; String name, address, phoneNumber ; } //子类Manager class Manager extends Employee { //子类增加的数据成员 String responsibilities, listOfEmployees; };设有三个类:Person, Employee, Manager。其类层次如图: ;public class Person { public String name; public String getName() { return name; } } public class Employee extends Person { public int employeeNumber; public int getEmployeeNumber() { return employeeNumber; } } public class Manager extends Employee { public String responsibilities; public String getResponsibilities() { return responsibilities; } };public class Exam4_2Test { public static void main(String args[]){ Employee li = new Employee(); = Li Ming; li.employeeNumber = 123456; System.out.println(li.getName()); System.out.println(li.getEmployeeNumber()); Manager he = new Manager(); = He Xia; he.employeeNumber = 543469; he.responsibilities = Internet project; System.out.println(he.getName()); System.out.println(he.getEmployeeNumber()); System.out.println(he.getResponsibilities()); } };运行结果 Li Ming 123456 He Xia 543469 Internet project 说明 子类不能直接访问从父类中继承的私有属性及方法,但可使用公有(及保护)方法进行访问 ;public class B { public int a = 10; private int b = 20; protected int c = 30; public int getB() { return b; } } public class A extends B { public int d; public void tryVariabl

文档评论(0)

1亿VIP精品文档

相关文档