📜  说明没有参数但有返回类型的方法的Java程序

📅  最后修改于: 2022-05-13 01:55:16.805000             🧑  作者: Mango

说明没有参数但有返回类型的方法的Java程序

任务是说明一个不包含任何参数但应该返回一些值的方法。方法用于将一个完整的程序分解成一个 可以一一执行的模块数量。考虑任何随机形状来说明:圆形

示例:首先应该创建一个名为“Circle”的类然后借助该类,它可以计算圆的面积和周长。

  1. 面积计算
  2. 周长计算

在 Circle 类中,我们将声明一个名为 radius 的变量来从用户那里获取半径值,以便通过使用这个半径值我们将能够计算圆的面积和周长。现在,为了计算面积和周长,我们需要创建两个单独的方法,称为area()circle() ,它们不接受任何值作为参数,但肯定会返回一些值,该方法将返回的值是圆的面积和周长的最终值。这些方法将在变量值的帮助下计算后返回值。计算面积和周长最重要的是,使用这些方法首先需要调用那些称为调用函数的方法,为此我们必须创建一个 Circle 类的对象,因为在 main 方法中,我们想要访问它们,另一个类的方法,因此我们需要创建包含我们的方法area()circle()的那个类的对象,并且通过使用类Circle的对象,我们将很容易调用这些方法和那些方法将返回一些值,我们将使用我们的预定义函数来显示它。

句法:

class name
    {
        datatype MethodName()
        {
            statements....
            ..
            ..
            return value;
        }
    }

执行:

示例 1:面积计算

Java
// Java Program to Illustrate a Method
// without Parameters but with Return Type
 
// Importing generic java classes
import java.util.*;
// Importing Scanner class if
// input is from user (optional)*/
import java.util.Scanner;
 
// Auxiliary Class (Sample class)
// class which contains the method area()
class Circle {
 
    // initializing the variable radius
    double radius;
 
    // functions calculating area
    double area()
    {
        double r = 7;
 
        // Area of circle will be calculate and
        // will store into the variable ar
        double ar = 3.14 * r * r;
 
        // Method returning the area
        return ar;
    }
}
 
// Main Class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Object of circle is created
        Circle c = new Circle();
       
        // Big type variable to hold area value
        double area;
 
        // Area method is called and
        // will store the value in variable area
        area = c.area();
 
        // Printing area of circle
        System.out.println("Area of circle is : " + area);
    }
}


Java
// Java Program to Illustrate a Method
// without Parameters but with Return Type
 
// Importing java generic classes
import java.util.*;
 
class Circle {
   
    double radius;
   
    // method which does not contain
    // parameter but will return
    // the circumference of circle
    double circumference()
    {
        
        double r = 7;
        double circum = 2 * 3.14 * r;
       
        // Method returning the circumference of circle
        return circum;
    }
}
public class GFG {
   
    public static void main(String args[])
    {
        // the object of circle is created to call the
        // method circumference()
        Circle c = new Circle();
        double circum;
       
        // the value returned from method will store into
        // the variable circum
        circum = c.circumference();
        System.out.println("Circumference of circle is : "
                           + circum);
    }
}



输出
Area of circle is : 153.86

示例 2:周长计算

Java

// Java Program to Illustrate a Method
// without Parameters but with Return Type
 
// Importing java generic classes
import java.util.*;
 
class Circle {
   
    double radius;
   
    // method which does not contain
    // parameter but will return
    // the circumference of circle
    double circumference()
    {
        
        double r = 7;
        double circum = 2 * 3.14 * r;
       
        // Method returning the circumference of circle
        return circum;
    }
}
public class GFG {
   
    public static void main(String args[])
    {
        // the object of circle is created to call the
        // method circumference()
        Circle c = new Circle();
        double circum;
       
        // the value returned from method will store into
        // the variable circum
        circum = c.circumference();
        System.out.println("Circumference of circle is : "
                           + circum);
    }
}


输出
Circumference of circle is : 43.96