bergerak ke Enums….

penjelasan enum menurutku adalah semacam array yang berupa class(Pendapatku)…untuk lebih jelasnya bisa dilihat di enums. langsung ke contoh enums..

example 1:

public class TrySimpleEnum {
    public enum day{senin,selasa,rabu,kamis,jumat,sabtu,minggu}

    public static void main(String[] args){
        for(day d : day.values()){
            System.out.print(d + " ");
        }
    }
}

example 2

public class AritmaticEnum {
    public enum arit{
        PLUS { double eval(double x,double y){ return x+y;}},
        MINUS{ double eval(double x,double y){ return x-y;}};
        abstract double eval(double x,double y);
    }
    public static void main(String[] args){
        double x,y;
        x = 3.0;
        y = 3.0;
        for(arit p : arit.values()){
            System.out.println(p.eval(x, y));
        }

    }
}

example 3

public enum Berat {
    ANTON(60,160),
    DONI(20,150),
    AMRON(30,180);

    private final int berat;
    private final int tinggi;

    Berat(int berat,int tinggi){
        this.berat = berat;
        this.tinggi = tinggi;
    }
    //getter and sete
    public int berat(){return berat;}
    public int tinggi(){return tinggi;}

    //berat ideal with wrong formula
    public static final int formula = 300;
    private int total;
    public int totalberat(){
        return total = berat+tinggi;
    }
    @Override
    public String toString(){
        return String.valueOf(total);
    }
}

public class Orang {
    public static void main(String[] args){
        int x,y;
        for(Berat b: Berat.values()){
            System.out.println(b + " " +b.totalberat());
        }
    }
}

jika penjelasan kurang silahkan hubungi google/ saya…jika perlu..:P

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s