简体   繁体   English

将方法作为参数从一个类传递给Java中另一个类的构造函数

[英]passing a method as an argument from one class to the constructor of another class in java

The question I have is as follows: 我的问题如下:

I have a particular assignment for a java course, where the instructions for the constructor of the second class are as follows: 我有一个Java课程的特别作业,其中第二个类的构造函数的说明如下:

Define a constructor, with parameters that pass in the user entered initial position and initial velocity, and use them to: 定义一个构造函数,使用传递用户输入的初始位置和初始速度的参数,并将其用于:

  • Initialize both the initial position constant and the current position data field to the value of the parameter for the initial position. 将初始位置常数和当前位置数据字段初始化为初始位置的参数值。
  • Initialize both the initial velocity constant and the current velocity data field to the value of the parameter for the initial velocity. 将初始速度常数和当前速度数据字段初始化为初始速度的参数值。

Now the first class's main method: 现在是第一类的主要方法:

Define a main method to: 定义一个主要方法来:

  • Display a description of what the program will do to the user (use the height threshold constant in your description). 显示程序对用户的作用的描述(在描述中使用高度阈值常数)。

  • Use the static methods ( which are also required to prompt for and validate input ) to read the initial position and initial velocity values from the user. 使用静态方法( 提示和验证输入也是必需的 )从用户读取初始位置和初始速度值。

  • Display a couple of blank lines after reading all of the user input. 阅读所有用户输入后,显示几个空白行。 o Create a new object of the FallingItem class, using the initial position and initial velocity read from the user as arguments for the constructor. o使用从用户读取的初始位置和初始速度作为构造函数的参数,创建FallingItem类的新对象。

This is how I have this set up so far: 到目前为止,这就是我的设置方式:

projector class: 投影机类:

      public static void main(String[] args) {

    System.out.println("This program will calculate the position and " 
            + "velocity \n of a falling object until it reaches " 
            + HEIGHT_THRESHOLD + " feet above the ground.");

    initialPosition();

    initialVelocity();

    System.out.println();
    System.out.println();

    FallingItem I = new FallingItem(initialPosition(), initialVelocity());

.... Methods below: ....方法如下:

'public static double initialPosition(){
      Scanner input = new Scanner(System.in);
      boolean validated; //validator for while statements
     double startPosition; //variable for initialPosition
     System.out.println("Please enter the intial position from which" +
            " you intend to drop the object." + "this cannot be lower "+
            "than 600 feet.");
    do{


    startPosition = input.nextDouble();
    if (startPosition > HEIGHT_THRESHOLD){
        validated = true;
    }
    else{
        System.out.print("error, value is too low. Try again");
        validated = false;
    }


    }while (validated = false);
   return startPosition;
   }


   public static double initialVelocity(){
   Scanner userInput = new Scanner(System.in);
   double startVelocity;

   System.out.println("\n please enter the initial velocity, in feet per" + 
           " second. \n This value cannot be less than -500");
   startVelocity = userInput.nextDouble();

   if(startVelocity <= FallingItem.TERMINAL_VELOCITY){
       System.out.print("error, this value cannot be less than -500 feet" +
               " per second. Please re-enter the value");
       startVelocity = userInput.nextDouble();


   }

   return startVelocity;'

Before you ask: yes , there needs to be a do-while loop, and yes , the methods need to prompt like that 在您询问之前: 是的 ,需要有一个do-while循环, 是的 ,方法需要像这样提示

constructor for item class: 项目类的构造函数:

public FallingItem(double initialPosition, double    initialVelocity){

    INITIAL_POSITION = initialPosition; //constant to be filled by user input

    INITIAL_VELOCITY = initialVelocity; //constant to be filled by user input

    currentPosition = initialPosition;

    currentVelocity = initialVelocity;

}

So, in short, I would greatly appreciate any help the community has to offer in finding a way to fulfill the requirements without having the user input prompts duplicate themselves, which is what happens currently, as I am frankly out of ideas. 简而言之,我非常感谢社区为找到一种满足需求而无需用户输入提示重复自己的方法所提供的帮助,而我坦白地说,这是当前正在发生的事情。

regards, firemagnet. 问候,磁铁。

I'll hit you up on a few of your convention breaks... 我会在您的一些会议休息时间打您...

FallingItem I = new FallingItem(initialPosition(), initialVelocity());

Variables in Java are to follow Camel casing while classes follow Pascal casing. Java中的变量遵循Camel大小写,而类遵循Pascal大小写。

  • Camel casing: fallingItem , itemThatIsFalling 骆驼外壳: fallingItemitemThatIsFalling
  • Pascal casing: MyFallingItem , FallingItem , BurgerKingHotdog Pascal大小写: MyFallingItemFallingItemBurgerKingHotdog

Also, your Scanner object keeps being defined locally, or within each method in which you want to only use it once. 同样,您的Scanner对象仍然在本地定义,或者在您只想使用一次的每种方法中定义。 Instead, try defining it globally (above the main method outside of the main method's brackets). 相反,尝试全局定义它(高于main的方法之外的main方法的括号内)。 You can then reference it anywhere in your program without having to initialize a new instance of it just to use it once. 然后,您可以在程序中的任何位置引用它,而不必初始化它的新实例即可使用它一次。 If you're having difficulty understanding global variables, check this out. 如果您遇到困难,了解全局变量,检查出。

Make sure that you're properly formatting your code as well. 确保您还正确设置了代码格式。 The easier you make it to read, the easier it is to detect errors. 您越容易阅读它,就越容易发现错误。 Clean code just makes everyone's life easier. 干净的代码只会使每个人的生活更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM