- 第一题
package net.mindview.polymorphism;
//基类-自行车
class Cycle{
}
子类-单轮车
Unicycle extends Cycle{
}
子类-双轮车
Bicycle extends Cycle{
}
子类-三轮车
Tricycle extends Cycle{
}
public CycleCompetition {
static void ride(Cycle cycle){
}
main(String[] args) {
Bicycle b = new Bicycle();
ride(b);
}
} 你
?
- (略)
- (略)
- 第四题
package net.mindview.polymorphism;
import java.util.Random;
形状
Shape{
draw(){}
erase(){}
}
圆形
Cycle1 extends Shape {
@Override
draw() {
System.out.println("draw cycle");
}
@Override
erase() {
System.erase cycle);
}
}
Square extends Shape {
@Override
draw Square);
}
}
矩形
Triangle extends Shape {
@Override
draw Triangle梯形
Trapezoid extends Shape {
@Override
draw Trapezoid);
}
}
RandomShapeFactory {
Random random = new Random(100);
下面画什么形状呢
public Shape next(){
switch(random.nextInt(4)) {
default:
case 0: return Cycle1();
1: Square();
2: Triangle();
3: Trapezoid();
}
}
}
Shapes {
main(String[] args) {
Shape[] shape = new Shape[9];
RandomShapeFactory factory = RandomShapeFactory();
for(int i=0; i<shape.length; i++){
shape[i] = factory.next();
shape[i].draw();
}
}
}
?
- 第五题
Cycle{
int wheels(){
return 0;
}
}
Unicycle extends Cycle{
1 Bicycle extends Cycle{
2 Tricycle extends Cycle{
3 ride(Cycle cycle){
System.out.println(cycle.wheels());
}
Bicycle();
ride(b);
}
}
?
- 第六题
package net.mindview.polymorphism.Music3;
import net.mindview.polymorphism.Note;
Instrument {
play(Note i){
System.Instrument.play() );
}
String toString () {
return Instrument..getName();
}
void adjust(){System.adjust Instrument);}
}
Wind extends Instrument {
@Override
play(Note i) {
System.Wind.play() );
}
String what (){ Wind; }
adjust Wind);}
}
Stringed extends Instrument {
@Override
Stringed.play() Stringedadjust Stringed Brass extends Instrument {
@Override
Brass.play() Brassadjust Brass Music3 {
曲调
tune(Instrument i){
i.play(Note.MIDDLE_C);
}
tuneAll(Instrument[] i){
for(Instrument ins: i){
tune(ins);
}
}
main(String[] args) {
Instrument[] i = {
Wind(), Stringed(),1)"> Brass()
};
tuneAll(i);
Instrument t = Instrument();
System..println(t);
}
}
?
- 第七题
Other extends Instrument {
@Override
Other.play() Otheradjust Other Brass(),1)"> Other()
};
tuneAll(i);
Instrument t = .println(t);
}
}
?
- 第八题
package net.mindview.polymorphism.Music3;
import java.util.Random;
import net.mindview.polymorphism.Note;
RandomInstrumentFactory {
Random rand = );
Instrument next(){
switch(rand.nextInt()){
Wind();
Stringed();
Brass();
Other();
}
}
}
main(String[] args) {
RandomInstrumentFactory factory = RandomInstrumentFactory();
Instrument[] instruments = new Instrument[10];
0; i<instruments.length; i++){
instruments[i] = factory.next();
}
tuneAll(instruments);
Instrument t = .println(t);
}
}
?
- 第九题
package net.mindview.polymorphism;
啮(nie四声)齿动物
Rodent{
void say(){System.hi,我是 Rodent老鼠
Mouse extends Rodent{
鼹鼠
Gerbil extends Rodent{
大颊鼠
Hamster extends Rodent{
);}
}
啮(nie四声)齿动物大家族
RodentHome {
instroduce(Rodent rodent){
rodent.say();
}
instroduceAll(Rodent[] rodents){
(Rodent r: rodents){
instroduce(r);
}
}
main(String[] args) {
Rodent[] rodents = Mouse(),1)"> Gerbil(),1)"> Hamster()
};
instroduceAll(rodents);
}
}
?
- 第十题
Base{
method1(){
System.method1);
method2();
}
method2(){
System.Base method2 Child extends Base{
@Override
method2() {
System.Child method2 Test10 {
up(Base b){
b.method1();
}
main(String[] args) {
Child c = Child();
up(c);
}
}
?
- afd
- adf
- 第十三题
package net.mindview.polymorphism;
import static net.mindview.util.Print.*;
这是一个共享类
Shared {
private int refCount = ;
long counter = private final long id = counter++;
Shared(){
println(Creating "+this addRef(){
refCount ++;
}
protected dispose(){
if(--refCount ==){
println(Disposing " + );
}
}
@Override
String toString() {
Shared " + id;
}
@Override
finalize() throws Throwable {
if(refCount != Error,引用计数不为0);
}
super.finalize();
}
}
组成类
Composing{
private Shared shared;
long id = counter ++ Composing(Shared shared){
println("+ );
this.shared = shared;
shared.addRef();
}
dispose() {
println(dispose );
shared.dispose();
}
@Override
Composing id;
}
}
引用计数
ReferenceCounting {
main(String[] args) {
TODO Auto-generated method stub
Shared shared = Shared();
Composing[] composing = Composing(shared),1)"> Composing(shared)
};
(Composing c:composing){
c.dispose();
}
}
}
?
- fa
- dfa
- 第十六题
AlertStatus {
@Override
AlertStatus StartStatus extends AlertStatus {
@Override
StartStatus EndStatus extends AlertStatus {
@Override
EndStatus PauseStatus extends AlertStatus {
@Override
PauseStatus Music4 {
AlertStatus as = StartStatus();
pause(){
PauseStatus();
}
end(){
EndStatus();
}
currentStatus(){
System.out.println(as);
}
}
Starship {
main(String[] args) {
Music4 m = Music4();
m.currentStatus();
m.pause();
m.currentStatus();
m.end();
m.currentStatus();
}
}
?
- 第十七题
balance(){
System.Unicycle balance;
}
Bicycle balance main(String[] args) {
Cycle[] cycle = Cycle(),1)"> Unicycle(),1)"> Bicycle(),1)"> Tricycle()
};
((Cycle)cycle[0]).balance();编译不通过
((Unicycle)cycle[]).balance();
((Bicycle)cycle[]).balance();
}
}
?