在Java中在包内使用主类

huangapple go评论70阅读模式
英文:

Using main class inside the package in java

问题

以下是翻译好的部分:

我试图运行文件Demo.java,该文件在同一个包中调用Protection类,但是出现了错误。
这是主类。

package p1;
// 在p1中实例化各个类。
class Demo {
  public static void main(String args[]) {
    Protection ob1 = new Protection();
    //Derived ob2 = new Derived();
    //SamePackage ob3 = new SamePackage();
  }
}

这是我想在主类中使用的类。

package p1;

public class Protection {

  public int n = 1;
  private int n_pri = 2;
  protected int n_pro = 3;
  public int n_pub = 4;

  public Protection() {
    System.out.println("基础构造函数");
    System.out.println("n = " + n);
    System.out.println("n_pri = " + n_pri);
    System.out.println("n_pro = " + n_pro);
    System.out.println("n_pub = " + n_pub);
  }
}

它给出了以下错误:

$ javac Demo.java
Demo.java:6: 错误: 找不到符号
Protection ob1 = new Protection();
^
  符号:   类 Protection
  位置: 类 Demo
Demo.java:6: 错误: 找不到符号
Protection ob1 = new Protection();
                     ^
  符号:   类 Protection
  位置: 类 Demo
2 错误
错误: 编译失败
英文:

I am trying to to run the file Demo.java which is calling Protection class within the same package but it is giving error
This is the main class.

package p1;
// Instantiate the various classes in p1.
class Demo {
  public static void main(String args[]) {
    Protection ob1 = new Protection();
    //Derived ob2 = new Derived();
    //SamePackage ob3 = new SamePackage();
  }
}

And this is the class that I want to use in the main class.

package p1;

public class Protection {

  public int n = 1;
  private int n_pri = 2;
  protected int n_pro = 3;
  public int n_pub = 4;

  public Protection() {
    System.out.println("base constructor");
    System.out.println("n = " + n);
    System.out.println("n_pri = " + n_pri);
    System.out.println("n_pro = " + n_pro);
    System.out.println("n_pub = " + n_pub);
  }
}

It is giving this error:

$ javac Demo.java
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
^
  symbol:   class Protection
  location: class Demo
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
                     ^
  symbol:   class Protection
  location: class Demo
2 errors
error: compilation failed

答案1

得分: 1

你应该使用 javac,而不仅仅是 java

当你使用命令 java 时,你可以执行一个文件,但只能执行该文件中的类。在这里,你有几个文件,所以你应该先编译它们,然后才能使用它们。

请执行以下步骤:

$ mkdir p1
$ mv Demo.java Protection.java p1/
# 编辑 p1/Demo.java,将 `class Demo` 改为 `public class Demo`
$ javac p1/*
$ java p1.Demo

这将会起作用,并且会产生以下结果:

base constructor
n = 1
n_pri = 2
n_pro = 3
n_pub = 4
英文:

You should use javac, not java only

When you use the command java, you can execute a file, but only the classes in that file. Here you have several files, so you should compile them in order to use them.

Do the following:

<!-- language-all: lang-bash -->

$ mkdir p1
$ mv Demo.java Protection.java p1/
# edit p1/Demo.java to change `class Demo` to `public class Demo`
$ javac p1/*
$ java p1.Demo

This worked and resulted in the following:

base constructor
n = 1
n_pri = 2
n_pro = 3
n_pub = 4

答案2

得分: 0

你可以尝试这样做:

  1. 在 p1 中打开命令提示符,使用命令 javac .\Demo.java .\Protection.java;然后你会看到生成了两个 .class 文件。
  2. 使用命令 cd ..,然后你会看到你的包 p1
  3. 使用命令 java p1.Demo,然后你会看到预期的输出。
英文:

You could try this:

  1. open cmd in p1, use javac .\Demo.java .\Protection.java; then you can see two .class files generated
  2. use cd .., then you can see your package p1
  3. use java p1.Demo then you can see the expected output

huangapple
  • 本文由 发表于 2020年8月31日 17:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63667850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定