Java 如何编译和运行从另一个包中的另一个类继承受保护成员的包中的类
这是我第一个放在名为certification的目录中的代码Java 如何编译和运行从另一个包中的另一个类继承受保护成员的包中的类,java,Java,这是我第一个放在名为certification的目录中的代码 package certification; class Parent{ protected int x=9;//protected access } 这是我的另一个代码,放在一个名为other的单独目录中 package other; import certification.Parent; class Child extends Parent{ public void te
package certification;
class Parent{
protected int x=9;//protected access
}
这是我的另一个代码,放在一个名为other的单独目录中
package other;
import certification.Parent;
class Child extends Parent{
public void testIt(){
System.out.println("x is" + x);
}
public static void main(String args[]){
Child n=new Child();
n.testIt();
}
}
但问题是,每当我试图编译类Child时,编译器都会给出以下错误
Child.java:2:包认证不存在
import certification.Parent;
^
java:3:找不到符号
符号:类父级
class Child extends Parent{
^
java:5:找不到符号
符号:变量x
位置:class-other.Child
System.out.println("x is" + x);
^
请帮我纠正一下,让它正常运行。
诚挚的问候。您需要在根目录下编译Child.java(在许多情况下应该是src),并使用一个路径进行编译,假设您的包名是这样的:
javac other/Child.java
因为,
子类
在不同的包中;父类<代码>必须是<代码>公共类<代码>才能在继承时可见。将父级公开为
public class Parent {
并修复输入错误n.voidtestIt();在您的Child#main()
方法中。然后假设以下目录结构
/src/other/Child.java
/src/certification/Parent.java
从/
内部编译Child.java
,如下所示
/$ javac -cp src -d bin src/other/Child.java
/$ java -cp bin other.Child
这将在以下位置创建.class
文件:
/bin/other/Child.class
编辑:编译后,从
/
运行,如下所示
/$ javac -cp src -d bin src/other/Child.java
/$ java -cp bin other.Child
您的类路径应设置为证书的父文件夹。通常,您会有一个包含包结构的“src”文件夹 1) cd到“src”文件夹。
2) 将此位置的类路径设置为
set classpath=%classpath%代码>
3) 将您的父级编译为javac certification\Parent.java
4) 将您的孩子编译为javac other\child.java
这应该行得通。以下内容需要更改
public class Parent{ //parent class should be public
protected int x=9;//protected access
}
及
假设你的文件夹结构是这样的
sources/certification/Parent.java
sources/other/Child.java
另外,将您的父类
设置为公共类
,因为我们试图在包外访问它。
另外,子类应该调用n.testIt()
,而不是n.voidTestIt()
。void是返回类型
课程将是
package certification;
public class Parent{
protected int x=9;//protected access
}
package other;
import certification.Parent;
class Child extends Parent{
public void testIt(){
System.out.println("x is" + x);
}
public static void main(String args[]){
Child n = new Child();
n.testIt();
}
}
遵循以下步骤
- 使用
cd-sources
- 使用命令
javac certification/Parent.java
- 然后使用
javac-classpath编译子类。其他/Child.java
。这里的-classpath
是告诉javac
命令从何处选择Child所需的类的选项。java
和
是当前目录,它是我们的类路径,即源
- 编译成功后,使用
javaother.Child
运行Child
类。这里我们为子项
使用完全限定名
只需导航到您的C驱动器并执行此操作
C:\>cd sources
C:\sources>javac certification/Parent.java
C:\sources>javac -classpath . other/Child.java
C:\sources>java other.Child
x is9
C:\sources>
理想情况下,您应该始终从目录结构的房间编译和启动java类。
java文件中的包名是目录结构。编译时,它们被视为java文件,因此编译时使用目录结构,例如certification/Parent.java
。但是在编译类时,使用包名识别类文件。因此,使用根目录中的完全限定名,即包结构开始的位置。在我们的示例中,sources
是目录,certification
和other
是包。因此,这些类应该被称为certification.Parent
和other.Child
编译Child
的命令是什么?您可能没有正确设置类路径。我正在使用javac Child.java编译Child@selig@user2545332. 将两个源代码编译为:javac-d。Parent.java
。对于Child.java也是如此。@RohitJain我应该从父类的认证目录和子命令的其他目录发出compile命令,还是从这两个目录所在的更高目录发出compile命令placed@user2545332. 如果您比认证目录高一个目录,请运行-javac-d。certification/Parent.java
我尝试了它bro@adithya类正在编译,但是当我ru类子级时,它给出了以下错误子级。java:2:包认证不存在导入认证。Parent;^Child.java:3:找不到符号符号:类父类子类扩展父类{^Child.java:5:找不到符号符号:变量x位置:类其他.Child系统.out.println(“x是”+x);^Child.java:9:找不到符号符号:方法voidtestit()位置:其他类。子类n.voidtestit();^运行时也应如此。如果您的“bin”文件夹与“src”文件夹处于同一级别,则1)cd到“bin”文件夹。以java certification.Parent的身份运行您的类。希望这有帮助。我现在可以编译该类,但问题出在它的运行中。当我尝试运行子类时,它会显示以下内容线程“main”中的错误异常no java.lang.classdeffounderror:child@user2545332作为java-cp bin other.Child运行
。检查更新。谢谢你,先生..我发现了我的错误,我只是用java Child而不是java other.Child运行它