java 外部包无法访问类构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7047173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:27:26  来源:igfitidea点击:

class constructor cannot be accessed by outside package

javaclasspermissionspackageprocessing

提问by Joe

so i can't find a question already asked that answers my exact problem. I have a package that i wrote in eclipse that i exported as a jar library to use in the processing ide. in processing i have a sketch that has a class that calls a class that is in the package. when i try to compile i get this error:

所以我找不到已经提出的问题来回答我的确切问题。我有一个我在 eclipse 中编写的包,我将它导出为 jar 库以在处理 ide 中使用。在处理中,我有一个草图,它有一个类,该类调用包中的一个类。当我尝试编译时出现此错误:

Pirate(processing.core.PApplet,java.lang.String,processing.core.PVector,float,float,int,int) is not public in fsg.pvzclone.pirateunits.Pirate; cannot be accessed from outside package
[javac]   fsg.pvzclone.pirateunits.Pirate pirate1 = new fsg.pvzclone.pirateunits.Pirate(this, "Pirate", pinPoint, pWidth, pHeight, 1, 1).displayPirate();

does anyone know why i can't access the class? I have both the class and constructor set as public, so i'm not sure why the class can't be accessed. any help would be greatly appreciated.

有谁知道为什么我无法访问课程?我将类和构造函数都设置为public,所以我不确定为什么无法访问该类。任何帮助将不胜感激。

CONSTRUCTOR CODE:

构造器代码:

package fsg.pvzclone.pirateunits;

import processing.core.*;

public class Pirate {
   public String pirateClass;
    int classId;
    PVector pinPoint;
    float width;
    float height;
    int id;
    PApplet parent;

    public Pirate(processing.core.PApplet _parent, String _pirateClass, PVector _pinPoint,
        float _width, float _height, int _classId, int _id) {
        parent = _parent;
        pirateClass = _pirateClass;
        classId = _classId;
        width = _width;
        height = _height;
        pinPoint = _pinPoint;
        id = _id;
    }

    public void displayPirate() {
        parent.fill(13, 183, 20, 255);
        parent.stroke(7, 59, 9, 255);
        parent.rect(this.pinPoint.x-this.width/2, (float)(this.pinPoint.y-this.height*.75), this.width, this.height);
    }

}

CODE CALLING PIRATE CLASS:

代码调用海盗类:

fsg.pvzclone.pirateunits.Pirate pirate1 = new fsg.pvzclone.pirateunits.Pirate(this, "Pirate", pinPoint, pWidth, pHeight, 1, 1).displayPirate();

采纳答案by Zds

If the Pirate class is 1:1 like shown, then I suspect you have an old version in classpath somewhere. Double-check the classpath for old versions, both .java and .class.

如果 Pirate 类是如图所示的 1:1,那么我怀疑您在类路径中的某个地方有一个旧版本。仔细检查旧版本的类路径,包括 .java 和 .class。

回答by kneethan

try to create public default constructor in Pirate class and try to call it as:

尝试在 Pirate 类中创建公共默认构造函数并尝试将其调用为:

public class Pirate{
 public Pirate () {}

  ....
}

calling code:

调用代码:

fsg.pvzclone.pirateunits.Pirate emptyPirate1 =  new fsg.pvzclone.pirateunits.Pirate();

And check you still get the same error msg?

并检查您是否仍然收到相同的错误消息?

回答by MByD

Not sure this is your problem, but it is a problem and too long for a comment - you should replace;

不确定这是你的问题,但这是一个问题,评论太长了 - 你应该更换;

fsg.pvzclone.pirateunits.Pirate pirate1 = new fsg.pvzclone.pirateunits.Pirate(this, "Pirate", pinPoint, pWidth, pHeight, 1, 1).displayPirate();

with:

和:

fsg.pvzclone.pirateunits.Pirate pirate1 = new fsg.pvzclone.pirateunits.Pirate(this, "Pirate", pinPoint, pWidth, pHeight, 1, 1);
pirate1.displayPirate();

Since displayPiratereturns nothing, not a pirate.

因为displayPirate什么都不返回,不是海盗。