我写了这个程序,它创建了一个球体,圆柱体和圆锥体对象,并计算了体积和表面积,但我有一些问题。
1)我在main方法中声明了变量r和h,然后将它们作为参数传递给实例方法。如果变量是本地变量并且在main中声明,为什么我能够这样做。它们不是静止的。
2)在main中声明变量并将它们作为参数传递是否有好处,或者将它们声明为实例变量并通过构造函数初始化它们会更好。
/*All these classes can be created in a separate file*/
import java.util.Scanner;
//Creating sphere class
class Sphere
{
//Method to find volume of sphere. Defined inside the Sphere class
double Volume(double r)
{
return (4.0/3.0) * Math.PI * Math.pow(r,3);
}
//Method to find surface of sphere. Defined inside the sphere class
double Surface (double r)
{
return 4.0 * Math.PI * Math.pow(r,2);
}
}
//Creating cylinder class
class Cylinder
{
//Methods for cylinder objects
double Volume(double r, double h)
{
return Math.PI * Math.pow(r,2) * h;
}
double Surface(double r,double h)
{
return (2 * Math.PI * r * h) + (2* Math.pow(r,2) * Math.PI);
}
}
//Creating cone class
class Cone
{
//Methods for cone objects
double Volume(double r, double h)
{
return Math.PI * Math.pow(r,2) * (h/3.0);
}
double Surface(double r,double h)
{
return Math.PI * r * (r + (Math.sqrt((h*h) + (r*r))));
}
}
//Analogous to our tester class. We could also write this class in a
separate file along with the main method
public class geometryOOP
{
public static void main(String[] args)
{
//Creating Scanner object named in
Scanner in = new Scanner(System.in);
//Prompt
System.out.println("Enter height: ");
//Storing input into h
double h = in.nextDouble();
//Prompt
System.out.println("Enter radius: ");
//Storing input into r
double r = in.nextDouble();
//Creating 3 objects one from each of our classes
Sphere firstsphere = new Sphere();
Cylinder firstcylinder = new Cylinder();
Cone firstcone = new Cone();
//Calling instance methods using dot notation and printing results
System.out.println("The volume of the sphere is : " + Double.toString(firstsphere.Volume(r)));
System.out.println("The surface of the sphere is : " + Double.toString(firstsphere.Surface(r)));
System.out.println("The volume of the cylinder is : " + Double.toString(firstcylinder.Volume(r,h)));
System.out.println("The surface of the cylinder is : " + Double.toString(firstcylinder.Surface(r,h)));
System.out.println("The volume of the cone is : " + Double.toString(firstcone.Volume(r,h)));
System.out.println("The surface of the cone is : " + Double.toString(firstcone.Surface(r,h)));
}
}
答案 0 :(得分:1)
您当前的Shape类可以改进。它们不包含状态。没有状态的类也可以有静态方法,其中调用者不需要创建类的实例来调用这些方法。
为了演示改进的方法,下面是三个不同的Sphere类。
new Sphere1()
创建它的实例,并且它的卷方法需要参数。这个类可以改进(参见下面的Sphere2和Sphere3)。总之,如果您的类具有状态,则创建构造函数并在创建对象时建立状态。然后方法调用不需要状态参数 如果您更喜欢无状态类,那么使用静态方法;在这种情况下创建实例没有任何价值(使用new)。
public class ShapeMain {
public static void main(String[] args) {
// Your code uses this approach
Sphere1 s1 = new Sphere1();
s1.volume(2.0);
// An improvement to above
Sphere2 s2 = new Sphere2(2.0);
s2.volume();
// An better alternative if you want stateless classes
Sphere3.volume(2.0);
}
// Models your class; has NO state
static class Sphere1 {
double volume(double r) {
return (4.0 / 3.0) * Math.PI * Math.pow(r, 3);
}
}
// A class with state (radius)
static class Sphere2 {
private final double r;
public Sphere2(double r) {
this.r = r;
}
double volume() {
return (4.0 / 3.0) * Math.PI * Math.pow(r, 3);
}
}
// A class with no state; use a static method to calculate Sphere volume
static class Sphere3 {
static double volume(double r) {
return (4.0 / 3.0) * Math.PI * Math.pow(r, 3);
}
}
}