Created
July 29, 2012 09:19
-
-
Save skRyo/3196957 to your computer and use it in GitHub Desktop.
教えてエライ人2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
やりたいことは、インターフェース型パラメータ(引数)を使いたいってことなんだよね。
ならそれで使えてると思います。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://goo.gl/yJofe