这行得通吗 - 公开课
我想知道以下代码是否应该工作:
if (M != 0){
Tools.Printer(stdnr);
}
背景信息:
我创建了一个具有一些功能的公共类工具。其中一项功能是 void Printer 功能。是否有规则或某些内容,如果您想使用 Tools.something,您需要将 Tools.something 分配给整数/打印它等。或者 Tools.Printer 应该独立工作。
我问这个是因为我找不到我的代码有什么问题。如果有人能帮助我正确使用上面使用的术语,我会很高兴 - 我想他们是非常错误的......
非常感谢,我们将不胜感激
I would like to know if the following code should work:
if (M != 0){
Tools.Printer(stdnr);
}
Background Info:
I created a public class Tools with a few functions. One of the functions is the void Printer function. Is there a rule or something that if you want to use Tools.something you need to do assign the Tools.something to an integer / print it, etc. Or is Tools.Printer supposed to work on its own.
I ask this because I can't find anything wrong with my code. I would be glad if someone can help me right with the terms I used above - I suppose they are terribly wrong...
Thank a lot, Help would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的方法是 void 类型,则它不会返回任何内容,因此您不需要将其分配给变量(事实上,如果这样做,您将收到编译时错误)。
如果它是一个函数,就像它返回一些值,例如:
您仍然不需要将它分配给变量。返回的值将被丢弃。例如,StringBuilder。 append 返回您刚刚附加到的 StringBuilder,但您可以像这样安全地调用它:
并忽略返回值。
但是,如果您的
Tools.Printer
方法不是static
方法,则您确实需要创建一个 Tools 实例,或者将其分配给如下变量:或者在不进行初始化的情况下进行初始化放置在变量中:
但是如果
Printer
是静态方法,那么您的代码应该没问题,因为您是通过它包含的类来调用该方法的。If your method is a void type, it returns nothing, therefore you do not need to assign it to a variable (in fact, you'll get a compile time error if you do).
If it was instead a function, like it returned some value, for instance:
You still don't necessary need to assign it to a variable. The returned value will just be discarded. For example, StringBuilder.append returns back the StringBuilder that you just appended to, but you can safely call it like this:
And ignore the return value.
However, if your
Tools.Printer
method is not astatic
method, you do need to create an instance of Tools, either assigned to a variable like this:or initialized without being placed in a variable:
But if
Printer
is a static method, your code should be fine, since you are calling the method by the class it's contained in.