2013年12月29日 星期日

[Java 教學範例拷貝]- 介面(interface)型態

[Java 教學範例拷貝]- 介面(interface)型態


 


剛才找資料時發現一個的Java 教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。


 


拷貝來源:
http://openhome.cc/Gossip/JavaGossip-V1/


http://openhome.cc/Gossip/JavaGossip-V1/InterfaceType.htm


 









public interface IRequest {
public void execute();
}
public interface IRequest {
void execute();
}
public abstract interface IRequest {
public abstract void execute();
}
public class HelloRequest implements IRequest {
private String name;

public HelloRequest(String name) {
this.name = name;
}

public void execute() {
System.out.printf("Hello! %s!%n", name);
}
}
public class WelcomeRequest implements IRequest {
private String place;

public WelcomeRequest(String place) {
this.place = place;
}

public void execute() {
System.out.printf("Welcome to %s!%n", place);
}
}
public class Test {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
int n = (int) (Math.random() * 10) % 2;
switch (n) {
case 0:
doRequest(
new HelloRequest("caterpillar"));
break;
case 1:
doRequest(new WelcomeRequest("PmWiki"));
}
}
}

public static void doRequest(IRequest request) {
request.execute();
}
}


 


沒有留言:

張貼留言