![](/img/trans.png)
[英]How to import class of one file to another in a subfolder in Java using vscode
[英]Import a custom class from another subfolder JAVA
我在以下目录中有一个班级:
examples.AgentDerivation;
还有另一个
examples.AgentClient;
问题是,第一个需要第二个,但是在尝试编译时出现“符号错误”。
有什么办法可以导入吗? 我想做类似import ../AgentClient;的操作。 但我找不到合适的语法。
请帮忙 !
从something.b
包中something.b
您可以通过import container.random.A.*;
来导入something.a
类import container.random.A.*;
检查以下示例:
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;
}
}
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;
}
}
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 ( ) );
}
}
1个
2
输出是来自B的A类的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.