Java 如何返回类 - 输入方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45217528/
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 return class - type in a method
提问by Tayyab
I have a class
我有一堂课
public class Engine {
Double engineSize;
public Engine(){
this.engineSize = 1.0;
}
public Double getEngineSize() {
return engineSize;
}
}
I have another class:
我还有一堂课:
public class ModelT {
public Engine getEngine() {
return null;
}}
Now I want to pass this test:
现在我想通过这个测试:
@Test
public void shouldHaveTheCorrectEngineSize(){
assertThat(modelT.getEngine().getEngineSize(), is(1.0));
}
I have the difficulty how can I return the Engine type in a method, I tried several ways but knowledge is limited because I am beginner to java.. Could you please tell me how could i do this? and what is the name of concept that i could further read on..
我有困难如何在方法中返回引擎类型,我尝试了几种方法,但知识有限,因为我是 Java 初学者..你能告诉我我该怎么做吗?我可以进一步阅读的概念的名称是什么..
回答by OldProgrammer
You need to store an instance of Engine in your ModeT class:
您需要在 ModeT 类中存储 Engine 的实例:
public class ModelT {
private Engine eng;
ModelT( Engine eng){
engine = eng;
}
public Engine getEngine() {
return eng;
}}
then in you Main - create an Engine object and pass in to modelt:
然后在你的 Main - 创建一个 Engine 对象并传入模型:
Engine eng = new Engine();
ModelT car = new Modelt(eng);
回答by SleepyFox89
It's a little hard to see what you're trying to achieve.
看到您想要实现的目标有点困难。
You could look into instanceof to get the class of Engine.
您可以查看 instanceof 以获取 Engine 的类。
You might want to look into inheritance and the various ways you can relate classes.
您可能想要研究继承以及关联类的各种方法。
回答by ΦXoc? ? Пepeúpa ツ
Your getterwill make more sense if you associate the Engine
class with the ModelT
and by associating I mean using a Composition relationship
如果您将类与
和关联我的意思是使用组合关系,您的getter将更有意义Engine
ModelT
public class ModelT {
private Engine myEngine;
//some setter, DI or Constructors here
public Engine getEngine() {
return myEngine;
}
}
how could I return the value of public double getEngineSize() in the class ModelT??
我怎么能在类 ModelT 中返回 public double getEngineSize() 的值?
you have somewhere:
你有地方:
ModelT foo = new ModelT();
Engine e = foo.getEngine();
double s = e.getEngineSize();
回答by Christian
This test will never pass, because you return null
in the getEngine()
method.
If you call getEngineSize()
on null
, you will receive a NullPointerException
这个测试永远不会通过,因为你null
在getEngine()
方法中返回。如果你调用getEngineSize()
上null
,您将收到NullPointerException
You need to do something like this:
你需要做这样的事情:
public class ModelT {
public Engine getEngine() {
return new Engine();
}
}
Then you will be able to use your Engine
.
然后你就可以使用你的Engine
.
Because of your questoin about how to learn the basics in Java, I would recommend you the Java-Basicsfrom Oracle directly.
由于您对如何学习 Java 基础知识有疑问,我会直接向您推荐Oracle的Java-Basics。
回答by Toni Montero
Maybe i'm not understanding your question but your test never will return ok because in your ModelT method getEngine you are returning a null..
也许我不理解你的问题,但你的测试永远不会返回 ok 因为在你的 ModelT 方法 getEngine 你返回一个空值..
Try with below code:
尝试使用以下代码:
public class ModelT {
public Engine getEngine() {
return new Engine();
}
}