Java面向对象编程中的方法的Void类型

huangapple go评论65阅读模式
英文:

Void type for method in Java OOP

问题

我几天前就遇到了一个问题,我弄不清楚"Void"方法类型是什么意思。一个方法没有返回值的目的是什么(方法没有返回值是什么意思,我的理解是该方法什么也不做)?我需要它用于即将到来的编程考试,而且书上总是以同样的方式写着"Void方法没有返回值"。

我以为具有这种返回值类型的方法只是用来重新定义它们自己。也许我有问题,是因为我把"没有返回值"这个术语与Mathlab联系在一起,因为在Mathlab中,如果我在命令的末尾加上一个冒号,该命令会被执行但不会返回任何东西。

例如,在Matlab中:

x = [ 90 0 0 ];
s=sin(x);
英文:

I've had a problem for a few days now, I can't figure out what the Void method type is. What is the purpose of a method that does not return a value (what does it mean that the method does not have a return value of the method, my experience of that explanation is that the method does nothing)? I need it for the programming exam that is coming soon, and in the books it is always written the same way "Void method does not return a value".

I thought that methods with this type of return value only serve to redefine themselves. Maybe I have a problem because I associate the term "Does not return a value" with Mathlab, because in Mathlab if I put a colon at the end of the command, the command will be executed but will not return anything.

Matlab e.g.:

x = [ 90 0 0 ];
s=sin(x);

答案1

得分: 1

简单来说,当我们说一个方法不返回任何内容时,这意味着该方法不产生一个可以赋值给变量或在表达式中使用的值。以下是一个例子:

public class SumOfNumbers {
    public static void main(String[] args) {
        //您可以选择首先初始化这些数字
        int num1 = 10;
        int num2 = 15;
        //调用方法来添加这两个数字
        add(num1, num2);

        //或者直接调用方法并传递数字
        System.out.println();//在两个解决方案之间打印新行
        add(10, 15);
    }

    //该方法接受两个数字,计算它们的和,并打印出和
    public static void add(int a, int b) {
        int sum = a + b;

        //打印和
        System.out.println("The sum of " + a + " and " + b + " is " + sum);
    }
}

请注意,在主方法中,不需要输出系统,因为该方法已经包含了输出语句。此代码中使用的输出系统用于区分两个输出,即我们首先初始化数字时和当我们直接将数字传递给方法时的情况。

英文:

In simple terms, when we say that a method does not return anything, this implies that the method does not yield a value that can be assigned to a variable or be used in expressions.
The following is an example of such:

public class SumOfNumbers {
    public static void main(String[] args) {
        //you can choose to first initialize the numbers
        int num1 = 10;
        int num2 = 15;
        //call the method to add the two numbers
        add(num1,num2);

        //alternatively call the method and pass the numbers direct
        System.out.println();//to print new line between the two solutions
        add(10, 15);
    }

    //the method takes two numbers, finds the sum, prints the sum
    public static void add(int a, int b){
        int sum = a + b;

        //print the sum
        System.out.println("The sum of " + a +  " and "  + b + " is " + sum);
    }
}

note that in the main method, there is need for the output system as the method already has the output statement. the output system used in this code is to differentiate the two outputs i.e where we first initialise the numbers and when we pass the number direct to the method.

答案2

得分: 0

void方法并不意味着它什么都不做,它意味着它的工作完成后不需要返回结果。

这个方法执行一个任务,但它不会返回任何东西

例如,setter方法就是很好的例子。
在这个例子中,setFirstname是一个将值赋给名字字段的方法。

public class Person {
    private String firstname;

    public void setFirstname(String s) {
        firstname = s;
    }

    public String getFirstname() {
        return firstname;
    }
}

让我们来使用它

public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        person.setFirstname("Ali");
        String personFirstname = person.getFirstname();
        System.out.println(personFirstname);
    }
}

输出将会是:

> Ali

正如你所看到的,当我们使用getFirstname方法并打印其返回值时,很明显名字字段已经改变了

谁执行了这个任务?
setFirstname方法。

尽管这个方法没有特定的输出,但它做了一些重要的事情

英文:

void method doesn't mean that it doesn't do anything, it means that its work is done without having to return a result.

This method performs a task but it does not give anything back.

for example, setter methods are good examples.
in this example, setFirstname is a method that assigns a value to the first name filed.

public class Person {
    private String firstname;

    public void setFirstname(String s) {
        firstname = s;
    }

    public String getFirstname() {
        return firstname;
    }
}

Let's use it

public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        person.setFirstname("Ali");
        String personFirstname = person.getFirstname();
        System.out.println(personFirstname);
    }
}

the output will be:

> Ali

As you can see, when we use the getFirstname method and print its return value, it is clear that the firstname field has changed.

Who performs this task?
setFirstname method.

Although this method did not have a specific output, it did something important.

huangapple
  • 本文由 发表于 2023年7月23日 16:51:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定