Skip to content

Instantly share code, notes, and snippets.

@skRyo
Created July 29, 2012 09:19
Show Gist options
  • Save skRyo/3196957 to your computer and use it in GitHub Desktop.
Save skRyo/3196957 to your computer and use it in GitHub Desktop.
教えてエライ人2
interface Debugprintable {
public static int NO_ERROR = 0;
public static int FILE_ERROR = 1;
public static int MEMORY_ERROR = 2;
public static String PREFIX = "ERROR:";
void debugprint();
}
public class Mynumber implements Debugprintable{
private int a;
private int y;
public Mynumber(int a) {
this.a = a;
this.setCode(a);
}
public int setCode(int x){
if (x == 100){
y = NO_ERROR;
}else if(x == 200){
y = FILE_ERROR;
}else{
y = MEMORY_ERROR;
}
return y;
}
public void debugprint() {
System.out.println(PREFIX + y);
}
}
//Debugprintable interface をメソッドの引数として使用する。
public class Mynumber2 {
public void printDebug(Debugprintable debugprintable) {
debugprintable.debugprint();
}
public static void main(String[] args) {
Mynumber a = new Mynumber(100);
Mynumber b = new Mynumber(200);
Mynumber c = new Mynumber(500);
Mynumber2 x = new Mynumber2();
x.printDebug(a);
x.printDebug(b);
x.printDebug(c);
}
}
@skRyo
Copy link
Author

skRyo commented Jul 29, 2012

@ko-sasaki
Copy link

やりたいことは、インターフェース型パラメータ(引数)を使いたいってことなんだよね。
ならそれで使えてると思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment