什么是“无效”意味着作为方法的返回类型? [英] What does "void" mean as the return type of a method?

查看:110
本文介绍了什么是“无效”意味着作为方法的返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对void感到困惑,
因为它与方法有关。

I'm confused about "void", as it pertains to methods.

我不知道两个方法之间的区别是什么,当一个有无效而另一个没有。

I don't know what the distinction between two methods is when one has "void" and another doesn't.

例如,如果我这样做:

Public meth (int amount)
{

    amount = initial * interest;
    return amount;

}

(不确定它是否正确,甚至是否有效,取名称金额,并将其命名为与我的形式参数相同的东西,但这里有意义的是你正在进行计算并返回结果)

( not sure if it was right, or even valid, to take the name "amount" and name it the same thing as my formal parameter, but what makes sense here is that you're performing a calculation and returning the result)

然后,如果我做了类似的事情:

Then, if I did something like:

Public void Testing (int array[])
{

    //code that would modify the internals of an array

}

第二个没有返回,因为它更像是一个通用方法,可以应用于任何整数数组,而第一个方法是关于对特定变量的工作吗?

Would the second one have no "return" because it's more of a general method, that can be applied to any integer array, while the first one is about doing work on specific variables?

当我愿意或不会使用void和return时,还会欣赏一两个例子。

Would also appreciate one or two more examples of when I would or wouldn't be using "void" and "return".

另外一个似乎让我感到困惑的是调用方法。

One other thing that seems to confuse me is calling methods.

我知道有时我会做一些事情,例如,使用上面的测试方法,

I know sometimes I'll do something like, for example, using the Testing method above,

Testing(ArrayName);

其他时间,它会像:

NameOfWhateverImApplyingMethodTo.MethodName();

然后有时候事情将通过以下方式正确完成:

And then there are times when things will be done properly by:

Thing1.MethodName(Thing2);

我会在哪种情况下切换方法调用的语法?

Which circumstances would I switch the syntax for method calls like this?

推荐答案

返回类型就是你从中得到的。当你打电话的时候,你有什么希望回来的?例如,如果方法获得两个数字的平均值,那么您期望返回一个数字,因此返回类型将是数字类型,如int(整数)。
你可以看到它应该使用那个逻辑或者查看单词return的方法 - 返回后返回的是返回的内容,它的类型应该在方法中声明(例如,如果它表示返回 4;它返回一个int,应该是例如public int getFour()

Return type is what you get out of it. When you call it, what are you hoping to get back? For instance, if the method gets the average of two numbers, then you're expecting a number back, so the return type will be a number type, like "int" (integer). You can see what it should be using that logic or by looking in the method for the word return - what comes after return is what is returned, and its type should be declared in the method (e.g. if it says "return 4;" it's returning an int, and should be e.g. public int getFour()

你还问过例如testing()vs testing(word)
I记住有相同的难度。两者之间的区别也与方法声明行有关。我将说明。

You also asked about e.g. testing() vs testing(word) I remember having the same difficulty. The distinction between the two also relates to the method declaration line. I'll illustrate.

    public String testing(){
        return "a word";
    }

调用此方法通过执行System.out.println(testing());应该打印一个单词的方法。通过执行System.out.println(testing(a word))来调用此方法;会给你一个问题 - 这是因为当你调用测试时,它会查看相应的方法:一个在正确的类中,具有正确的返回类型和正确的参数/参数。如果你是cal ling测试(一个单词),这意味着你使用String作为参数(因为一个单词是一个字符串),所以它试图使用测试(String aString)方法 - 它不存在。
所以当方法没有输入时你使用空括号,并且当方法需要东西时你把东西放在括号中。这应该比听起来更容易混淆,因为它通常是合乎逻辑的 - 如果你想调用一个返回平均值的方法,你需要问自己 的平均值?您可能需要为其提供您想要的平均值。

Calling this method by doing "System.out.println(testing());" should print "a word". Calling this method by doing "System.out.println(testing("a word"));" will give you an issue - this is because when you call testing, it looks at the appropriate method: one in the right class, with the right return type and with the right arguments/parameters. If you're calling testing("a word"), that means you're using a String as an argument (because "a word" is a string), and so it tries to use the testing(String aString) method - which doesn't exist. So you use empty brackets when the method takes no input, and you put stuff in brackets when the method expects stuff. This should be less confusing than it sounds, because it's usually logical - if you want to call a method that returns an average, you need to ask yourself "Average of what?" You'd probably need to supply it with the values you want the average of.

继续:(a)测试()与(b)AClass.testing()与(c)aclass.testing() -

Moving on: (a) testing() versus(b) AClass.testing() versus(c) aclass.testing() -

在(a)中,没有指定类。因此,如果你从那个类中调用它,Java可以猜出哪个类:这个类,它会起作用。从任何其他课程,它不会知道你在说什么,甚至可能侮辱你。
在(b)中,您通常会指定一个类 - 因此它会知道要在哪个类中找到它 - 如果它是静态方法,它将起作用。 * [见下图]
在(c)中,您要指定要在*上运行testing()的AClass的实例

In (a), there's no class specified. Therefore, if you call it from that class, Java can guess which class: this one, and it'll work. From any other class, it won't know what you're talking about, and might even insult you. In (b), you're specifying a class in general - therefore it'll know what class to find it in - and it'll work if it's a "static method". *[see bottom] In (c), you're specifying an instance of AClass you want to run "testing()" on*.

例如,假设您创建了一个名为Business的类。通过为每个名称,数字,地址指定,可以创建一百个Business对象。
,例如

For instance, imagine you've created a class called Business. You make a hundred Business objects by specifying for each a name, number, address. e.g.

   Business b = new Business(name, number, address);

然后在Business类中,您有一个方法getName()。这个方法不带参数 - 你可以看到括号是空的 - 所以如果从另一个类调用Business.getName(),它怎么知道你想要的名字?你刚刚做了一百家企业!
它根本不可能。因此,对于这样的方法,您将调用b.getName()(b是我们上面创建的业务),它将获得业务的此实例的名称 - 即b 。
我很乐意提供帮助,所以如果你对我刚写的任何特定部分感到困惑,请告诉我,我会尽力详细说明!

Then in the Business class you have a method "getName()". This method takes no argument - you could see that the brackets are empty - so if, from another class, you call "Business.getName()", how could it know which name you want? You've just made a hundred businesses! It simply can't. Therefore, for such a method, you'd call "b.getName()" (b being the Business we created above) and it would get the name for this instance of a Business - namely, b. I'm happy to help, so if you're confused about any particular parts of what I just wrote please let me know and I'll try to elaborate!

编辑:有点静态方法:
静态方法不属于该类的实例。例如,getName()会得到 this Business的名称 - 即Business类的这个实例。但是,假设您在Business类中创建了一个方法,该方法将字符串中每个单词的第一个字母转换为大写字母 - 就像您希望在打印出来时使公司名称看起来更专业一样。

edit: A bit on static methods: Static methods don't belong to an instance of the class. getName(), for example, would get the name of this Business - ie, this instance of the Business class. But let's say that in the Business class you made a method that took the first letter of each word in a String and transformed it to uppercase - like if you wanted to make the business names look more professional when you printed them out.

public static String stringToUpperCase(String aString){
    aString = aString.substring(0, 1).toUpperCase() + aString.substring(1);
    return aString;
}

要使用它,你可以改变getName()方法:

And to use that, you change the getName() method from:

public String getName(){
    return name;
}

public String getName(){
    return stringToUpperCase(name);
}

这里使用新方法使名称首字母大写 - 但这是它参与商务舱的程度。您注意到它不会询问有关特定企业的名称,地址或编号的信息。它只需要一个字符串你给它,做一些事情,然后把它还给它。无论你是没有企业还是一百个都没关系。
要调用此方法,您可以使用:

The new method is used here to make the name have an uppercase first letter - but that is the extent of its involvement with the Business class. You notice it doesn't ask for information about the name, address, or number for a particular business. It just takes a string you give it, does something to it, and gives it back. It doesn't matter whether you have no Businesses or a hundred. To call this method, you'd use:

System.out.println(Business.stringToUpperCase("hello"));

这将打印Hello。
如果它不是静态方法,您必须首先开展新业务:

This would print Hello. If it were not a static method, you'd have to make a new Business first:

Business b = new Business("aName", "aNumber", "anAddress"); 

System.out.println(b.stringToUpperCase("hello"));

如果方法 需要访问更多业务实例信息(就像商家的名字或地址一样,它不可能是一个实例变量。

And if the method did need access to more Business-instance information (like a business's name number or address) it wouldn't be able to be an instance variable.

这篇关于什么是“无效”意味着作为方法的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆