📜  如何调用返回Java中其他方法的方法

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

如何调用返回Java中其他方法的方法

方法是执行某些特定任务并将结果返回给调用者的语句的集合。方法还可以执行某些特定任务而不返回任何内容。在本文中,我们将了解如何在Java中调用返回某个其他方法的方法。

在Java中,有两种方法。他们是:

  1. 静态方法:静态方法是Java中无需创建类对象即可调用的方法。它们由类名本身或对该类的对象的引用来引用。
  2. 实例方法:实例方法是需要在调用它之前创建其类的对象的方法。要调用实例方法,我们必须创建它定义的类的对象。

调用返回一些其他静态方法的静态方法:由于静态方法与它们所在的类相关联(即)即使不创建类的实例也可以调用它们,我们可以直接定义一个调用的方法该方法通过调用该方法的定义。让我们通过以下示例了解如何执行此操作:

  • 示例 1:在此示例中,让我们可视化一辆汽车。汽车在行驶之前必须启动。因此,汽车具有启动汽车的启动函数。为了让汽车启动,它的引擎必须首先启动,其余的其他实体启动,从而最终使汽车准备好运行。因此,这个carStart()函数需要有一个签名 engineStart() 来启动引擎。那是:
    // Java program to visualize the Car
      
    // A class Car
    public class CarFunc {
      
        // A method engineStart() which
        // simply starts the engine
        public static void engineStart()
        {
            System.out.println("Engine Started!");
        }
      
        // A method carStart() which starts
        // the engine and other entities
        // required
        public static void carStart()
        {
            // Calling the function
            // engineStart() inside the
            // definition of function
            // carStart()
            engineStart();
      
            // Definitions of starting
            // various other entities
            // can be defined here
      
            // Finally, the car is
            // started
            System.out.println("Car Started!");
        }
      
        public static void main(String[] args)
        {
      
            carStart();
      
            // When the car is started,
            // we are ready to drive
            System.out.println("Let's Drive!");
        }
    }
    
    输出:
    Engine Started!
    Car Started!
    Let's Drive!
    
  • 示例 2:在此示例中,让我们考虑一个类似的示例,其中实体是加速器并提高速度。为了增加车辆的速度,踩下加速器。一旦踩下油门,速度就会增加,直到没有达到速度限制。这是按如下方式实现的:
    // A carSpeed class which manages
    // the speed of the class
    public class CarSpeedFunc {
      
        // Speed is a global variable. If
        // the speed is changed at any
        // part of the car, then the overall
        // speed of the car automatically
        // changes
        static int speed = 0;
      
        // Function to increase the speed
        public static void increaseSpeed()
        {
            speed = speed + 50;
            System.out.println(
                "Speed increased by 50 km/hr");
      
            // main() called inside this function
            main(new String[0]);
        }
      
        // Method which performs the increase
        // speed function when the accelerator
        // is pressed
        public static void pressAccelerator()
        {
            System.out.println(
                "Accelerator pressed "
                + "to increase speed");
            increaseSpeed();
        }
      
        // Driver code
        public static void main(String[] args)
        {
            System.out.println(
                "Driving with speed = "
                + speed
                + "km/hr");
      
            if (speed < 200) {
                pressAccelerator();
            }
            else {
                System.out.println(
                    "Speed Limit Reached! "
                    + "Drive Slow. Stay safe.");
            }
        }
    }
    
    输出:
    Driving with speed = 0km/hr
    Accelerator pressed to increase speed
    Speed increased by 50 km/hr
    Driving with speed = 50km/hr
    Accelerator pressed to increase speed
    Speed increased by 50 km/hr
    Driving with speed = 100km/hr
    Accelerator pressed to increase speed
    Speed increased by 50 km/hr
    Driving with speed = 150km/hr
    Accelerator pressed to increase speed
    Speed increased by 50 km/hr
    Driving with speed = 200km/hr
    Speed Limit Reached! Drive Slow. Stay safe.
    

调用返回一些其他静态方法的静态方法:实例方法属于类的对象,而不属于类(即)它们可以在创建类的对象后调用。实例方法也可以从另一个方法调用。但是,我们需要知道我们正在调用的方法的地址。当前对象的地址存储在thissuper之类的关键字中。让我们通过一个例子来理解这一点。在本例中, super关键字用于从子类调用父类的对象。那是:

class GFG {
  
    // A nested parent class
    // which has the method
    // detail
    public static class BriefDescription {
        void detail()
        {
            System.out.println(
                "Ferrari 812 is "
                + "an awesome car.");
        }
    }
  
    // A child class extending the
    // parent class
    public static class DetailedDescription
        extends BriefDescription {
  
        // Overriding the parent
        // method
        @Override
        void detail()
        {
            // Using super keyword to call
            // 'detail()' method from the
            // parent class
            // 'BriefDescription'
            super.detail();
            System.out.println(
                "It has a seating "
                + "capacity of 2, "
                + "fuel economy of 7 kmpl "
                + "and comes with a horsepower "
                + "of 588 kW.");
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        BriefDescription briefDesc
            = new BriefDescription();
  
        BriefDescription detailDesc
            = new DetailedDescription();
  
        System.out.println(
            "Brief detail of Ferrari:");
  
        // Method from the parent class
        // is invoked
        briefDesc.detail();
  
        System.out.println(
            "Complete detail of Ferrari:");
  
        // Method from both parent class
        // and subclass is invoked.
        detailDesc.detail();
    }
}
输出:
Brief detail of Ferrari:
Ferrari 812 is an awesome car.
Complete detail of Ferrari:
Ferrari 812 is an awesome car.
It has a seating capacity of 2, fuel economy of 7 kmpl and comes with a horsepower of 588 kW.