-
-
Save skRyo/3217011 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 { | |
enum ErrStatus { | |
NO_ERROR(0), FILE_ERROR(1), MEMORY_ERROR(2); | |
private final int statusCode; | |
private ErrStatus(int statusCode) { | |
this.statusCode = statusCode; | |
} | |
@Override | |
public String toString() { | |
return Integer.toString(statusCode); | |
} | |
} | |
String PREFIX = "ERROR:"; | |
void debugprint(); | |
} | |
class Mynumber implements Debugprintable{ | |
private int a; | |
private ErrStatus y; | |
public Mynumber(int a) { | |
this.a = a; | |
setCode(a); | |
} | |
private void setCode(int x){ | |
switch (x) { | |
case 100: | |
y = ErrStatus.NO_ERROR; | |
break; | |
case 200: | |
y = ErrStatus.FILE_ERROR; | |
break; | |
default: | |
y = ErrStatus.MEMORY_ERROR; | |
} | |
} | |
@Override | |
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