为什么magpierunner不需要导入和调用magpie,而是直接使用它。
import java.util.Scanner;
/**
* A simple class to run the Magpie class.
*/
public class MagpieRunner2
{
/**
* Create a Magpie, give it user input, and print its replies.
*/
public static void main(String[] args)
{
Magpie2 maggie = new Magpie2();
System.out.println (maggie.getGreeting());
Scanner in = new Scanner (System.in);
String statement = in.nextLine();
while (!statement.equals("Bye"))
{
System.out.println (maggie.getResponse(statement));
statement = in.nextLine();
}
}
}发布于 2015-08-21 02:40:31
如果你的喜喜鸟和喜喜鸟跑步者都在同一个包中,你就不必导入了。仅当您需要访问的类位于不同的包中时才导入。
发布于 2015-08-21 02:41:44
所以首先,你在你的问题中遗漏了很多上下文,这使得回答起来很困难,但我想我知道你想问的是什么,所以就这样吧。
我相信您在同一个包中有两个类,即Magpie和MagpieRunner。Magpie可能是一个应用了某些属性和方法的对象。而MagpieRunner可能是你的main所在的地方,它将实例化你的MagpieRunner对象,并使用这些属性和方法中的一部分。
我相信你的问题是,为什么你不需要导入你的喜羊羊类,你可以直接在MagpieRunner中使用它。
这是因为,如果我上面的假设是正确的,那么您的Magpie类和MagpieRunner类位于同一个包中,并且Magpie是一个公共类。在某些IDE中(例如Eclipse),您可以直接将类添加到Java项目中,然后将其放在“默认包”中。但是包中的任何公共类都是可以访问的,而不需要使用该包中另一个类的导入。
我希望这对你有帮助!
https://stackoverflow.com/questions/32125751
复制相似问题