当前位置:
首页 > temp > 简明python教程 >
-
《 Java 编程思想》CH08 多态(2)
// Brass.play() n = MIDDLE_C
// Woodwind.play() n = MIDDLE_C
// Instrument
// Percussion
// Stringed
// Instrument
// Woodwind
练习7
class Piano extends Instrument {
@Override
void play(Note n) {
System.out.println("Piano.play() n = " + n);
}
@Override
public String toString() {
return "Piano";
}
@Override
void adjust() {
System.out.println("Adjusting Piano");
}
}
public class Music3 {
public static void tune(Instrument i) {
i.play(Note.MIDDLE_C);
}
public static void tuneAll(Instrument[] instruments) {
for (Instrument instrument: instruments) {
tune(instrument);
}
}
public static void main(String[] args) {
Instrument[] instruments = {
new Wind(),
new Percussion(),
new Stringed(),
new Brass(),
new Woodwind(),
new Piano(),
};
tuneAll(instruments);
for (Instrument instrument: instruments) {
System.out.println(instrument);
}
}
}
// Wind.play() n = MIDDLE_C
// Percussion.play() n = MIDDLE_C
// Stringed.play() n = MIDDLE_C
// Brass.play() n = MIDDLE_C
// Woodwind.play() n = MIDDLE_C
// Piano.play() n = MIDDLE_C
// Instrument
// Percussion
// Stringed
// Instrument
// Woodwind
// Piano
练习8
class InstrumentGenerator {
private Random random = new Random(42);
public Instrument next() {
switch (random.nextInt(6)) {
default:
case 0: return new Wind();
case 1: return new Percussion();
case 2: return new Stringed();
case 3: return new Brass();
case 4: return new Woodwind();
case 5: return new Piano();
}
}
}
public class Music3 {
public static void tune(Instrument i) {
i.play(Note.MIDDLE_C);
}
public static void tuneAll(Instrument[] instruments) {
for (Instrument instrument: instruments) {
tune(instrument);
}
}
public static void main(String[] args) {
Instrument[] instruments = new Instrument[10];
InstrumentGenerator instrumentGenerator = new InstrumentGenerator();
for (int i = 0;i < 10; i++) {
instruments[i] = instrumentGenerator.next();
}
tuneAll(instruments);
for (Instrument instrument: instruments) {
System.out.println(instrument);
}
}
}
// Stringed.play() n = MIDDLE_C
// Brass.play() n = MIDDLE_C
// Wind.play() n = MIDDLE_C
// Stringed.play() n = MIDDLE_C
// Wind.play() n = MIDDLE_C
// Percussion.play() n = MIDDLE_C
// Piano.play() n = MIDDLE_C
// Stringed.play() n = MIDDLE_C
// Percussion.play() n = MIDDLE_C
// Piano.play() n = MIDDLE_C
// Stringed
// Instrument
// Instrument
// Stringed
// Instrument
// Percussion
// Piano
// Stringed
// Percussion
// Piano
练习9
package com.company.ch08;
public class Rodent {
void eat() {
System.out.println("Rodent.eat()");
}
public static void main(String[] args) {
Rodent[] rodents = new Rodent[] {
new Rodent(),
new Mouse(),
new Gerbil(),
new Hamster(),
};
for (Rodent rodent: rodents) {
rodent.eat();
}
}
}
class Mouse extends Rodent {
@Override
void eat() {
System.out.println("Mouse.eat()");
}
}
class Gerbil extends Rodent {
@Override
void eat() {
System.out.println("Gerbil.eat()");
}
}
class Hamster extends Rodent {
@Override
void eat() {
System.out.println("Hamster.eat()");
}
}
// Rodent.eat()
// Mouse.eat()
// Gerbil.eat()
// Hamster.eat()
练习10
package com.company.ch08;
class Base {
void func1() {
func2();
}
void func2() {
System.out.println("Base");
}
}
public class Ex10 extends Base {
@Override
void func2() {
System.out.println("Ex10");
}
public static void main(String[] args) {
Base base = new Ex10();
base.func1();
}
}
// Ex10
因为func2
既不是static也不是final,所以他是动态绑定的,因此基类的 func1 中调用 func2 方法也是调用到导出类的 func2。
练习11
package com.company.ch08;
class Meal {
Meal() {
System.out.println("Meal()");
}
}
class Bread {
Bread() {
System.out.println("Bread()");
}
}
class Cheese {
Cheese() {
System.out.println("Cheese()");
}
}
class Lettuce {
Lettuce() {
System.out.println("Lettuce()");
}
}
class Lunch extends Meal {
Lunch() {
System.out.println("Lunch()");
}
}
class PortableLunch extends Lunch {
PortableLunch() {
System.out.println("PortableLunch()");
}
}
class Pickle {
Pickle() {
System.out.println("Pickle");
}
}
public class Sandwich extends PortableLunch {
private Bread b = new Bread();
private Cheese c = new Cheese();
private Lettuce l = new Lettuce();
private Pickle p = new Pickle();
public Sandwich() {
System.out.println("Sandwich()");
}
public static void main(String[] args) {
new Sandwich();
}
}
练习12
package com.company.ch08;
public class Rodent {
Rodent() {
System.out.println("Rodent");
}
void eat() {
System.out.println("Rodent.eat()");
}
public static void main(String[] args) {
Rodent[] rodents = new Rodent[] {
new Rodent(),
new Mouse(),
new Gerbil(),
new Hamster(),
};
for (Rodent rodent: rodents) {
rodent.eat();
}
}
}
class Mouse extends Rodent {
Mouse() {
System.out.println("Mouse");
}
@Override
void eat() {
System.out.println("Mouse.eat()");
}
}
class Gerbil extends Rodent {
Gerbil() {
System.out.println("Gerbil");
}
@Override
void eat() {
System.out.println("Gerbil.eat()");
}
}
class Hamster extends Rodent {
Hamster() {
System.out.println("Hamster");
}
@Override
void eat() {
System.out.println("Hamster.eat()");
}
}
练习13
package com.company.ch08;
class Shared {
private int refcount = 0;
private static long counter = 0;
private final long id = counter++;
public Shared() {
System.out.println("Create " + this);
}
void addRef() {
refcount++;
}
protected void dispose() {
if(--refcount == 0)
System.out.println("Disposing " + this);
}
@Override
public String toString() {
return "Shared{" +
"id=" + id +
'}';
}
@Override
protected void finalize() throws Throwable {
System.out.println("finalize()");
if (refcount != 0) {
System.out.println("refcount != 0");
}
super.finalize();
}
}
class Composing {
private Shared shared;
private static long counter = 0;
private final long id = counter++;
public Composing(Shared shared) {
System.out.println("Creating " + this);
this.shared = shared;
this.shared.addRef();
}
protected void dispose() {
System.out.println("disposing " + this);
shared.dispose();
}
@Override
public String toString() {
return "Composing{" +
"id=" + id +
'}';
}
}
public class ReferenceCounting {
public static void main(String[] args) {
// Shared shared = new Shared();
// Composing[] composings = {
// new Composing(shared), new Composing(shared),
// new Composing(shared), new Composing(shared),
// new Composing(shared)
// };
//
// for (Composing composing: composings) {
// composing.dispose();
// }
new Composing(new Shared());
System.gc();
}
}
练习14
练习15
package com.company.ch08;
class Glyph {
void draw() {
System.out.println("Glyph.draw()");
}
Glyph() {
System.out.println("Glyph() before draw()");
draw();
System.out.println("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
radius = r;
System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
}
@Override
void draw() {
System.out.println("RoundGlyph.draw(), radius = " + radius);
}
}
class RectangularGlygh extends Glyph {
private int length;
RectangularGlygh(int length) {
this.length = length;
System.out.println("RectanguarGlygh length = " + length);
}
@Override
void draw() {
System.out.println("RectanguarGlygh.draw() length = " + length);
}
}
public class PolyConstructors {
public static void main(String[] args) {
new RectangularGlygh(10);
}
}
// Glyph() before draw()
// RectanguarGlygh.draw() length = 0
// Glyph() after draw()
// RectanguarGlygh length = 10
练习16
package com.company.ch08;
class Status {
void func() {}
}
class StatusA extends Status {
void func() {
System.out.println("Status A");
}
}
class StatusB extends Status {
void func() {
System.out.println("Status B");
}
}
class StatusC extends Status {
void func() {
System.out.println("Status C");
}
}
class AlterStatus {
Status status = new StatusA();
void A() {
status = new StatusA();
}
void B() {
status = new StatusB();
}
void C() {
status = new StatusC();
}
void call() {
status.func();
}
}
public class Starship {
public static void main(String[] args) {
AlterStatus alterStatus = new AlterStatus();
alterStatus.call();
alterStatus.B();
alterStatus.call();
alterStatus.C();
alterStatus.call();
alterStatus.A();
alterStatus.call();
}
}
// Status A
// Status B
// Status C
// Status A
练习17
package com.company.ch08;
public class Cycle {
void run() {
System.out.println("Cycle run");
}
int wheels() {
return 0;
}
}
class Unicycle extends Cycle {
@Override
void run() {
System.out.println("Unicycle run");
}
@Override
int wheels() {
return 1;
}
void balance() {}
}
class Bicycle extends Cycle {
@Override
void run() {
System.out.println("Bicycle run");
}
@Override
int wheels() {
return 2;
}
void balance() {}
}
class Tricycle extends Cycle {
@Override
void run() {
System.out.println("Tricycle run");
}
@Override
int wheels() {
return 3;
}
}
class Test {
static void ride(Cycle c) {
c.run();
}
public static void main(String[] args) {
Cycle[] cycles = new Cycle[]{new Unicycle(), new Bicycle(), new Tricycle()};
// for(Cycle cycle: cycles) {
// cycle.balance(); // 无法调用
// }
Unicycle unicycle = (Unicycle)cycles[0];
Bicycle bicycle = (Bicycle)cycles[1];
Tricycle tricycle = (Tricycle)cycles[2];
unicycle.balance();
bicycle.balance();
// tricycle.balance(); //无法调用
}
}
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数