gpt4 book ai didi

java - 从另一个包调用函数

转载 作者:行者123 更新时间:2023-12-02 08:55:24 26 4
gpt4 key购买 nike

我正在努力调用一个与我的主类位于不同包中的函数。我所说的函数如下:

wtchPrdct = createWatch(键盘);

它位于我的主函数中,用于创建如下所示的 Watch 对象

观看 wtchPrdct = null;

因为我的程序变得很长,因为我向其中添加了一些东西,所以我想从我的主类中去掉一些重量,因此,我创建了一个包“Utils”,我在其中添加了 createWatch( ) 功能。然而我现在很难调用这个函数。如果我坚持原来的行,我会收到“错误找不到符号”:

wtchPrdct = createWatch(键盘);

并改变我计算方法的方式:

创建wtchPrdct = new Create();
wtchPrdct = createWatch(键盘);

但不幸的是遇到了同样的错误。

我忘了提及我已经导入了我的包:导入Utils.Create;

package SportswearProduct;

import DBClass.Model;
import java.sql.SQLException;
import java.util.*;
import java.sql.Date;
import Utils.Create;

public class SSD_CA2 {

public static void main(String[] args) throws SQLException {

System.out.println("\n================== MENU ==================");

do {
/* the user may choose beetwen CRUD interactions provided by the program */
System.out.println("\n1. Create a new Sportswear");
System.out.println("2. Read Watch");
System.out.println("3. Update Watch");
System.out.println("4. Delete Watch");
System.out.println("5. Exit");

System.out.print("\nEnter option: ");

/* optCommand interprets as a string the command from the user, parse it as an Integer
* & checks whether the command matches one of the CRUD interactions below */
String optCommand = keyboard.nextLine();
opt = Integer.parseInt(optCommand);

// The program prompts a command feedback
System.out.println("\nYou chose option " + opt);

// I use a do-while loop that iterates until the user prompts a correct command (see option 5)
switch (opt) {

case 1: {

System.out.println("\nWhat product would you like to add?");
System.out.println("1. A Watch");
System.out.println("2. Runners");

System.out.print("\nEnter option: ");
String createCmd = keyboard.nextLine();
int newOpt = Integer.parseInt(createCmd);

do {

switch (newOpt) {
case 1: {

System.out.println("\n================== Create Watch ==================");
/* spwrPrdct will store the data of an object watch created in the function createWatch to which we pass
* as a parameter the method Scanner() */
Create wtchPrdct = new Create();
wtchPrdct = createWatch(keyboard);




// once returned, prdctWatch id passed to the addWatch() function in Model.java
model.addWatch(wtchPrdct);
break;
}
case 2: {

//same process as above
System.out.println("\n================== Create Runners ==================");
runPrdct = createRunners(keyboard);

model.addRunners(runPrdct);
break;
}
default:
if (opt > 2) {
/* if the user inputs a value greater than 5, instead of stoping, the program prompts an error message
* & iterates again */
System.out.println("\n================== ERROR ==================");
System.out.println("\nWrong command! select one or 2 :");
}
}

break;
} while (newOpt != 1 || newOpt != 2);

}


}



}


这是我的功能:

package Utils;

import SportswearProduct.Sportswear;
import SportswearProduct.Runners;
import SportswearProduct.Watch;
import java.sql.Date;
import java.util.Scanner;

public class Create {

Watch wtchPrdct = null;

/* -------------------- CREATE WATCH FUNCTION -------------------- */
/* createWatch() passes Keyboard()so that the user may assign data to the object */
public static Watch createWatch(Scanner keyboard) {

/* the user enters data that is stored into a variable of matching datat type */
System.out.print("\nEnter the watch brand: ");
String brand = keyboard.nextLine();

System.out.print("Enter the date of sale (yyyy-mm-dd): ");
Date onSale = Date.valueOf(keyboard.nextLine());

...


Watch prdctWatch = new Watch(
brand,
onSale,
price,
movement,
chargingType,
batteryLife,
waterProof
);


return prdctWatch;

我剪掉了最后一部分,因为它有效,并且不想淹没代码。

感谢您的帮助。

最佳答案

我认为您混淆了 Java 和 Pyhon 等脚本语言的语法。

由于您的方法 createWatch 是在类 Create 中声明的,因此当您想要调用该方法时,您应该:

  • 使用类 Create 的实例调用该方法:

    wtchPrdct = wtchPrdct.createWatch(keyboard);

  • 或者通过将方法设置为静态来调用该方法,并使用类名来调用它:

    wtchPrdct = Create.createWatch(keyboard);

关于java - 从另一个包调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60522835/

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