How to import a class from default package

后端 未结 9 655
盖世英雄少女心
盖世英雄少女心 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:23

    From some where I found below :-

    In fact, you can.

    Using reflections API you can access any class so far. At least I was able to :)

    Class fooClass = Class.forName("FooBar");
    Method fooMethod =
        fooClass.getMethod("fooMethod", new Class[] { String.class });
    
    String fooReturned =
        (String) fooMethod.invoke(fooClass.newInstance(), "I did it");
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-22 11:30
    1. Create a new package.
    2. Move your files from the default package to the new one.
    0 讨论(0)
提交回复
热议问题