简体   繁体   English

需要帮助来了解如何在Java中将对象从不同的类连接到主类

[英]Need help understanding how to connect an object from different class to main class in Java

I'm working on a project that has two classes. 我正在一个有两个类的项目中。 The main class outputs the data while the other class does all the calculations. 主类输出数据,而另一类进行所有计算。 Quick summary: The user inputs a lot of numbers (0-200) and then has to type a letter to stop input and calculate the numbers inputted. 快速摘要:用户输入许多数字(0-200),然后必须键入字母以停止输入并计算输入的数字。

The project that I'm working on says that the other class will have a method 我正在研究的项目说另一个类将有一个方法

public boolean inputInformation

that takes in a Scanner object passed in from main(). 接受从main()传入的Scanner对象。 For some reason I'm having a brain fart and am having trouble understanding what that means (translating to code). 由于某种原因,我发了疯,无法理解其含义(转换为代码)。

Extra Q: my other method public boolean addNumber takes in an integer grade <--- what does that mean in code? 额外问:我的另一种方法public boolean addNumber接受整数级<---这在代码中是什么意思?

Your question isn't exactly clear, but I'll take a stab at answering it anyway. 您的问题尚不清楚,但是无论如何我都会回答。 I'm guessing you're working on organizing your code through classes in Java. 我猜您正在通过Java中的类来组织代码。 What you've mentioned is very common practice. 您提到的是非常常见的做法。

"The other class will have a Scanner object passed into it from main". “另一个类将有一个从main传递到其中的Scanner对象”。 What its asking you to do is create a class 'Scanner' which handles the input related operations in a function, 'inputInformation'. 它要求您执行的操作是创建一个类“ Scanner”,该类在函数“ inputInformation”中处理与输入有关的操作。 In your main function, create an object of this class and call the method to take in the input. 在您的main函数中,创建此类的对象并调用方法以接受输入。

The 'boolean' return type probably indicates that you have to return true/false based on whether input has ended or not (by checking for that special character). 'boolean'返回类型可能表明您必须根据输入是否结束(通过检查该特殊字符)来返回true / false。 The query about addNumber refers to the fact that you will be sending an integer to that function as a parameter when calling it. 有关addNumber的查询是指您将在调用该函数时将整数作为参数发送给该函数的事实。

I think you should try to explain your question more clearly, for anyone to give you a little more advice. 我认为您应该尝试更清楚地解释您的问题,以便任何人给您更多建议。

What it's saying is that you should create a Scanner object in the main() method. 这就是说,您应该在main()方法中创建一个Scanner对象。 Your inputInformation method should take a Scanner object as a parameter, so you'd have to call that method from main() passing your Scanner object to it. 您的inputInformation方法应将Scanner对象作为参数,因此您必须从main()调用该方法,并将您的Scanner对象传递给该方法。

As for your second question, it's saying that it should have a parameter of type 'int'. 至于第二个问题,就是说它应该有一个'int'类型的参数。

Class A{

    public boolean method(int x){
        //do something to x
        return true;
    }

};

Class MainClass{

    public static void Main(){
        A someObject = new A();                     // create an object of class A
        boolean result = someObject.method(5);      // call the method of that object 
    }
};

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

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