无法使用 * 通配符访问导入的 class,但在使用全限定名称导入时能够使用相同的 class

Not able to access imported class using * wildcard but able to use same class when imported with full qualified name

    javac javatesting2.java
    javatesting2.java:2: error: cannot access javatesting1
    class testingclass extends javatesting1
                               ^
    bad source file: .\javatesting1.java
    file does not contain class javatesting1
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.


    javatesting2.java:6: error: cannot find symbol
        System.out.println(a);
                           ^
    symbol:   variable a
    location: class testingclass

    2 errors

这是我的代码

    package test1;
    public class javatesting1
    {
         protected int a=45;
         int b=78;
    }
    //I am not able to use the javatesting1 class when i use test1.* instead of test1.javatesting1
    
    // the below code is on another file in the same directory
    import test1.javatesting1;
    class testingclass extends javatesting1
    {
        public void meth1()
        {
            System.out.println(a);
           // System.out.println(b);
        }
    }
    public class javatesting2
    { 
        public static void main(String [] args)
        {
                  testingclass obj=new testingclass();
                  obj.meth1();
        }
    }

您好,欢迎来到 Stack Overflow!

由于您声明 class javatesting1 在包 test1 中,Java 希望在与包同名的文件夹中找到 class 以便扫描它(使用通配符)。 我测试了你的代码,使用通配符 * 导入,文件夹结构如下

folder

  • javatesting2.java
  • test1
    • javatesting1.java

尝试编辑您的文件夹结构。

另外请尽量遵守编码约定:class 名称应使用 CamelCase 命名,例如Java测试1