如何从其他类访问“package”的java文件中导入java类

时间:2022-03-09 16:02:26

explane me please how can I do import class in case below. I have two java file (this is example). First:

解释我,请问如何在下面的情况下进行导入课程。我有两个java文件(这是示例)。第一:

package com.mypackage.task

public class TaskF7 {

    public static void main(String[] args) {
    }
}

class Database {
}

class Employee {
}

Second file:

package com.mypackage.test

import com.mypackage.task.TaskF7.*; // import does not work
import com.mypackage.task.TaskF7.Employee; // import does not work

public class TestF7 {

    public static void main(String[] args) {
        testTask();
    }

    // so, my IDEA mark as red <Employee> below
    public static void testTask(List<Employee> expected, List<Employee> actual> ) {
    if (Arrays.deepEquals(expected.toArray(), actual.toArray())) {
        System.out.println("passed");
    } else {
        System.out.println("failed: expected " + expected + ", actual " + actual);
    }
}

class Employee have is package access.

class Employee具有包访问权限。

Please note: in task write: "place all solution classes in one file (not as inner classes). " Not inner classes

请注意:在任务写入中:“将所有解决方案类放在一个文件中(而不是内部类)。”不是内部类

3 个解决方案

#1


1  

You cannot access a class with package access outside the package. You could change Employee class to public or change the package of my.package.test

您无法访问包外部具有包访问权限的类。您可以将Employee类更改为public或更改my.package.test的包

#2


0  

You can access this through make it "Inner Class" but you should make it public

您可以通过将其设为“内部类”来访问它,但您应该将其公之于众

public class TaskF7 {
   public class Employee {
   }

    public static void main(String[] args) {
    }
}

public class TaskF7{
   public static void testTask(List<TaskF7.Employee> expected,   
                               List<TaskF7.Employee> actual ) {
      if (Arrays.deepEquals(expected.toArray(), actual.toArray())) {
          System.out.println("passed");
      } else {
          System.out.println("failed: expected " + expected + ", actual " +   
          actual);

      }
   }
}

Now your IntellijIDEA will not give any red sign underline error :)

现在您的IntellijIDEA不会给出任何红色标记下划线错误:)

#3


0  

I solve it this way: 1.

我这样解决:1。

 public static void testTask(Object expected, Object actual> ) {
    if (expected.equals(actual)) {
        System.out.println("passed");
    } else {
        System.out.println("failed: expected " + expected + ", actual " + actual);
}

2.

Class Employee {

    @Override 
    equals(){
    }

    @Override
    hashCode(){
    }
}

#1


1  

You cannot access a class with package access outside the package. You could change Employee class to public or change the package of my.package.test

您无法访问包外部具有包访问权限的类。您可以将Employee类更改为public或更改my.package.test的包

#2


0  

You can access this through make it "Inner Class" but you should make it public

您可以通过将其设为“内部类”来访问它,但您应该将其公之于众

public class TaskF7 {
   public class Employee {
   }

    public static void main(String[] args) {
    }
}

public class TaskF7{
   public static void testTask(List<TaskF7.Employee> expected,   
                               List<TaskF7.Employee> actual ) {
      if (Arrays.deepEquals(expected.toArray(), actual.toArray())) {
          System.out.println("passed");
      } else {
          System.out.println("failed: expected " + expected + ", actual " +   
          actual);

      }
   }
}

Now your IntellijIDEA will not give any red sign underline error :)

现在您的IntellijIDEA不会给出任何红色标记下划线错误:)

#3


0  

I solve it this way: 1.

我这样解决:1。

 public static void testTask(Object expected, Object actual> ) {
    if (expected.equals(actual)) {
        System.out.println("passed");
    } else {
        System.out.println("failed: expected " + expected + ", actual " + actual);
}

2.

Class Employee {

    @Override 
    equals(){
    }

    @Override
    hashCode(){
    }
}