如何解决“无法静态引用非静态方法”?

时间:2018-12-05 22:06:49

标签: java

我对“ getClass”有疑问,月食写了这样的消息: “无法从对象类型中静态引用非静态方法getClass()”

这是代码:

int  Longest_Run(int nums[], int siz) {
    //
    int returnValue = 0;
    int freq[101] = { 0 };  //if needed
    int arr_length = 0;


    arr_length = sizeof(nums) / sizeof(int); //WRONG

    __asm {
        //LONGEST RUN. Return value to caller :

        MOV ECX, siz // arr_length //For looping
        //LEA ESI, nums //ptr to array
        mov ESI, nums //ptr to array
        //LEA EDI, nums //ptr to array
        MOV EBX, 0 //Frequency counter
        MOV EAX, [ESI] //First array int in SI

        L1:
        CMP EAX, [ESI] //Compare 1st int with 1st int
            JE L2
            JMP L3
        L4:
            LOOP L1
        JMP CLEAR

            L2 :  //If equal
            INC EBX //Inc counter  
            ADD ESI, 4 //Inc to 2nd item in array
            MOV EAX, [ESI] //Move 2nd item into AX
            JMP L1


        L3 :
        PUSH EBX // Puts counter int on stack
            MOV EBX, 0 // Clears BX to start over
            ADD ESI, 4 //Inc to 3rd item in array
            MOV EAX, [ESI] //Move 3rd item into AX
            JMP L4


            CLEAR:

            POP EDX
            MOV EDX, returnValue


            //END

    }
    return returnValue;
}

谢谢!

3 个答案:

答案 0 :(得分:2)

(如果您的班级名称为Main,则使用Main.class.getResource而不是this.getClass.getResource

阅读this了解更多详细信息。

答案 1 :(得分:1)

静态方法属于class

非静态方法属于该类的instance

调用getResource()时,它与任何实例都没有关联。

做类似的事情

Main.class.getResource("images/pic.png")

您可以在here上找到有关static的更多信息

答案 2 :(得分:0)

在此情况下,静态关键字表示函数“ main”已绑定到类本身,因此您不能像“ getClass()”那样调用非静态的方法,因为那样会与说“ this.getClass()”但this不能引用任何对象,因为您是在静态方法中调用getClass的。因此,为什么必须在静态方法中引用类MainClass.class.getResource()