简体   繁体   中英

Import a custom class from another subfolder JAVA

I have one class in the following directory :

examples.AgentDerivation;

And another one in

examples.AgentClient;

The thing is, the first one needs the second one but I get the "Symbol error" when I try to compile.

Is there any way to do an import ? I want to do something like import ../AgentClient; but I can't find the good syntax for it.

Please help !

From package something.b you can import a something.a class by import container.random.A.*;

Check this example:

Configuration:

包配置

RandomA:

package container.random.A;

public class RandomA
{
    private int randomAID;

    public RandomA()
    {

    }

    /**
     * @return the randomAID
     */
    public int getRandomAID ( )
    {
        return randomAID;
    }

    /**
     * @param randomAID the randomAID to set
     */
    public void setRandomAID ( int randomAID )
    {
        this.randomAID = randomAID;
    }




}

RandomB:

package container.random.B;

import container.random.A.*;

public class RandomB
{
    private int RandomBID;
    private RandomA myForeignRandom;

    public RandomB(RandomA myForeignRandom)
    {
        this.setMyForeignRandom ( myForeignRandom );
    }

    /**
     * @return the randomBID
     */
    public int getRandomBID ( )
    {
        return RandomBID;
    }

    /**
     * @param randomBID the randomBID to set
     */
    public void setRandomBID ( int randomBID )
    {
        RandomBID = randomBID;
    }

    /**
     * @return the myForeignRandom
     */
    public RandomA getMyForeignRandom ( )
    {
        return myForeignRandom;
    }

    /**
     * @param myForeignRandom the myForeignRandom to set
     */
    public void setMyForeignRandom ( RandomA myForeignRandom )
    {
        this.myForeignRandom = myForeignRandom;
    }

}

Test:

package container.random.B;

import container.random.A.RandomA;

public class test
{

    public static void main ( String [ ] args )
    {
        RandomA randomA = new RandomA ( );
        randomA.setRandomAID ( 1 );

        RandomB randomB = new RandomB ( randomA );
        System.out.println ( randomB.getMyForeignRandom ( ).getRandomAID ( ) );

        randomA.setRandomAID ( 2 );
        System.out.println ( randomB.getMyForeignRandom ( ).getRandomAID ( ) );

    }

}

Output:

1

2

The output are the IDs of A class from B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM