迭代继承的类并调用它们的静态方法

时间:2015-08-12 18:29:58

标签: java inheritance static-methods

我想迭代一些继承自同一个超类的类。 它们都有相同的静态方法,但我该怎么称呼呢?

我试过这样,但这不起作用:

public abstract class Tower {
    private static int text = 5;

    public static int getText() {
        return text;
    }
}

public class aTower extends Tower {
    private static int text = 10;

    public static int getText() {
        return text;
    }
}

public class Main {
    public static void main(String [] args) {
        LinkedList<Class<?extends Tower>> towers = new LinkedList<>();
        towers.add(aTower.class);

        for (int i = 0; i < towers.size(); i++) {
            towers.get(i).getText(); //Does not work
        }
    }
}

上下文

我希望有一个从Tower继承的类列表,用于调用它们的静态方法,例如他们的质地。还有其他办法吗?

编辑:主要目标是,我将在列表中包含许多不同的塔式课程,并且每个塔级都应该有一个菜单。要绘制菜单,我想获得例如纹理,名称等。当您单击菜单条目时,您应该获得特定塔的对象,您可以在某处构建它。但我不喜欢有一个或多或少未使用的实例列表的想法,因此我认为使用静态方法是正确的解决方案。

3 个答案:

答案 0 :(得分:1)

查看Java Interfaces虽然已经说明它们不是静态方法。

 public interface ITower {
   public String getText();
 }

从这一点开始,您可以定义实现 ITower 的塔式对象,然后在 main 中定义:

public class Main {
   public static void main(String [] args) {
     List<ITower> towers = new LinkedList<>();

     // create your tower objects and add them to the list
     towers.add(new ATower());
     towers.add(new BTower());

     for (ITower iObj : towers) {
         iObj.getText();
     }
   }
}

答案 1 :(得分:0)

通过将静态方法转换为实例方法,并在每个子类中使用静态字段,您可以使其工作:

public abstract class Tower {
    private static int text = 5;

    public int getText() {
        return text;
    }
}

public class aTower extends Tower {
    private static int text = 10;

    @Override public int getText() {
        return text;
    }
}

public class Main {
    public static void main(String [] args) {
        LinkedList<aTower> towers = new LinkedList<>();
        towers.add(new aTower());

        for (int i = 0; i < towers.size(); i++) {
            towers.get(i).getText(); 
        }
    }
}

在原始main()方法中,您为List个对象创建了Class个实例。我认为你真的打算包含aTower个实例。这很重要,因为课程Class不会有getText()方法。

答案 2 :(得分:-1)

多态性不适用于静态方法。为什么你希望<android.support.design.widget.FloatingActionButton app:layout_behavior="com.codepath.floatingactionbuttontest.ScrollAwareFABBehavior" /> 是静态的?

如果private Object sendObject(URL servlet, Serializable obj) throws Exception { HttpURLConnection connection = null; InputStream inputstream = null; ObjectOutputStream objectoutputstream = null; ObjectInputStream result = null; Object payload = null; try { // Set the HostnameVerifier regardless of whether SSL is enabled. HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String urlHostName, SSLSession session) { return true; } }; HttpsURLConnection.setDefaultHostnameVerifier(hv); connection = (HttpURLConnection) servlet.openConnection(); // Prepare for both input and output connection.setDoInput(true); connection.setDoOutput(true); // Turn off caching connection.setUseCaches(false); connection.setRequestMethod("POST"); // Set the content type to be application/x-java-serialized-object connection.setRequestProperty("Content-Type", "application/x-java-serialized-object"); setupHeaderAttributes(getHttpHeaders()); setupSessionCookies(getHttpHeaders()); // Load/add httpHeaders addHeadersToConnection(connection, getHttpHeaders()); // Write the serialized object as post data objectoutputstream = new ObjectOutputStream(connection.getOutputStream()); objectoutputstream.writeObject(obj); objectoutputstream.flush(); // Get ready to receive the reply. inputstream = connection.getInputStream(); setHttpStatus(connection.getResponseCode()); if (getHttpStatus() == HttpURLConnection.HTTP_OK) { ... 中有无参数构造函数,则可以使用反射从类名创建实例。

getText()