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();
}
}
- 通过IP定位区域的SQL优化思路(r10笔记第10天)
- Java基础-day06-知识点回顾与练习
- 【Go 语言社区】Golang语言的多核并行化例子
- 一条SQL语句的执行计划变化探究(r10笔记第9天)
- 【Go 语言社区】Web 通信 之 长连接、长轮询(long polling)--转
- Dubbo入门-协议;注册中心
- Oracle 12c PDB浅析(二)(r8笔记第29天)
- 【Go 语言社区】在 Go 语言中,如何正确的使用并发
- Data Guard高级玩法:通过闪回恢复failover备库 (r10笔记第7天)
- ajax跨域问题-web开发必会
- 在线重定义的补充测试(r10笔记第26天)
- 聊聊Data Guard中的DG Broker(r10笔记第24天)
- stuts2返回json数据简单实现
- Linux命令ssh-copy-id (r10笔记第21天)
- 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 文档注释
- Centos7.3服务器搭建LNMP环境的方法
- Linux中解除端口占用的方法
- ubuntu服务器环境下安装python的方法
- 带你入门Linux中size命令的6个例子
- 详解CentOS重启后resolv.conf被重置的解决方案
- scRNA-seq Clustering quality control(二)
- ubuntu环境下安装memcache及启动的方法
- Linux下批量修改服务器用户密码方法步骤
- Linux 常用文本处理命令和vim文本编辑器
- centos7利用yum安装lnmp的教程(linux+nginx+php7.1+mysql5.7)
- linux根据进程号PID查找启动程序的全路径
- linux服务器安装PHP扩展zip,zlib方法
- Lamp环境下设置绑定apache域名的方法分析
- Linux杀不死的进程之CPU使用率700%解决方法
- 解读Linux下ip命令展示的网络连接信息