練習問題6.5
単純に色を返せば良いだけなので、この場合は定数固有のメソッドを使用する必要は無いんじゃないかと思う。
enum Color { GREEN("GREEN"){ String getColor(){ return this.name; } }, YELLOW("YELLOW"){ String getColor(){ return this.name; } }, RED("RED"){ String getColor(){ return this.name; } }; String name; Color(String name){ this.name = name; } abstract String getColor(); }
練習問題6.4
enum Color { GREEN("GREEN"), YELLOW("YELLOW"), RED("RED"); String name; Color(String name){ this.name = name; } abstract String getColor(){ return this.name; } }
練習問題6.2
public class Vehicle implements Cloneable{ enum Turn{LEFT,RIGHT} private double speed; private double angle; private String owner; private long VehicleID; private static long NextVehicleID=0; private EnergySource es; Vehicle(){ VehicleID = NextVehicleID; NextVehicleID++; } Vehicle(String owner){ this(); this.owner = owner; } Vehicle(EnergySource es){ this(); this.es = es; } public void start(){ if(es.empty()) System.out.println("Empty!"); else System.out.println("Start!"); } public void print(){ System.out.println(this); } public static long getMaxID(){ return NextVehicleID - 1; } public String toString(){ String vehicle = "ID : " + VehicleID + "\n" + "Speed : " + speed + "\n" + "Angle : " + angle + "\n" + "Owner : " + owner + "\n"; return vehicle; } public double getSpeed(){return speed;} public double getAngle(){return angle;} public String getOwner(){return owner;} public String setOwner(String owner){return this.owner = owner;} public void changeSpeed(double speed){this.speed = speed;} public void stop(){speed = 0.0;} public void turn(double angle){ this.angle += angle; } public void turn(Turn turn){ switch(turn){ case LEFT: turn(-Math.PI/2);break; case RIGHT: turn(Math.PI/3);break; } } public Vehicle clone() throws CloneNotSupportedException { try{ return (Vehicle)super.clone(); }catch(CloneNotSupportedException e){ throw new InternalError(e.toString()); } } public static void main(String args[]){ Vehicle vc = new Vehicle("Taro"); vc.turn(Turn.LEFT); System.out.println(vc); } }
enumを使う利点は、
public static final int TURN_LEFT=0; public static final int TURN_RIGHT=1; ||<< こう書く代わりに >|java| enum Turn{LEFT,RIGHT} ||<< こう宣言できて、使うときにも分かりやすい(switch文でも使えるし)と言う事だろうか。 *[勉強][Java][プログラミング言語Java]練習問題6.3 >|java| public interface Verbose { enum Verbosity{SILENT,TERSE,NORMAL,VERBOSE} void setVerbosity(Verbosity level); Verbosity getVelbosity(); }
練習問題6.1
public enum Week { SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY }
enum Color {
GREEN,YELLOW,RED
}
練習問題5.2
非常に美しくない><
History内部クラスでのAccountの持ち方は、Listとかで持った方が良いのだろうか??10件しか持ってはいけないという事だったので配列で処理してみたけどなんかしっくりこない。。
BankAccount.java
public class BankAccount { private long number; private long balance; private History history = new History(); public class Action { private String act; private long amount; Action(String act, long amount){ this.act = act; this.amount = amount; } public String toString(){ return number + " : " + act + " " + amount; } } public class History { private int currentIndex=-1; private int index=0; private Action[] his = new Action[10]; public Action next(){ if(currentIndex++ < his.length -1) return his[currentIndex]; else return null; } public void add(Action act){ if(index < 10) his[index] = act; else{ for(int i=0; i<his.length-1 ;i++){ his[i] = his[i+1]; } his[his.length-1]=act; } index++; } public void show(){ System.out.println(this.next()); } } public void deposit(long amount){ balance += amount; this.history.add(new Action("deposit",amount)); } public void withdraw(long amount){ balance -= amount; this.history.add(new Action("withdraw",amount)); } public History history(){ return this.history; } }
Main.java
public class Main { public static void main(String args[]){ BankAccount ba = new BankAccount(); ba.deposit(100); ba.deposit(110); ba.deposit(120); ba.withdraw(130); ba.deposit(140); ba.deposit(150); ba.withdraw(160); ba.deposit(170); ba.deposit(180); ba.deposit(190); ba.withdraw(200); ba.deposit(210); System.out.println(ba.history().next()); System.out.println(ba.history().next()); System.out.println(ba.history().next()); System.out.println(ba.history().next()); System.out.println(ba.history().next()); System.out.println(ba.history().next()); System.out.println(ba.history().next()); System.out.println(ba.history().next()); System.out.println(ba.history().next()); System.out.println(ba.history().next()); System.out.println(ba.history().next()); } }
結果
0 : deposit 120
0 : withdraw 130
0 : deposit 140
0 : deposit 150
0 : withdraw 160
0 : deposit 170
0 : deposit 180
0 : deposit 190
0 : withdraw 200
0 : deposit 210
null
>Historyはネストしたクラスでかつ非staticであるべき。BankAccountインスタンス毎にHistoryオブジェクトのインスタンスがいるから。
練習問題5.1
AttributedインタフェースはAttrクラスに対する操作を定義しているため、AttrクラスがAttributedクラスのネストした型であるべき。