1、Java高级程序设计2第七章 多态7.1 概念多态即程序中同名的不同方法共存的情况,以下为两种常见的多态方式: 子类对父类方法的覆盖 利用重载在同一个类中定义多个同名的不同方法7.2 域的继承与隐藏 父类的所有非私有域是各子类都有的域的集合:继承 子类中变量对同名父类变量和属性的隐藏 子类执行继承父类的操作时,处理父类的变量,执行自己定义的方法时,操作自己定义的变量。程序7-1 TestHiddenField.java 程序源代码 说明public class TestHiddenField public static void main(String args) D200_Card my20
2、0 = new D200_Card(); my200.balance = 50.0; System.out.println(Father balance: +my200.getBalance();/子类D200_Card中没有getBalance()方/法,故继承父类PhoneCard的方法,所/以使用PhoneCard的balance域 if(my200.performDial() System.out.println(Son balance: +my200.balance);/子类D200_Card中有balance域,所以/使用自己的balance域 abstract class Pho
3、neCard double balance; abstract boolean performDial(); double getBalance() return balance; abstract class Number_PhoneCard extends PhoneCard/抽象子类Number_PhoneCard long cardNumber; int password; String connectNumber; boolean connected; boolean performConnection(long cn, int pw) if(cn=cardNumber & pw =
4、password) connected = true; return true; else connected = false; return false; class D200_Card extends Number_PhoneCard/具体子类D200_Card double additoryFee; double balance; boolean performDial() if(balance(0.5+additoryFee) balance -= 0.5+additoryFee; return true; else return false; 7.3 方法的继承与覆盖 子类可以实现对
5、父类同名方法的覆盖。 方法的覆盖与域的隐藏的区别在于:子类隐藏父类的域只是使之不可见,父类的同名域在子类对象中仍占有内存空间,而子类方法对父类方法的覆盖将清除父类方法占用的内存空间。 子类对父类方法的覆盖应保持与父类完全相同的方法头声明,即方法名,返回值,参数列表都相同。程序72 TestOverLoad.java 程序源代码 说明public class TestOverLoad public static void main(String args) D200_Card my200 = new D200_Card(); my200.balance = 50.0; System.out.pr
6、intln(Father balance: +my200.getBalance();/因为子类D200_Card已定义了方法/getBalance(),所以覆盖父类/PhoneCard定义的方法getBalance()if(my200.performDial() System.out.println(Son balance: +my200.balance); abstract class PhoneCard double balance; abstract boolean performDial(); double getBalance()/父类PhoneCard定义的方法getBalance
7、() return balance; abstract class Number_PhoneCard extends PhoneCard long cardNumber; int password; String connectNumber; boolean connected; boolean performConnection(long cn, int pw) if(cn=cardNumber & pw =password) connected = true; return true; else connected = false; return false; class D200_Car
8、d extends Number_PhoneCard double additoryFee; double balance; boolean performDial() if(balance(0.5+additoryFee) balance -= 0.5+additoryFee; return true; else return false; double getBalance()/ 子类D200_Card定义的方法getBalance() return balance; 7.4 this与super Java系统默认,每个类都缺省地具有null, this, super三个域,可以直接使用。
9、null代表空,在定义一个对象但尚未为其开辟内存单元时可以指定这个对象为nullNode *pnode;pnode = NULL;if(pnode=NULL) new malloc(.)delete pnode;pnode = NULL;this代表当前对象的一个引用,但它不是地址,与指针无关,仅仅是对象的另一个名字super表示当前对象的直接父类对象 父类对象与子类对象的转换要注意以下原则:子类对象可以被视为是其父类的一个对象父类对象不能被当作是其某一个子类的对象如果一个方法的形式参数定义的是父类对象,那么调用这个方法时,可以使用子类对象作为实际参数如果父类对象引用指向的实际是一个子类对象,
10、那么这个父类对象的引用可以用强制类型转换转化为子类对象的引用程序73 HiddenField.java 程序源代码 说明public class HiddenField public static void main(String args) D200_Card my200 = new D200_Card(); my200.balance = 50.0; System.out.println(Father balance: +my200.getBalance(); /用直接父类Number_PhoneCard的balance/域,它没有,则用再上一层PhoneCard的balance if(m
11、y200.performDial() System.out.println(Son balance: +my200.balance); abstract class PhoneCard double balance; abstract boolean performDial(); double getBalance() return balance; abstract class Number_PhoneCard extends PhoneCard long cardNumber; int password; String connectNumber; boolean connected; b
12、oolean performConnection(long cn, int pw) if(cn=cardNumber & pw =password) connected = true; return true; else return false; class D200_Card extends Number_PhoneCard double additoryFee; double balance; boolean performDial() if(balance(0.5+additoryFee) balance -= 0.5+additoryFee; return true; else re
13、turn false; double getBalance() return super.balance; /表示使用它直接父类的balance域 7.5 继承与finalize()public class Frog extends Amphibian /Construct the application Frog() System.out.println(Frog(); protected void finalize() throws Throwable System.out.println(Frog finalize); if(DoBaseFinalization.flag) super.
14、finalize(); /Main method public static void main(String args) if(args.length !=0 & args0.equals(finalize) DoBaseFinalization.flag = true; else System.out.println(Not finalizing bases); new Frog(); System.out.println(Bye); System.gc(); class DoBaseFinalization public static boolean flag = false;class
15、 Amphibian extends Animal Characteristic p = new Characteristic (can live in water); Amphibian() System.out.println(Amphibian(); protected void finalize() throws Throwable System.out.println(Amphibian finalize); if(DoBaseFinalization.flag) super.finalize(); class Characteristic String s; Characteris
16、tic(String c) s = c; System.out.println(Creating Characteristic +s); protected void finalize() System.out.println(finalizing Characteristic +s); class Animal extends LivingCreature Characteristic p = new Characteristic(has heart); Animal() System.out.println(Animal(); protected void finalize() throw
17、s Throwable System.out.println(Animal finalize); if(DoBaseFinalization.flag) super.finalize(); class LivingCreature Characteristic p = new Characteristic(is alive); LivingCreature() System.out.println(LivingCreature(); protected void finalize() throws Throwable System.out.println(LivingCreature fina
18、lize); if(DoBaseFinalization.flag) super.finalize(); 输出:P2361 Not finalizing bases2 Creating Characteristic is alive3 LivingCreature()4 Creating Characteristic has heart5 Animal()6 Creating Characteristic can live in water7 Amphibian()8 Frog()9 Bye10 Frog finalize11 finalizing Characteristic is aliv
19、e12 finalizing Characteristic has heart13 finalizing Characteristic can live in water 每个class含有一个characteristic成员,它会被终止 object的finalize()是个protected,不允许被降低访问权限 如果不人工调用super.finalize(),则父亲的finalize不会被启动7.6 构造函数的继承与重载 构造函数可以从父类那里继承,也可以互相重载。 类的若干个构造函数可以相互调用,一个构造函数调用另一构造函数时,可以使用关键字this, 同时这个调用语句应该是整个构造函
20、数的第一个可执行语句。 子类继承父类的构造函数遵循以下原则(1) 子类无条件地继承父类的不含参数的构造函数(2) 如果子类没有构造函数,则根据上一条,继承父类无参数的构造函数(3) 如果子类有构造函数,则先继承父类无参数的构造函数,再执行自己的构造函数(4) 子类可以继承父类的有参数的构造函数,必须用super关键字显式使用,而且该语句必须是子类构造函数的第一个可执行语句。public class PolyConstructors /Main method public static void main(String args) new RoundGlyph(5); class RoundGl
21、yph extends Glyph int radius =1; RoundGlyph(int r) radius = r; System.out.println(RoundGlyph.RoundGlyph(), radius= +radius); void draw() System.out.println(RoundGlyph.draw(), radius= +radius); abstract class Glyph abstract void draw(); Glyph() System.out.println(Glyph() before draw(); draw(); System
22、.out.println(Glyph() after draw(); 输出:P2391 Glyph() before draw()2 RoundGlyph.draw(), radius= 0 /调用RoundGlyph的draw是从Glyph过来的,因此此时的radius还未赋值3 Glyph() after draw()4 RoundGlyph.RoundGlyph(), radius= 5第八章 接口8.1 接口8.1.1 接口概述 接口是用来实现类间多重继承的功能的 接口的定义:将完成特定功能的若干属性组织成相对独立的属性集合,该属性集合就是接口 ActionListener接口的功能a
23、ctionPerformed()方法 接口定义的仅仅是实现某一特定功能的一组功能的对外接口和规范,而并没有真正实现这个功能,真正实现在继承这个接口的各个类中完成,因而通常把接口功能的继承称为“实现”。8.1.2 声明接口public interface 接口名extends 父接口名列表 接口是由常量和抽象方法组成 类只能有一个父类,但可实现多个接口(中间用“,”分隔) public接口是公共接口,没有public的接口只能被同一包中的其他类和接口使用 接口中的属性都是public static final的,方法都是public abstract的,方法还可用其他语言写,这时是native修
24、饰的。8.1.3 实现接口一个类要实现接口时,要注意: 在类的声明部分,用implements关键字声明该类将要实现哪些接口 如果实现某接口的类不是abstract的抽象类,则在类的定义部分必须实现指定接口的所有抽象方法 如果实现某接口的类是abstract的抽象类,则它可以不实现该接口所有的方法。但是对于这个抽象类任何一个非抽象的子类面言,它们父类所实现的接口中的所有抽象方法都必须有实在的方法体。 一个类在实现某接口的抽象方法时,必须使用完全相同的方法头,如果所实现的方法与抽象方法有相同的方法名和不同的参数列表,则只是在重载一个新的方法,而不是实现已有的抽象方法。 接口的抽象方法的访问限制符
25、都已指定为public,所以类在实现方法时,必须显式地使用public修饰符,否则将被系统警告为缩小了接口中定义的方法的访问控制范围。程序85 ImplementActionListener.java 程序源代码 说明import java.applet.*;import java.awt.*;import java.awt.event.*;public class ImplementActionListener extends Applet implements ActionListener /该类要实现接口ActionListener TextField password = new Te
26、xtField(Password); Button btn = new Button(Hidden); public void init() add(password); add(btn); btn.addActionListener(this); public void actionPerformed(ActionEvent e) password.setEchoChar(*); password.selectAll(); 8.2 嵌套interfaceclass A interface B void f(); public class BImp implements B public vo
27、id f() private class BImp2 implements B public void f() public interface C void f(); class CImp implements C public void f() private class CImp2 implements C public void f() private interface D void f(); private class DImp implements D public void f() public class DImp2 implements D public void f()
28、public D getD()return new DImp2(); private D dRef; public void receiveD(D d) dRef = d; dRef.f(); interface E interface G void f(); public interface H /redundant public void f(); void g(); /cannot be private within an interface; /!private interface I /接口内的所有元素都是public,这里不能是privatepublic class NestingInterfaces public class BImp implements A.B public void f() class CImp implements A.C public void f() /ca
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1