Method without return value, method without argument

Asked 1 years ago, Updated 1 years ago, 59 views

I don't quite understand methods without return values or arguments.

Please give me an easy-to-understand explanation that even junior high school students can understand, including specific examples.

java

2022-09-30 21:45

2 Answers

Method of returning the circumference (no arguments)

double getpi()
{
    return 3.1415;
}

Method to Print (No Return Value)

void puri (string p)
{
System.out.println(p);
}

Something like that.


2022-09-30 21:45

In object-oriented programming languages, including Java, an object has a state (information), but such methods may appear when setting/changing this state (information) or inquiring about it.

When the clock object has alarm time information,

  • Set the alarm time on the clock

A typical implementation in the case is to use a method with no return value that takes the alarm time for the argument.

  • See the alarm time set to the clock

A typical implementation of the would be a method with no arguments.

class Clock {
    LocalTime alarm;

    // method with no return value
    // set the alarm time
    void setAlarm (LocalTime alarm) {this.alarm=alarm;}

    // method without arguments
    // inquire about the alarm time
    LocalTime getAlarm() {return alarm;}
}


2022-09-30 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.