简体   繁体   English

Java包和访问修饰符

[英]Java Package and Access Modifier

I created a Directory p1 under which I wrote a Demo.java program :- 我创建了一个Directory p1,在其下我写了一个Demo.java程序: -

package p1;

public class Demo {

     public static void main(String[] args){
         Protection ob1 = new Protection();
         Derived ob2 = new Derived();
         SamePackage ob3 = new SamePackage();
    }
}

Under the same Directory I wrote Program for Protection.java, Derived.java, SamePackage.java as follows :- 在同一目录下,我编写了Program for Protection.java,Derived.java,SamePackage.java,如下所示: -

package p1;

public class Protection {

    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_pro = " + n_pro);
        System.out.println("n_pub = " + n_pub);
    }
}  

and

package p1;

class Derived extends Protection {

    Derived(){
        System.out.println("derived Constructor.");
        System.out.println("n = " + n);

        System.out.println("n_pro = " + n_pro);
        System.out.println("n_pub = " +n_pub);
    }
}

and

package p1;

class SamePackage {

    SamePackage(){
        Protection p = new Protection();
        System.out.println("Same Package Constructor");
        System.out.println("n = " + p.n);
        System.out.println("n_pro = " + p.n_pro);
        System.out.println("n_pub = " + p.n_pub);
    }
}  

But when I am doing javac Demo.java in p1 folder I am getting an error that it can't find Protection, Derived and SamePackage symbols. 但是当我在p1文件夹中执行javac Demo.java时,我收到一个错误,它找不到Protection,Derived和SamePackage符号。 What could be wrong here where I am mistaken? 在这里我错了什么可能是错的? Any lead will be thankfully appreciated. 任何领导将不胜感激。

Your problem has nothing to do with access specifiers, its related to missing required classes during compilation. 您的问题与访问说明符无关,它与编译期间缺少必需的类有关。

You need to compile Protection & Derived classes before compiling Demo class. 您需要在编译Demo类之前编译ProtectionDerived类。 As your code in Demo class is using Protection and Derived classes so those classes should be compiled otherwise compiler will not be able to find these classes and will generate error during Demo class compilation. 由于您的Demo类中的代码使用了Protection和Derived类,因此应该编译这些类,否则编译器将无法找到这些类,并且会在Demo类编译期间生成错误。

As per your classes the order of compilation should be: 根据您的类,编译顺序应为:

  1. Protection (independent class) 保护(独立课程)
  2. Derived (depends on Protection) 派生(取决于保护)
  3. Demo (depends on Protection and Derived) 演示(取决于保护和派生)

Compile Protection and Derived first. 首先编译ProtectionDerived Then compile Demo class. 然后编译Demo类。

Rule: 规则:

Compile the composing classes BEFORE compiling the composed classes.

Example: 例:

package com.vivek.one;

class A{



}

package com.vivek.two;

class B{



}


package com.vivek.three;

import  com.vivek.one.A;
import  com.vivek.two.B;

class C{

  A a = new A();
  B b = new B();

}

Compiling: 编译:

javac A.java
javac B.java
javac C.java

Running: 运行:

java C

Compile in this order by exucuting This command >javac -d . 通过exucuting按此顺序编译此命令> javac -d。 FileName.java FileName.java

 Protection-->>SamePackage-->>Derived-->>Demo

After all file compilation use >java p1.Demo.java 在所有文件编译之后使用> java p1.Demo.java

Sure it will work for you 当然它会对你有用

All the answers here were suggesting that I need to compile in certain order to get this problem fixed. 这里的所有答案都表明我需要按特定顺序编译才能解决这个问题。

But what worked for me was when I ran 但对我来说有用的是我跑步的时候

    >javac Demo.java Protection.java SamePackage.java Derived.java

ie I compiled all the various dependent source codes simultaneously. 即我同时编译了所有各种依赖源代码。

I know now what works but now my curiosity forces me to know why this works? 我现在知道什么有效但现在我的好奇心迫使我知道为什么这有效? I am putting this as part of question. 我把这作为问题的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM