Java 如何在main方法中从类调用构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18425049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a constructor from a class, in main method
提问by user90000000009
I know this is probably a super simple question but I can't seem to figure it out for the life of me.
我知道这可能是一个超级简单的问题,但我似乎一辈子都无法弄清楚。
As the title states I just want to call the constructor in the Main Method.
正如标题所述,我只想在 Main 方法中调用构造函数。
class Example{
public static void main (String[] args)
{
//I want to call the constructor in the mpgCalculator class....
}
public class mpgCalculator {
public double compute(double mpg, double sizeOfTank)
{
double mpL = mpg * 4;
double tankSizeL = sizeOfTank * 4;
double kmpL = mpL * 1.6;
double result = kmpL / tankSizeL;
return result;
}
}
}
回答by chrylis -cautiouslyoptimistic-
The class mpgCalculator
only has the default constructor since you didn't define one. You don't call the constructor manually; instead, you create a new object and it's called automatically. You probably want this:
该类mpgCalculator
只有默认构造函数,因为您没有定义。您不会手动调用构造函数;相反,您创建一个新对象并自动调用它。你可能想要这个:
mpgCalculator calc = new mpgCalculator();
In this particular case, the compute
function doesn't actually relate to any particular copy of mpgCalculator
, and so you can make it static
if you want (public static double compute
). Then in another method you can say:
在这种特殊情况下,该compute
函数实际上与 的任何特定副本无关mpgCalculator
,因此您可以根据static
需要制作它( public static double compute
)。然后在另一种方法中,您可以说:
double kmpL = mpgCalculator.compute(mpg, size);
回答by dans3itz
To add additionally to the problem; you appear to be using an inner class. You cannot instantiate the class the way you have built it without having an object of Example first.
补充问题;您似乎正在使用内部类。如果首先没有 Example 的对象,则无法按照构建类的方式实例化该类。
EDIT: two examples of possible resolutions to your problem:
编辑:您的问题的可能解决方案的两个示例:
First, static inner class; essentially like a nested class of C++. The inner class does not need to be associated with another instance.
一、静态内部类;本质上就像一个嵌套的 C++ 类。内部类不需要与另一个实例相关联。
public class InnerClassTest {
public static void main(String[] args) {
InnerClass test = new InnerClass();
}
public static class InnerClass {
InnerClass() {
System.out.println("test");
}
}
}
Second, the complicated inner class instantiated from the outer class.
其次,从外部类实例化的复杂内部类。
public class InnerClassTest {
public static void main(String[] args) {
InnerClassTest example = new InnerClassTest();
InnerClass test = example.new InnerClass();
}
public class InnerClass {
InnerClass() {
System.out.println("test");
}
}
}
If you just need a utility function, then follow chrylis's answer.
如果您只需要一个实用程序功能,请按照 chrylis 的回答进行操作。
回答by Ravi Thapliyal
Since, mpgCalculator
(which should be renamed to MpgCalculator
or MPGCalculator
to differentiate it from variable names) is an inner class of Example
you need an outer class instance to instantiate it.
因为,mpgCalculator
(应该重命名为MpgCalculator
或MPGCalculator
将其与变量名称区分开来)是一个内部类,Example
您需要一个外部类实例来实例化它。
double value = new Example().new MpgCalculator().compute();
But, this seems like a utility class in which case you can have it nested by making it static
.
但是,这似乎是一个实用程序类,在这种情况下,您可以通过将它嵌套static
。
public static class MpgCalculator {
// ...
}
You could then create its instance without needing to create an outer class instance too.
然后,您可以创建它的实例,而无需创建外部类实例。
double value = new MpgCalculator().compute();