用户通过类输入并使用另一个类中的输入 [英] user input via a class and use the input in another class
问题描述
我有三个班级。 Circle,GetResult和UserInterface 。
Cirlce Class :
I've three classes viz. Circle, GetResult and UserInterface.
The Cirlce Class:
package set_01_14_pkg;
public class Circle {
private double radius;
// do I need this constructor ??
// public Circle(double r)
// {
// radius = r;
//
// }
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea()
{
return radius*radius*Math.PI;
}
}
GetResult Class :
该类包含主函数
GetResult Class:
This class contains the main function
package set_01_14_pkg;
public class GetResult{
public static void main(String[] args)
{
// get the values of radius of the circle from UserInterface Class and output the result
}
}
UserInterface Class
此类用于询问用户输入。
---------- -------------------------------------------------- -------------------------------
我该怎么做?
1. 当我们要求用户输入时,我们需要在Circle CLASS中声明GET和SET方法。
2 。在这种情况下,我们是否需要Circle CLASS中的构造函数。
3.我怎么能这样做,请帮助。
请回复我,因为我是OOP的新手。
谢谢。
UserInterface Class
This class is used for asking the user for input.
-------------------------------------------------------------------------------------------
How can i do this?
1. When we ask the user for input should we need to declare GET and SET methods in Circle CLASS.
2. Do we need the constructor in Circle CLASS in this case.
3. How could I do this, please help.
Please reply me briefly since I am novice in OOP.
Thanks.
推荐答案
在您的主类中,您应该首先获得圆的半径值。使用该值,您可以创建一个新的Circle
对象并设置其半径,尽管在构造函数中执行此操作可能更好。所以你会有类似的东西:
In your main class you should first get the value for the radius of the circle. With that value you can create a newCircle
object and set its radius, although it may be better to do that in the constructor. So you would have something like:
// in Circle class
Circle(double r)
{
radius = r;
}
// in main
public class GetResult{
public static void main(String[] args)
{
double newradius = // call method to get value from user
Circle mycircle = new Circle(newradius);
System.out.format("Area = %f%n", mycircle.getArea());
}
}
这篇关于用户通过类输入并使用另一个类中的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!