- 第三题
@H_502_3@package net.mindview.interfaces;
abstract class Base{
public Base(){
print();
}
void print();
}
public Test3 extends Base{
private int i = 5;
@Override
print() {
System.out.println(i);
}
static main(String[] args) {
Test3 t = new Test3();
t.print();
}
}
输出结果:
@H_502_3@0
5
调用基类构造方法的时候,只是给子类的成员变量分配了一块内存空间,并将内存空间的值设置为默认值0. 当真正调用子类构造方法之前才会为成员变量赋值.
- 第七题
@H_502_3@package net.mindview.interfaces;
//啮(nie四声)齿动物
interface Rodent{
say();
}
老鼠
Mouse implements Rodent{
void say(){System.out.println("hi,我是 Mouse");}
}
鼹鼠
Gerbil implements Rodent{
大颊鼠
Hamster implements Rodent{
);}
}
RodentHome {
instroduce(Rodent rodent){
rodent.say();
}
instroduceAll(Rodent[] rodents){
for(Rodent r: rodents){
instroduce(r);
}
}
main(String[] args) {
Rodent[] rodents = {
Mouse(), Gerbil(),1)"> Hamster()
};
instroduceAll(rodents);
}
}
?
- 第八题
@H_502_3@package net.mindview.interfaces;
import java.util.Random;
/** 定义一个乐器类 */
Instrucment {
int value = 5; 定义在接口中的成员是static&final的
play(Note n);
adjust();
}
*抽象类 PublicMethod implements Instrucment{
这个方法不用谢,以为继承自Object的类都有toString()方法
public abstract String toString();
*定义n个子类 Wind extends PublicMethod {
void play(Note n){ System.Wind.play() " + n);}
public String toString(){ return Wind.what();}
void adjust(){ System.Wind.adjust() Purcussion extends PublicMethod{
Purcussion.play() Purcussion.what()Purcussion.adjust() Stringed extends PublicMethod{
Stringed.play() Stringed.what()Stringed.adjust() Brass extends Wind{
Brass.play() Brass.adjust() WoodWind extends Wind{
WoodWind.play() WoodWind.what();}
}
Other extends Wind{
Other.play() Other.what()* 定义一个随机乐器生成器 RandomInstrucmentGenerator {
Random rand = new Random(100);
Instrucment next(){
switch(rand.nextInt(6)){
default:
case 0: return Wind();
1: Purcussion();
2: Stringed();
3: Brass();
4: WoodWind();
5: Other();
}
}
}
Music5 {
tune(Instrucment i){
i.play(Note.MIDDLE_C);
i.toString();
}
tuneAll(Instrucment[] e){
(Instrucment i : e){
tune(i);
}
}
static RandomInstrucmentGenerator gen = RandomInstrucmentGenerator();
main(String[] args) {
Instrucment[] orchestra = {
new Wind(),new Purcussion(),new Stringed(),new Brass(),new WoodWind(),new Other()
};*/
Instrucment[] ins = new Instrucment[10];
for(int i=0; i<ins.length; i++){
ins[i] = Music5.gen.next();
}
tuneAll(ins);
}
}
?
- 练习11--这个练习是巩固如何写适配器设计模式
@H_502_3@*
* 字符串反转类
StringReverse {
String name(){
return getClass().getSimpleName();
}
反转
String reverse(String s) {
char[] array = s.tocharArray();
String reverse = "";
for (int i = array.length - 1; i >= 0; i--) {
reverse += array[i];
}
reverse;
}
}
@H_502_3@ StringReverseAdapter implements Processor{
StringReverse stringReverse;
StringReverseAdapter(StringReverse stringReverse){
this.stringReverse = stringReverse;
}
@Override
String name() {
TODO Auto-generated method stub
stringReverse.name();
}
@Override
Object process(Object input) {
stringReverse.reverse((String)input);
}
}
在使用的时候,可以直接调用Apply的process方法
@H_502_3@ main(String[] args) {
Apply.process(new StringReverseAdapter(new StringReverse()),i am lily);
}
Apply方法没有写出来,这个类实在课文内部定义的,可以参考http://www.cnblogs.com/ITPower/p/8550627.html中第二点:解耦的案例一,案例二和案例三. 其中Apply类定义在案例一中。
- 第十二题
@H_502_3@package net.mindview.interfaces;
CanFight {
fight();
}
CanSwim {
swim();
}
CanFly {
fly();
}
CanClimb {
climb();
}
行为特征
ActionCharacter {
fight(){ }
}
Hero extends ActionCharacter implements CanFight,CanSwim,CanFly,CanClimb{
@Override
fly() { }
@Override
swim() { }
@Override
climb() { }
}
冒险
Adventure {
f(CanFly fly){
fly.fly();
}
s(CanSwim swim){
swim.swim();
}
v(CanFight fight){
fight.fight();
}
m(ActionCharacter ac){
ac.fight();
}
p(CanClimb c){
c.climb();
}
main(String[] args) {
Hero hero = Hero();
f(hero);
s(hero);
v(hero);
m(hero);
p(hero);
}
}
?
- 第十四题:这道题的思想和书上p180页的案例思想一样.继承+多次实现接口
@H_502_3@ BaseInterface1 {
a();
b();
}
BaseInterface2 {
c();
d();
}
BaseInterface3 {
e();
f();
}
Interface4 extends BaseInterface1,BaseInterface2,BaseInterface3{
g();
}
Specific implements Interface4{
h(){ }
@Override
a() { }
@Override
b() { }
@Override
c() { }
@Override
d() { }
@Override
e() { }
@Override
f() { }
@Override
g() { }
}
Test14 extends Specific implements Interface4{
aa(BaseInterface1 b1){
b1.a();
b1.b();
}
bb(BaseInterface2 b){
b.c();
b.d();
}
cc(BaseInterface3 b){
b.e();
b.f();
}
dd(Interface4 b){
b.g();
}
main(String[] args) {
Specific specific = Specific();
aa(specific);
bb(specific);
cc(specific);
dd(specific);
}
}
?
- 第十六题
@H_502_3@package net.mindview.interfaces;
import java.io.IOException;
import java.nio.CharBuffer;
import java.util.Random;
import java.util.Scanner;
RandomChar {
Random rand = 47);
Random count = static final char[] captials = ABCDEFGHIJKLMNOPQRSTUVWXYZ.tocharArray();
char[] make(){
StringBuffer sb = new StringBuffer();
0; i<count.nextInt(10); i++){
sb.append(captials[rand.nextInt(captials.length)]);
}
sb.toString().tocharArray();
}
main(String[] args) {
RandomChar rc = RandomChar();
char[] c = rc.make();
System..println(c);
ch:c){
System..print(ch);
}
}
}
AdapterRandomChar implements Readable{
RandomChar rc;
int count;
public AdapterRandomChar(RandomChar rc, count){
this.rc = rc;
this.count = count;
}
@Override
read(CharBuffer cb) throws IOException {
if(count-- == 0){
return -1;
}
StringBuffer sb = c:rc.make()){
sb.append(c);
}
String result = sb.toString() + " ;
cb.append(result);
result.length();
}
main(String[] args) {
Scanner s = new Scanner(new AdapterRandomChar(new RandomChar(),1)">));
while(s.hasNext()){
System.out.print(s.next()+);
}
}
}
?
- 第十八题
@H_502_3@产品
Cycle {
}
Unicycle implements Cycle{
Unicycle(){
System.我是一个Unicycle);
}
}
Bicycle implements Cycle{
Bicycle(){
System.我是一个Bicycle Tricycle implements Cycle{
Tricycle(){
System.我是一个Tricycle工厂类
CycleFactory{
Cycle make();
}
UnicycleFactory implements CycleFactory{
@Override
Cycle make() {
Unicycle();
}
}
BicycleFactory implements CycleFactory{
@Override
Bicycle();
}
}
TricycleFactory implements CycleFactory{
@Override
Tricycle();
}
}
CycleCustomer {
static Cycle serviceCustoemr(CycleFactory fact){
fact.make();
}
main(String[] args) {
Cycle u = serviceCustoemr( UnicycleFactory());
Cycle b = serviceCustoemr( BicycleFactory());
Cycle t = serviceCustoemr( TricycleFactory());
}
}
?
- 第十九题
@H_502_3@package net.mindview.interfaces;
import java.util.Random;
*
* 这时一个抛硬币和掷骰子等类型的框架
*/
ThrowProduct {}
ThrowCorn implements ThrowProduct{
Random rand = ThrowCorn(){
if(rand.nextInt(100) %2 ==){
System.硬币的正面);
}else{
System.硬币的反面);
}
}
}
ThrowDice implements ThrowProduct{
Random rand = ThrowDice(){
System.掷的骰子数是"+rand.nextInt(7));
}
}
ThrowFactory{
ThrowProduct throwOut();
}
ThrowCornFactory implements ThrowFactory{
ThrowCornFactory(){
System.out.print(开始抛硬币:);
}
@Override
ThrowProduct throwOut() {
ThrowCorn();
}
}
ThrowDiceFactory implements ThrowFactory{
ThrowDiceFactory(){
System.开始掷骰子: ThrowDice();
}
}
ThrowFrame {
ThrowProduct service(ThrowFactory f){
f.throwOut();
}
main(String[] args) {
service( ThrowCornFactory());
service( ThrowDiceFactory());
}
}
结果:
@H_502_3@开始抛硬币:硬币的正面
开始掷骰子:掷的骰子数是6
?
(编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|