gpt4 book ai didi

java - Java多个文件和文件夹层次结构

转载 作者:行者123 更新时间:2023-12-02 11:08:54 25 4
gpt4 key购买 nike

我有多个Java文件,其中大多数功能位于其中一个(Demo)中,而MainTest位于其中。
我尝试打包演示文件并在其他文件中使用该包,但是当我使用终端(javac ...)进行编译时,DemoTest1无法编译,因为它还具有一个带有参数的函数,并且返回的值是演示文件的对象。

Demo.java文件:

package xyz.mypackage;

class Demo{
private double val1;
private double val2;

private void setValues(double num1, double num2){
val1 = num1;
val2 = num2;
}
private double getValue1(){
return (this.val1);
}
private double getValue2(){
return (this.val2);
}
private double getAverage(){
return((this.val1 + this.val2) / 2);
}

public Demo(double double1, double double2){
this.val1 = double1;
this.val2 = double2;
}

static void decrDemo(Demo obj){
obj.setValues(obj.val1 - 1, obj.val2 - 1);
}
}

DemoTest1.java文件:
import java.util.Scanner; 
import xyz.mypackage.Demo;

public class DemoTest1{
static Demo addDemo(Demo obj1, Demo obj2){
return new Demo(obj1.getValue1() + obj2.getValue1(), obj1.getValue2() + obj2.getValue2());
}

static void incrDemo(Demo obj){
obj.setValues(obj.getValue1() + 5, obj.getValue2() + 5);
}

public static void main(){
Demo item = Demo();

System.out.printl("\nPlease enter 2 numbers: ");
Scanner input = new Scanner(System.in);
double val1 = input.nextDouble();
double val2 = input.nextDouble();
demo.setValues(val1, val2);

System.out.printl("\nThe average of the two values entered is: " + item.getAverage());

Demo obj1 = new Demo(5, 7),
obj2 = new Demo(14, 9);

Demo objR = addDemo(obj1, obj2);

incrDemo(obj1);

System.out.printl("\nobjR has the following values. val1: &d\tval2: &d", objR.getValue1(), objR.getValue2());
System.out.printl("\nobj1 has the following values. val1: &d\tval2: &d", obj1.getValue1(), obj1.getValue2());
}
}

错误输出:
Air:Project xyz$ javac Demo.java DemoTest1.java
DemoTest1.java:2: xyz.mypackage.Demo is not public in xyz.mypackage;
cannot be accessed from outside package
import xyz.mypackage.Demo;
^
./Demo.java:3: duplicate class: xyz.mypackage.Demo
class Demo
^
DemoTest1.java:6: cannot access Demo
bad class file: ./Demo.java
file does not contain class Demo
Please remove or make sure it appears in the correct subdirectory of
the classpath.
static Demo addDemo(Demo obj1, Demo obj2)

如果我正确地遵循了说明,它应该可以运行,但是我想Demo不会包含在DemoTest1中,我不理解,因为我使用了包装。还是我需要对每个文件进行不同的编译,而不是对每个文件使用javac ..然后使用Java ..运行DemoTest1?

编辑:更改文件夹层次结构并修复语法错误,使其可以成功编译,但是在尝试运行它时(java DemoTest1),我得到了:
Exception in thread "main" java.lang.NoSuchMethodError: main  

编辑2:异​​常已修复。认为main()就足够了,但是它需要main(String args [])。
现在,它可以按预期运行完美。感谢您的帮助和学习的机会!

解决方案:适用于寻求类似问题的任何人。我在使用软件包时写了xyz.mypackage,所以它必须放在那些文件夹/子文件夹中(我认为,它本身就是软件包的常规语法,而不管文件夹如何),还必须添加默认构造函数,因为我添加了一个特殊的构造函数,我错误地认为,如果没有参数,它将仅使用常规参数。
除了一些较小的语法错误(未正确使用println或不使用%d代替&f等)以外,另一个问题是main()不能为空,它需要一个参数(字符串args [])。

最佳答案

import java.util.Scanner; 

public class DemoTest1 {
static Demo addDemo(Demo obj1, Demo obj2) {
return new Demo(obj1.getValue1() + obj2.getValue1(),
obj1.getValue2() + obj2.getValue2());
}

static void incrDemo(Demo obj) {
obj.setValues(obj.getValue1() + 5, obj.getValue2() + 5);
}

public static void main(String args[]) {
Demo item = new Demo(0, 0);

System.out.println("\nPlease enter 2 numbers: ");
Scanner input = new Scanner(System.in);
double val1 = input.nextDouble();
double val2 = input.nextDouble();
item.setValues(val1, val2);

System.out.println("\nThe average of the two values entered is: "
+ item.getAverage());

Demo obj1 = new Demo(5, 7);
Demo obj2 = new Demo(14, 9);

Demo objR = addDemo(obj1, obj2);

incrDemo(obj1);

System.out.printf("\nobjR has the following values. val1: %.2f\tval2: %.2f",
objR.getValue1(), objR.getValue2());
System.out.printf("\nobj1 has the following values. val1: %.2f\tval2: %.2f",
obj1.getValue1(), obj1.getValue2());
}
}

演示类很好,但是可以通过这种方式尝试。测试类中有一些语法错误。要调用 double 型,您可以使用 %.2f代替具有两位小数位的 double 型,而不是原来的 double 型。另外,您不需要将它们放在新包装中或类似的东西。

关于java - Java多个文件和文件夹层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49681862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com