APCS Magpie和Magpie赛跑者致电

时间:2015-08-20 18:30:48

标签: java

为什么magpierunner不需要导入和调用喜鹊,而只需使用它。

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();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

如果您的喜鹊和喜鹊跑步者都在同一个包裹中,您不必进口。仅当您需要访问的类位于不同的包中时才导入。

答案 1 :(得分:0)

所以首先你在你的问题中留下了许多难以回答的背景,但我认为我得到了你想要的东西,所以在这里。

我相信你在同一个包里面有两个类,Magpie和MagpieRunner。喜鹊可能是一个应用了一些属性和方法的对象。 MagpieRunner可能就是你的主要生活,然后将实例化你的Magpie对象,并使用Magpie暴露给它的一些属性和方法。

我相信你的问题是为什么你不需要导入你的Magpie类,你可以直接在MagpieRunner中使用它。

这是因为,如果我的上述假设是正确的,那么你的Magpie课程和MagpieRunner课程就住在同一个课程中,而Magpie是一个公共课程。在某些IDE(例如Eclipse)中,您可以直接将类添加到Java项目中,然后它将存在于“默认包”中。但是,包中的任何公共类都可以在不使用该包中另一个类的导入的情况下访问。

我希望有所帮助!

相关问题