Java程序示例:一个没有参数和返回类型的方法


prtyaa
prtyaa 2024-01-08 22:28:10 51405 赞同 0 反对 0
分类: 资源 标签: 运维
首先,让我们熟悉一下语法、示例,然后最后再进行实现。 Java中的方法非常重要,因为它允许重复使用相同的代码,减少代码中需要编写的语句数量。

有三个主要部分的方法,以使其可执行。

  • 方法的声明。

  • 方法的定义。

  • 调用该方法。

方法调用是最后一步,而其他两个步骤可以互换。唯一需要记住的是,在调用方法之前必须先声明它。

Syntax

To create a method without any parameter and return type, the following syntax is considered.

Class class_name{
function _name() {
Statement 1;
Statement 2;
.
.
Statement n;
//an optional return
return;
}
Main function() {
// invoking the above function
function_name();
}
}

在一个类中创建一个空参数列表的方法。方法内部写入语句,可能会在最后加上一个空的返回语句。然后在主方法中调用这个方法。

Example

下面的程序是为了展示如何创建一个既没有参数也没有返回类型的方法。

A class named Wish is created within which, a method named wish() with return type void is created denoting it does not return any value, also it does not consist of any parameter. A statement is written within the wish() method and is displayed by invoking this method in the main method.

// Java Program to demonstrate a method without Parameters and Return Type
public class Wish {
// Declaration and Definition of the method
public static void wish(){
System.out.println(“Good Morning! Have a nice day”);
}
public static void main(String args[]){
// Calling the method without any parameters
wish ();
}
}

输出

Good Morning! Have a nice day

Example

下面的程序是为了展示如何创建一个既没有参数也没有返回类型的方法。

A class named Wish is created within which, a method named wish() with return type void is created denoting it does not return any value, also it does not consist of any parameter. The statements written inside the wish() method are displayed by invoking the method in the main method.

// Java Program to demonstrate a method without Parameters and Return Type
public class Wish {
// Declaration and Definition of the method
public static void wish(){
System.out.println(“Congratulations! Have a great professional life”);
//It is optional to use a return statement here.
return;
}
public static void main(String args[]){
// Calling the method without any parameters
wish();
}
}

输出

Congratulations! Have a great professional life

结论

本文阐述了如何在Java中定义一个没有参数和返回值的方法。我们从语法开始,进一步看到了一个示例和两个Java程序,以便清楚地了解这个主题。

如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!

评价 0 条
prtyaaL2
粉丝 1 资源 1949 + 关注 私信
最近热门资源
银河麒麟桌面操作系统备份用户数据  125
统信桌面专业版【全盘安装UOS系统】介绍  120
银河麒麟桌面操作系统安装佳能打印机驱动方法  112
银河麒麟桌面操作系统 V10-SP1用户密码修改  105
最近下载排行榜
银河麒麟桌面操作系统备份用户数据 0
统信桌面专业版【全盘安装UOS系统】介绍 0
银河麒麟桌面操作系统安装佳能打印机驱动方法 0
银河麒麟桌面操作系统 V10-SP1用户密码修改 0
作者收入月榜
1

prtyaa 收益393.62元

2

zlj141319 收益218元

3

1843880570 收益214.2元

4

IT-feng 收益209.03元

5

风晓 收益208.24元

6

777 收益172.71元

7

Fhawking 收益106.6元

8

信创来了 收益105.84元

9

克里斯蒂亚诺诺 收益91.08元

10

技术-小陈 收益79.5元

请使用微信扫码

加入交流群

请使用微信扫一扫!