How to import a class from default package

后端 未结 9 925
盖世英雄少女心
盖世英雄少女心 2020-11-22 11:02

Possible Duplicate: How to access java-classes in the default-package?


I am using Eclipse 3.5 and I have created a project with so

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 11:29

    There is a workaround for your problem. You can use reflection to achieve it.

    First, create an interface for your target class Calculatons :

    package mypackage;
    
    public interface CalculationsInterface {  
        int Calculate(int contextId);  
        double GetProgress(int contextId);  
    
    }
    

    Next, make your target class implements that interface:

    public class Calculations implements mypackage.CalculationsInterface {
        @Override
        native public int Calculate(int contextId);
        @Override
        native public double GetProgress(int contextId);
        static  {
            System.loadLibrary("Calc");
        }
    }
    

    Finally, use reflection to create an instance of Calculations class and assign it to a variable of type CalculationsInterface :

    Class calcClass = Class.forName("Calculations");
    CalculationsInterface api = (CalculationsInterface)calcClass.newInstance();
    // Use it 
    double res = api.GetProgress(10);
    

提交回复
热议问题