我试图用consoleDroid初始化类consoleDroid console = new consoleDroid();的一个实例,但它抛出了一个class, interface, or enum expected编译错误。初始化类的正确方法是什么?
案例选择器.java:

package com.caseselector;

import android.app.Activity;
import android.os.Bundle;

consoleDroid console = new consoleDroid();

public class caseSelector extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        console.create();
    }
}

consoledroid.java:
package com.caseselector;

import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;

public class consoleDroid {

    private static String output;
    private Button consoleButton;
    private TextView consoleView;
    private EditText consoleInput;

    public static void create() {
    }

    public static void write(String input) {
    }

    public static void writeLine(String input) {
    }

    public static String readLine(boolean print) {
        return output;
    }
}

最佳答案

只需将实例化下移到类体中:

public class caseSelector extends Activity
{
    consoleDroid console = new consoleDroid();
    // ...
}

顺便说一句:按惯例类名应该以大写字母开头。

08-17 18:23