2013-04-16 6 views
5

Więc jestem zdezorientowany, jeśli w Java wyliczenia mogą mieć funkcje. Robię prosty edytor HTML i chciałem użyć enums do reprezentowania tagów html, tak, wiem, że to nie jest najlepszy sposób, aby to zrobić, ale to jest sposób, w jaki moja grupa zdecydowała się go wdrożyć.Metody w Enums

Tak więc próbowałem zrobić coś takiego, ale kiedy próbuję zadzwonić pod numer TagEnums.normalTags() sugeruje to uczynienie go metodą statyczną, myślę, że zastanawiam się, czy to prawda, lub jeśli jest lepszy sposób na wdrożenie go zamiast dokonywania publicznej ArrayList<String> normalTags() do public static ArrayList<String> normalTags()

public enum TagEnum { 
    H1, H2, H3, H4, H5, H6, P, B, I, U, BR, HR, RP, RT, RUBY 

    public ArrayList<String> normalTags(){ 
     String normalTags = "H1, H2, H3, H4, H5, H6, P, B, I, U"; 
     ArrayList<String> tags = new ArrayList<String>(); 
     for(Enum<?> currentEnum: TagEnum.values()){ 
      if(normalTags.contains(currentEnum.toString())){ 
       tags.add("<"+currentEnum.toString().toLowerCase()+">"); 
       tags.add("</"+currentEnum.toString().toLowerCase()+">"); 
      } 
     } 
     return tags; 
    } 
} 
+0

Jest w porządku tak jest :) kiedy mówisz „sugeruje” Zgaduję Eclipse. Sugestie Eclipse są czasem dobre, innym razem głupie. Dobrze, że zdecydowałeś się zapytać! – slezica

+2

Można nawet tworzyć abstrakcyjne metody i zastępować je dla każdego wyliczenia! Zobacz to pytanie: http://stackoverflow.com/questions/14850842/overriding-abstract-method-lub-using-one-single-method-in-enums –

+2

@ uʍopǝpısdn Myślę, że sugestia ma sens - implementacja metody nie jest " t specyficzne dla konkretnej stałej wyliczeniowej. Robi coś statycznie, wykorzystując wszystkie wartości wyliczeniowe. –

Odpowiedz

5

Tak, teksty stałe Java mogą mieć funkcje.

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Przykład z tej strony:

public enum Planet { 
    MERCURY (3.303e+23, 2.4397e6), 
    VENUS (4.869e+24, 6.0518e6), 
    EARTH (5.976e+24, 6.37814e6), 
    MARS (6.421e+23, 3.3972e6), 
    JUPITER (1.9e+27, 7.1492e7), 
    SATURN (5.688e+26, 6.0268e7), 
    URANUS (8.686e+25, 2.5559e7), 
    NEPTUNE (1.024e+26, 2.4746e7); 

    private final double mass; // in kilograms 
    private final double radius; // in meters 
    Planet(double mass, double radius) { 
     this.mass = mass; 
     this.radius = radius; 
    } 
    private double mass() { return mass; } 
    private double radius() { return radius; } 

    // universal gravitational constant (m3 kg-1 s-2) 
    public static final double G = 6.67300E-11; 

    double surfaceGravity() { 
     return G * mass/(radius * radius); 
    } 
    double surfaceWeight(double otherMass) { 
     return otherMass * surfaceGravity(); 
    } 
    public static void main(String[] args) { 
     if (args.length != 1) { 
      System.err.println("Usage: java Planet <earth_weight>"); 
      System.exit(-1); 
     } 
     double earthWeight = Double.parseDouble(args[0]); 
     double mass = earthWeight/EARTH.surfaceGravity(); 
     for (Planet p : Planet.values()) 
      System.out.printf("Your weight on %s is %f%n", 
          p, p.surfaceWeight(mass)); 
    } 
} 
+0

Widziałem to w Internecie, ale nie byłem pewien, czy inna klasa potrzebuje dostępu, jeśli to konieczne być publicznymi metodami zamiast prywatnymi –

+0

@ Wszystkie metody Pawła powinny być prywatne, jeśli są metodami wewnętrznymi, które są szczegółami implementacji, a nie szczegółami kontraktu/czym jest obiekt z punktu widzenia użytkowników obiektu. Metody powinny być publiczne, jeśli definiują, do czego służy obiekt/o co go poprosisz. – Patashu