访问其他类中的对象

时间:2014-05-25 16:50:28

标签: java android class object constructor

我在一本java书中读到,为了访问一个类中的方法,需要使用构造函数创建一个对象。以下是一个例子......

Class1
Class2 MyObject = new Class2();
MyObject.methodCall();

Class2
methodCall(){

}

现在我正在制作一个Android应用程序,我使用了上面的方法,但我得到了错误,然后我读了一个blog,它提供了另一种方法来访问其他类的方法。< / p>

Class1
Class2 myObject;
myObject.myMethod();

Class2
myMethod(){

}

如果没有

,上述方式如何运作
new *InsertClassName*();

修改

public class ButtonClickListener implements OnClickListener{
     VerificationMethods VM;
     VM.authenticateForFunctionsandNumbers();
}


public class VerificationMethods {
    ButtonClickListener BCL;
    public boolean authenticateForFunctionsandNumbers(){
         return trueOrFalse;
    }
}

logcat的:

05-25 17:19:02.195: E/AndroidRuntime(718): FATAL EXCEPTION: main
05-25 17:19:02.195: E/AndroidRuntime(718): java.lang.StackOverflowError
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonCli
05-25 17:19:02.265: W/ActivityManager(61):   Force finishing activity com.example.myfirstcalculator/.MainActivity

代码:

ButtonClickListener.java

public class ButtonClickListener implements OnClickListener{
    EditText outputBoxID;
    String buttonID;
    Context context;
    static String valueofOutput;
    static double NumBeforeFunct;
    static double NumAfterFunct;
    static String rtAnswer;
    static char[] realTimeStringArray;
    static char[] valueofOutputArray;
    static StringBuilder valueofOutputBuild = new StringBuilder();
    static String NBFAS;
    static String NAFAS;
    static StringBuilder realTimeStringBuild = new StringBuilder();

    static String realTimeString;
    VerificationMethods VM = new VerificationMethods();
    //error occuring between here

    public ButtonClickListener(EditText outputBoxID, String buttonID, Context context){

        this.outputBoxID = outputBoxID;
        this.buttonID = buttonID;
        this.context = context; 

    }

    @Override
    public void onClick(View v) {

        valueofOutput = outputBoxID.getText().toString();

    ...

VerificationMethods.java

public class VerificationMethods {
    ButtonClickListener BCL = new ButtonClickListener(null, null, null);
    //error - ...and here

    public boolean authenticateForFunctionsandNumbers(){
        if(ButtonClickListener.valueofOutput.isEmpty()){
            return true;
        }else{
            return false;
        }

    ...

3 个答案:

答案 0 :(得分:3)

首先,快速解释new关键字。

类就像创建对象的蓝图。所以在这段代码中:

public class Cat {

    // This is a constructor
    public Cat() {

    }

    public void purr() {
        System.out.println("prrrrrrrr");
    }

}

这个班叫做Cat。每次我们编写new Cat()时,我们都会创建一个Cat类的新对象(实例)。每次我们创建一个新的cat对象时都会调用构造函数。

让我们说我们希望猫咕噜咕噜。为此,我们首先需要创建一个cat对象(否则,什么是咕噜声?),所以我们可以编写new Cat().purr();

这将为我们创造一只新猫,然后告诉那只猫发出咕噜声。

假设有一种实际上不需要猫工作的方法,例如:

public static void buyCatCollar() {

}

我们可以买一只没有猫的猫项圈。我们不需要真正的猫咪。

我们在此处添加static关键字,这意味着该方法属于,而不属于对象。这意味着我们可以在没有cat对象的情况下编写Cat.buyCatCollar()。这意味着我们不需要写new Cat(),因为我们实际上并不需要猫来买领。

现在转到你的例子。

当你写:

public class ButtonClickListener implements OnClickListener{
    VerificationMethods VM;
    VM.authenticateForFunctionsandNumbers();
}

这意味着VM是VerifcationMethods类的一个实例。当您编写VM.authenticateForFunctionsandNumbers();时,您将获得空指针异常,因为您实际上从未将VM设置为新实例。您需要将其更改为:

public class ButtonClickListener implements OnClickListener{
    VerificationMethods VM = new VerificationMethods();
    VM.authenticateForFunctionsandNumbers();
}

或者,假设您不需要新实例,可以将authenticateForFunctionsandNumbers()方法设为静态,然后写:

public class ButtonClickListener implements OnClickListener{
    VerificationMethods.authenticateForFunctionsandNumbers();
}

但除非您在方法中添加static关键字,否则无法使用此功能。

答案 1 :(得分:0)

我的猜测是你使用静态方法的第二个例子,它不需要用new运算符创建新的对象实例。

这将可以在任何地方使用,您不会使用new创建对象:

public class MyClass1 {
    public static void myMethod1(){
        //something something
    }
}

这需要创建新对象:

public class MyClass2 {
    public void myMethod2(){
         //something something again
    }
}

现在你可以使用:

MyClass1 myObject1;
myObject1.myMethod1();

可是:

MyClass2 myObject2 = new MyClass();
myObject1.myMethod2();

答案 2 :(得分:0)

您必须创建一个类(类型),它是该类型的后续实例化对象的定义。

e.g。

public class MyClass {
    /**
     * Constructor method
     */
    public MyClass() {
        // some code
    }

    /**
     * Some method, that does something.
     */
    public void someMethod() {
        // do something here
    }
}

以后,在代码中的其他地方,您可以使用对该类型构造函数的调用来获取该类型的实例。 e.g。

MyClass instance = new MyClass();

如果向指定的类添加更多方法,则可以在实例化的对象上调用这些方法:

instance.someMethod();