Java_04 面向对象程序设计–2

时间:2026-01-17

第三章 Java面向对象程序设计– 23.5 3.6 3.7类的继承 Object类 final类与final方法

3.83.9

abstract类类的组合

3.10访问控制

3.11包

3.5类的继承 (Inheritance)

类的继承——软件重用的一种方法

一种由已有的类创建新类的机制,是面向对象程序设计的基石之一,也是面向对象技术的三大特性之一。

通过继承,可以根据已有类来定义新类,新类拥有已有父类的所有功能

Java只支持类的单继承,每个子类只能有一个直接父类父类是所有子类的公共属性及方法的集合,子类则是父类

的特殊化。

继承机制可以提高程序的抽象程度,提高代码的可重用性2

3.5.1继承的概念

基类(base class)

也称超类(superclass)

是被直接或间接继承的类

派生类(derived-class)

也称子类 (subclass)

继承其他类而得到的类继承所有祖先的状态和行为派生类可以增加变量和方法

派生类也可以覆盖(override)继承的方法3

3.5.1继承的概念

is_a关系

子类对象与父类对象存在“IS A”(或“is kind of”)的关系

3.5.1继承的概念

动物类层次举例

3.5.2继承的语法

继承的语法class childClass extends parentClass{//类体}

3.5.2继承的语法举例:

在一个公司中,有普通员工(Employees)及管理人员(Magagers)两类人员职员对象(Employees)可能有的属性信息包括

员工号(employeeNumber)姓名(name)地址(address)电话号码(phoneNumber)

管理人员(Managers)除具有普通员工的属性外,还可能具有下面的属性

职责(responsibilities)

所管理的职员(listOfEmployees)

3.5.2继承的语法

父类Employee

class Employee{

int employeeNumbe;String name, address, phoneNumber;}

子类Manager

class Manager extends Employee{//子类增加的数据成员 String responsibilities, listOfEmployees;

}8

3.5.2继承的语法

设有三个类:Person, Employee, Manager。public class Employee extends Person{public int employeeNumber; public int getEmployeeNumber(){ return employeeNumber;

public class Person{public String name; public String getName(){ return name;

}}}

}public class Manager extends Employee{ public String responsibilities; public String getResponsibilities(){ return responsibilities;

}}9

测试上例——InheritanceTest.java

public class InheritanceTest{ public static void main(String args[]){ Employee li= new Employee(); http://www.77cn.com.cn="Li Ming"; li.employeeNumber= 123456; System.out.println(li.getName()); System.out.println(li.getEmployeeNumber()); Manager he= new Manager(); http://www.77cn.com.cn="He Xia"; he.employeeNumber= 543469; he.responsibilities="Internet project"; System.out.println(he.getName()); System.o

ut.println(he.getEmployeeNumber()); System.out.println(he.getResponsibilities());}}

运行结果Li Ming 123456 He Xia 543469 Internet project

3.5.2继承的语法

说明

子类不能直接访问从父类中继承的私有属性及方法,但可使用

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 tryVariables(){ System.out.println(a); System.out.println(b); System.out.println(getB()); System.out.println(c);}}//允许//不允许//允许//允许11

3.5.3隐藏和覆盖

隐藏和覆盖 子类对从父类继承来的属性变量及方法可以重新定义属性的隐藏 子类中声明了与父类中相同的成员变量名,则从父类继承的变量将被隐藏 子类拥有了两个相同名字的变量,一个继承自父类,另一个由自己声明 当子类执行继承自父类的操作时,处理的是继承自父类的变量,而当子类执行它自己声明的方法时,所操作的就是它自己声明的变量class Child extends Parent{Float aNumber;}13

class Parent{

int aNumber;}

3.5.3隐藏和覆盖

如何访问被隐藏的父类属性

调用从父类继承的方法,则操作的是从父类继承的属性

使用super.属性class B1 extends A1{ int x=100; void printb(){ super.x= super.x+10; System.out.println ("super.x="+ super.x+" x="+ x);}}

举例

class A1{ int x= 2; public void setx(int i){ x= i;} void printa(){ System.out.println(x);}}

14

3.5.3隐藏和覆盖public class SuperTest1 SuperTest1{ public static void main(String[] args){ A1 a1= new A1(); a1.setx(4); a1.printa(); B1 b1= new B1(); b1.printb(); b1.printa(); b1.setx(6);//将继承到的x值设置为6 b1.printb(); b1.printa(); a1.printa();

.java

运行结果4 super.x= 12 x= 100 12 super.x= 16 x= 100 16 415

}}

3.5.3隐藏和覆盖子类不能继承父类中的静态属性,但可以对父类中的静态属性进行操作。 如在上面的例子中,将“int x= 2;”改为“static int x= 2;”,再编译及运行程序。

class A1{ static int x= 2; public void setx(int i){ x= i;} void printa(){ System.out.println(x);}}

class B1 extends A1{ int x=100; void printb(){ super.x= super.x+10; System.out.println ("super.x="+ super.x+" x="+ x);}}16

3.5.3隐藏和覆盖public class SuperTest2 SuperTest2 .java{ public static void main(String[] args){ 运行结果 A1 a1= new A1(); 4 a1.setx(4); super.x= 14 x= 100 a1.printa(); 14 B1 b1= new B1(); super.x= 16 x= 100 b1.printb(); 16 b1.printa(); 16 b1.setx(6);//将继承到的x值设置为6 b1.printb(); b1.printa(); 在上面的结果中,第一行及最后一 …… 此处隐藏:1777字,全部文档内容请下载后查看。喜欢就下载吧 ……

Java_04 面向对象程序设计–2.doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:4.9 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:19元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219