java编程思想第四版第八章习题
时间:2022-07-25
本文章向大家介绍java编程思想第四版第八章习题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
- 第一题
package net.mindview.polymorphism;
//基类-自行车
class Cycle{
}
//子类-单轮车
class Unicycle extends Cycle{
}
//子类-双轮车
class Bicycle extends Cycle{
}
//子类-三轮车
class Tricycle extends Cycle{
}
public class CycleCompetition {
public static void ride(Cycle cycle){
}
public static void main(String[] args) {
Bicycle b = new Bicycle();
ride(b);
}
} 你
- (略)
- (略)
- 第四题
package net.mindview.polymorphism;
import java.util.Random;
//形状
class Shape{
public void draw(){}
public void erase(){}
}
//圆形
class Cycle1 extends Shape {
@Override
public void draw() {
System.out.println("draw cycle");
}
@Override
public void erase() {
System.out.println("erase cycle");
}
}
class Square extends Shape {
@Override
public void draw() {
System.out.println("draw Square");
}
@Override
public void erase() {
System.out.println("draw Square");
}
}
//矩形
class Triangle extends Shape {
@Override
public void draw() {
System.out.println("draw Triangle");
}
@Override
public void erase() {
System.out.println("draw Triangle");
}
}
//梯形
class Trapezoid extends Shape {
@Override
public void draw() {
System.out.println("draw Trapezoid");
}
@Override
public void erase() {
System.out.println("draw Trapezoid");
}
}
class RandomShapeFactory {
Random random = new Random(100);
//下面画什么形状呢
public Shape next(){
switch(random.nextInt(4)) {
default:
case 0: return new Cycle1();
case 1: return new Square();
case 2: return new Triangle();
case 3: return new Trapezoid();
}
}
}
public class Shapes {
public static void main(String[] args) {
Shape[] shape = new Shape[9];
RandomShapeFactory factory = new RandomShapeFactory();
for(int i=0; i<shape.length; i++){
shape[i] = factory.next();
shape[i].draw();
}
}
}
- 第五题
package net.mindview.polymorphism;
//基类-自行车
class Cycle{
public int wheels(){
return 0;
}
}
//子类-单轮车
class Unicycle extends Cycle{
public int wheels(){
return 1;
}
}
//子类-双轮车
class Bicycle extends Cycle{
public int wheels(){
return 2;
}
}
//子类-三轮车
class Tricycle extends Cycle{
public int wheels(){
return 3;
}
}
public class CycleCompetition {
public static void ride(Cycle cycle){
System.out.println(cycle.wheels());
}
public static void main(String[] args) {
Bicycle b = new Bicycle();
ride(b);
}
}
- 第六题
package net.mindview.polymorphism.Music3;
import net.mindview.polymorphism.Note;
class Instrument {
public void play(Note i){
System.out.println("Instrument.play() ");
}
public String toString () {
return Instrument.class.getName();
}
void adjust(){System.out.println("adjust Instrument");}
}
class Wind extends Instrument {
@Override
public void play(Note i) {
System.out.println("Wind.play() ");
}
String what (){ return "Wind"; }
void adjust(){System.out.println("adjust Wind");}
}
class Stringed extends Instrument {
@Override
public void play(Note i) {
System.out.println("Stringed.play() ");
}
String what (){ return "Stringed"; }
void adjust(){System.out.println("adjust Stringed");}
}
class Brass extends Instrument {
@Override
public void play(Note i) {
System.out.println("Brass.play() ");
}
String what (){ return "Brass"; }
void adjust(){System.out.println("adjust Brass");}
}
public class Music3 {
//曲调
public static void tune(Instrument i){
i.play(Note.MIDDLE_C);
}
public static void tuneAll(Instrument[] i){
for(Instrument ins: i){
tune(ins);
}
}
public static void main(String[] args) {
Instrument[] i = {
new Wind(),
new Stringed(),
new Brass()
};
tuneAll(i);
Instrument t = new Instrument();
System.out.println(t);
}
}
- 第七题
package net.mindview.polymorphism.Music3;
import net.mindview.polymorphism.Note;
class Instrument {
public void play(Note i){
System.out.println("Instrument.play() ");
}
public String toString () {
return Instrument.class.getName();
}
void adjust(){System.out.println("adjust Instrument");}
}
class Wind extends Instrument {
@Override
public void play(Note i) {
System.out.println("Wind.play() ");
}
String what (){ return "Wind"; }
void adjust(){System.out.println("adjust Wind");}
}
class Stringed extends Instrument {
@Override
public void play(Note i) {
System.out.println("Stringed.play() ");
}
String what (){ return "Stringed"; }
void adjust(){System.out.println("adjust Stringed");}
}
class Brass extends Instrument {
@Override
public void play(Note i) {
System.out.println("Brass.play() ");
}
String what (){ return "Brass"; }
void adjust(){System.out.println("adjust Brass");}
}
class Other extends Instrument {
@Override
public void play(Note i) {
System.out.println("Other.play() ");
}
String what (){ return "Other"; }
void adjust(){System.out.println("adjust Other");}
}
public class Music3 {
//曲调
public static void tune(Instrument i){
i.play(Note.MIDDLE_C);
}
public static void tuneAll(Instrument[] i){
for(Instrument ins: i){
tune(ins);
}
}
public static void main(String[] args) {
Instrument[] i = {
new Wind(),
new Stringed(),
new Brass(),
new Other()
};
tuneAll(i);
Instrument t = new Instrument();
System.out.println(t);
}
}
- 第八题
package net.mindview.polymorphism.Music3;
import java.util.Random;
import net.mindview.polymorphism.Note;
class Instrument {
public void play(Note i){
System.out.println("Instrument.play() ");
}
public String toString () {
return Instrument.class.getName();
}
void adjust(){System.out.println("adjust Instrument");}
}
class Wind extends Instrument {
@Override
public void play(Note i) {
System.out.println("Wind.play() ");
}
String what (){ return "Wind"; }
void adjust(){System.out.println("adjust Wind");}
}
class Stringed extends Instrument {
@Override
public void play(Note i) {
System.out.println("Stringed.play() ");
}
String what (){ return "Stringed"; }
void adjust(){System.out.println("adjust Stringed");}
}
class Brass extends Instrument {
@Override
public void play(Note i) {
System.out.println("Brass.play() ");
}
String what (){ return "Brass"; }
void adjust(){System.out.println("adjust Brass");}
}
class Other extends Instrument {
@Override
public void play(Note i) {
System.out.println("Other.play() ");
}
String what (){ return "Other"; }
void adjust(){System.out.println("adjust Other");}
}
class RandomInstrumentFactory {
Random rand = new Random(100);
public Instrument next(){
switch(rand.nextInt(4)){
default:
case 0: return new Wind();
case 1: return new Stringed();
case 2: return new Brass();
case 3: return new Other();
}
}
}
public class Music3 {
//曲调
public static void tune(Instrument i){
i.play(Note.MIDDLE_C);
}
public static void tuneAll(Instrument[] i){
for(Instrument ins: i){
tune(ins);
}
}
public static void main(String[] args) {
RandomInstrumentFactory factory = new RandomInstrumentFactory();
Instrument[] instruments = new Instrument[10];
for(int i=0; i<instruments.length; i++){
instruments[i] = factory.next();
}
tuneAll(instruments);
Instrument t = new Instrument();
System.out.println(t);
}
}
- 第九题
package net.mindview.polymorphism;
//啮(nie四声)齿动物
class Rodent{
void say(){System.out.println("hi,我是 Rodent");}
}
//老鼠
class Mouse extends Rodent{
void say(){System.out.println("hi,我是 Mouse");}
}
//鼹鼠
class Gerbil extends Rodent{
void say(){System.out.println("hi,我是 Gerbil");}
}
//大颊鼠
class Hamster extends Rodent{
void say(){System.out.println("hi,我是 Hamster");}
}
//啮(nie四声)齿动物大家族
public class RodentHome {
public static void instroduce(Rodent rodent){
rodent.say();
}
public static void instroduceAll(Rodent[] rodents){
for(Rodent r: rodents){
instroduce(r);
}
}
public static void main(String[] args) {
Rodent[] rodents = {
new Mouse(),
new Gerbil(),
new Hamster()
};
instroduceAll(rodents);
}
}
- 第十题
package net.mindview.polymorphism;
class Base{
public void method1(){
System.out.println("method1");
method2();
}
public void method2(){
System.out.println("Base method2");
}
}
class Child extends Base{
@Override
public void method2() {
System.out.println("Child method2");
}
}
public class Test10 {
public static void up(Base b){
b.method1();
}
public static void main(String[] args) {
Child c = new Child();
up(c);
}
}
- afd
- adf
- 第十三题
package net.mindview.polymorphism;
import static net.mindview.util.Print.*;
//这是一个共享类
class Shared {
private int refCount = 0;
private static long counter = 0;
private final long id = counter++;
public Shared(){
println("Creating "+this);
}
public void addRef(){
refCount ++;
}
protected void dispose(){
if(--refCount ==0){
println("Disposing " + this);
}
}
@Override
public String toString() {
return "Shared " + id;
}
@Override
protected void finalize() throws Throwable {
if(refCount != 0){
println("Error, 引用计数不为0");
}
super.finalize();
}
}
//组成类
class Composing{
private Shared shared;
private static long counter = 0;
private final long id = counter ++;
public Composing(Shared shared){
println("Creating "+ this);
this.shared = shared;
shared.addRef();
}
protected void dispose() {
println("dispose " + this);
shared.dispose();
}
@Override
public String toString() {
return "Composing " + id;
}
}
//引用计数
public class ReferenceCounting {
public static void main(String[] args) {
// TODO Auto-generated method stub
Shared shared = new Shared();
Composing[] composing = {
new Composing(shared),
new Composing(shared),
new Composing(shared),
new Composing(shared),
new Composing(shared),
new Composing(shared)
};
for(Composing c:composing){
c.dispose();
}
}
}
- fa
- dfa
- 第十六题
package net.mindview.polymorphism;
class AlertStatus {
@Override
public String toString() {
return "AlertStatus";
}
}
class StartStatus extends AlertStatus {
@Override
public String toString() {
return "StartStatus";
}
}
class EndStatus extends AlertStatus {
@Override
public String toString() {
return "EndStatus";
}
}
class PauseStatus extends AlertStatus {
@Override
public String toString() {
return "PauseStatus";
}
}
class Music4 {
AlertStatus as = new StartStatus();
public void pause(){
as = new PauseStatus();
}
public void end(){
as = new EndStatus();
}
public void currentStatus(){
System.out.println(as);
}
}
public class Starship {
public static void main(String[] args) {
Music4 m = new Music4();
m.currentStatus();
m.pause();
m.currentStatus();
m.end();
m.currentStatus();
}
}
- 第十七题
package net.mindview.polymorphism;
//基类-自行车
class Cycle{
public int wheels(){
return 0;
}
}
//子类-单轮车
class Unicycle extends Cycle{
public int wheels(){
return 1;
}
public void balance(){
System.out.println("Unicycle balance");
}
}
//子类-双轮车
class Bicycle extends Cycle{
public int wheels(){
return 2;
}
public void balance(){
System.out.println("Bicycle balance");
}
}
//子类-三轮车
class Tricycle extends Cycle{
public int wheels(){
return 3;
}
}
public class CycleCompetition {
public static void ride(Cycle cycle){
System.out.println(cycle.wheels());
}
public static void main(String[] args) {
Cycle[] cycle = {
new Cycle(),
new Unicycle(),
new Bicycle(),
new Tricycle()
};
//((Cycle)cycle[0]).balance();//编译不通过
((Unicycle)cycle[1]).balance();
((Bicycle)cycle[2]).balance();
}
}
- InfoPath中repeationg section动态填充数据
- CSS魔法堂:重新认识Box Model、IFC、BFC和Collapsing margins
- jenkins 入门教程(中)
- docker学习(1) 安装
- Ajax等待返回结果时,弹出一个友好的等待提示
- Facebook Like Button在IE上的bug
- CSS魔法堂:hasLayout原来是这样!
- jetty 9 嵌入式开发示例
- 人工智能之头像识别
- mysql : utf8mb4 的问题
- 列举一下项目中使用的产品和技术
- mysql的sql_mode模式
- docker学习(5) 在mac中创建mysql docker容器
- ssh远程执行目标机器上的命令
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- matlab 单元数组和元胞数组
- matlab串联结构体,按属性创建含有元胞数组的结构体
- 一起来学matlab-matlab学习笔记6 性能剖析
- 一起来学matlab-matlab学习笔记5 低级文件输入输出函数
- 一起来学matlab-matlab学习笔记4 数据导入和导出_3 导入和导出电子数据表
- 数据导入和导出_1 MAT文件的保存和读取
- 论文研读-基于决策变量分析的大规模多目标进化算法
- 用python画 pareto front
- 一起来学演化计算-matlab基本数据结构struct
- 一起来学演化计算-matlab基本函数inf, isempty, round, floor, fix
- 一起来学演化计算-matlab基本函数randperm end数组索引
- 论文研读-基于决策变量聚类的大规模多目标优化进化算法
- 一起来学演化计算-matlab基本函数min
- 一起来学演化计算-matlab基本函数find
- 欧拉图和哈密顿图