我正在努力要求一个位于与主类不同的包中的函数。我正在谈论的功能是以下之一:

wtchPrdct = createWatch(keyboard);

它位于我的主要功能内,以创建如下所示的Watch对象

Watch wtchPrdct = null;

因为我的程序在添加内容时变得很长,所以我想从主类中减轻一些负担,因此,我创建了一个“ Utils”包,并在其中添加了createWatch()函数。
但是,我现在很难调用该函数。
如果我坚持原来的行,就会出现“找不到错误的符号”:

wtchPrdct = createWatch(keyboard);

并更改我这样校准方法的方式:

Create wtchPrdct = new Create();
wtchPrdct = createWatch(keyboard);

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

我忘了提到我已经导入了我的包裹:
import 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);

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


  wtchPrdct = Create.createWatch(键盘);

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

10-15 19:45