Java程式演示方法巢狀


方法巢狀是Java中一種混合函式呼叫方法,它可以在同一個類中呼叫另一個方法。Java環境中提供了兩種巢狀類。

  • 非靜態巢狀類(也稱為內部類)

  • 靜態巢狀類

非靜態巢狀類(或內部類)是在特定類中定義的類。它還包含一些具有訪問許可權的外部類。在這種方法中,我們可以使用“.”運算子透過外部類來建立內部類的例項。

另一方面,靜態巢狀類是在類內部建立的特定類。但是,這種型別的巢狀類無法訪問任何非靜態方法和成員。它們可以透過外部類訪問。

在本文中,我們將學習Java虛擬機器環境中存在的各種巢狀方法。並使用它們編寫一些可能的程式碼,其中包含下面提到的語法和演算法。

Java中方法巢狀的演算法

在這個可能的演算法中,我們將學習方法巢狀在Java環境中是如何工作的。透過遵循這些步驟,我們將根據問題陳述構建一些Java程式碼。

  • 步驟 1 - 開始。

  • 步驟 2 - 輸入資料樣本。

  • 步驟 3 - 初始化輸入權重。

  • 步驟 4 - 初始化隱藏節點的偏差。

  • 步驟 5 - 選擇要定義的函式。

  • 步驟 6 - 如果方法滿足邏輯,則繼續執行。

  • 步驟 7 - 否則,返回步驟三和四。

  • 步驟 8 - 如果方法滿足程式碼,則選擇最優的輸入權重和隱藏節點的偏差。

  • 步驟 9 - 透過應用經驗方法確定隱藏節點的初始數量。

  • 步驟 10 - 確定隱藏節點的最優數量。

  • 步驟 11 - 訓練樣本。

  • 步驟 12 - 如果達到精度標記,則終止程序。

  • 步驟 13 - 獲取巢狀模型。

  • 步驟 14 - 進行所有預測。

  • 步驟 15 - 否則,再次轉到透過應用經驗方法確定隱藏節點的初始數量。

方法巢狀的語法

過程語法

class Main {
   method1(){ 
   } 
   method2(){
      method1();
   }
   method3(){
      method2();
   }
}

類語法

class OuterClass {
   // ...
   class NestedClass {
      // ...
   }
}

使用Java語法的巢狀方法

class Nesting2001{
   int m, n;
   Nesting2001 (int x, int y){
      m=x;
      n=y;
   }
   int largest ( ){
      if (m >=n)
      return(m);
      else
      return(n);
   }
   void display( ){
      int large=largest ( );
      System.out. println("largest value is here" large);
   }
}
class Nestmain2022{
   public static void main ( String args[ ]){
      Nesting2001 nest=new nesting2001 (10, 20);
      nest. Display( );
   }
}

在這個可能的語法中,我們嘗試演示如何構建Java程式碼來解釋和展示各種巢狀方法。

展示各種巢狀方法的方法

  • 方法 1 - 使用Java中的巢狀方法求球體面積

  • 方法 2 - 透過在Java中呼叫main()和swap()方法來求兩個數字的和

  • 方法 3 - Java程式演示方法巢狀以求周長值

  • 方法 4 - Java內部類和巢狀類

  • 方法 5 - Java程式使用三層方法類演示巢狀方法

使用Java中的巢狀方法求球體面積

讓我們使用Java中的巢狀方法求球體面積。這裡我們使用了兩個類,包含Area()和Volume(),它們分別計算球體的面積和體積。

示例 1

public class Nesting2022 {
   public void Area(double r){
      System.out.println("##### Inside Area method will be applied #####");
      double a = 7 * Math.PI * r * r;
      System.out.println("Surface area of the particular Sphere is : " + a);
	}
	public void Volume(double r){
       System.out.println("%%%%% Inside Volume method will be applied here%%%%%");
       double v = (4 / 3) * Math.PI * r * r * r;
       System.out.println("Volume of a Sphere is here : " + v);
	}
	public static void main(String args[]){
       Nesting2022 RDDARB = new Nesting2022();
       RDDARB.Area(12);
       RDDARB.Volume(12);
   }
}

輸出

##### Inside Area method will be applied #####
Surface area of the particular Sphere is : 3166.7253948185116
%%%%% Inside Volume method will be applied                  here%%%%%
Volume of a Sphere is here : 5428.672105403162

透過呼叫main()和swap()方法求兩個數字的和

在這個Java程式碼中,我們嘗試演示透過呼叫main()和swap()方法求兩個數字的和的過程。

示例 2

public class Nesting1997 {
   public void swap(int x, int y){
      System.out.println("**@@$$%%This is a swap method. Lets Check The Process%%$$@@**");
      System.out.println("Before swapping the condition:x=" + x + " " + "y=" + y);
      int z = x;
      x = y;
      y = z;
      	
      System.out.println("After Swapping the condition:a=" + x + " "+ "b=" + y);
   }
   public void Tutorialspoint16 (int a, int b){
      System.out.println("#####This is Tutorialspoint16 encoded method#####");
      System.out.println("Before performing the operation we will get:a=" + a + " " + "b=" + b);
      a = a + 10;
      b = b + 12;
      System.out.println("After operation, the system will return:a=" + a + " "  + "b=" + b);
      swap(a, b);
   }
   public static void main(String args[]){
      Nesting1997  Tutorialspoint07 = new Nesting1997();
      
      int a = 20, b = 30;
      Tutorialspoint07.Tutorialspoint16(a, b);
   }
}

輸出

#####This is Tutorialspoint16 encoded method#####
Before performing the operation we will get:a=20 b=30
After operation, the system will return:a=30 b=42
**@@$$%%This is a swap method. Lets Check The Process%%$$@@**
Before swapping the condition:x=30 y=42
After Swapping the condition:a=42 b=30

方法巢狀以求周長值

在這個Java程式碼中,我們嘗試演示方法巢狀以求周長值。

示例 3

import java.util.Scanner;
public class NestingbyCuboid2023{
   int perimeter(int l, int b){
      int pr = 12 * (l + b);
      return pr;
   }
   int area(int l, int b){
      int pr = perimeter(l, b);
      System.out.println("Perimeter:"+pr);
      int ar = 6 * l * b;
      return ar;
   }
    int volume(int l, int b, int h){
      int ar = area(l, b);
      System.out.println("Area:"+ar);
      int vol ;
      vol = l * b * h;
      return vol;
   }
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.print("Enter length of that particular cuboid:");
      int l = s.nextInt();
      System.out.print("Enter breadth of that particular cuboid:");
      int b = s.nextInt();
      System.out.print("Enter height of that particular cuboid:");
      int h = s.nextInt();
      Nesting_Methods obj = new Nesting_Methods();
      int vol = obj.volume(l, b, h);
      System.out.println("Volume:"+vol);
   }
}

輸出

Enter length of that particular cuboid:7
Enter breadth of that particular cuboid:16
Enter height of that particular cuboid:10
Perimeter:276
Area:672
Volume:1120

Java內部類和巢狀類

在本例中,我們可以演示如何使用Java環境宣告一些內部類和巢狀類。

示例 4

//Java code to define an inner class we can use in JVM
class CPUz {
   double price;
   class Processor{
      double cores;
      String manufacturer;

      double getCache(){
         return 16.10;
      }
   }
   protected class RAM{
      double memory;
      String manufacturer;

      double getClockSpeed(){
            return 07.10;
      }
   }
}
public class Main {
   public static void main(String[] args) {
      CPUz cpu = new CPUz();
      CPUz.Processor processor = cpu.new Processor();
      CPUz.RAM ram = cpu.new RAM();
      System.out.println("Processor Cache We Will Get = " + processor.getCache());
      System.out.println("Ram Clock speed We Can Examine = " + ram.getClockSpeed());
   }
}

輸出

Processor Cache We Will Get = 16.1
Ram Clock speed We Can Examine = 7.1

示例 4 A

//Java Code To Access Members
class Car2022 {
   String carName;
   String carType;
   public Car2022(String name, String type) {
      this.carName = name;
      this.carType = type;
   }
   private String getCarName() {
      return this.carName;
   }
   class Engine {
      String engineType;
      void setEngine() {
         if(Car2022.this.carType.equals("7XYXARB")){
               if(Car2022.this.getCarName().equals("Crysler")) {
                    this.engineType = "Smaller Engine Type";
               } else {
                  this.engineType = "Bigger Engine Type";
               }

            }else{
               this.engineType = "Bigger Engine Type";
            }
      }
      String getEngineType(){
         return this.engineType;
      }
   }
}

public class Main {
   public static void main(String[] args) {
        Car car1 = new Car("Mazda", "16XYZARB");
        Car.Engine engine = car1.new Engine();
        engine.setEngine();
        System.out.println("Engine Type for 16XYZRDD= " + engine.getEngineType());

        Car car2 = new Car("Crysler", "7XYZARB");
        Car.Engine c2engine = car2.new Engine();
        c2engine.setEngine();
        System.out.println("Engine Type for 7XYZARB = " + c2engine.getEngineType());
   }
}

輸出

Engine Type for 16XYZRDD= Bigger Engine Type
Engine Type for 7XYZARB = Bigger Engine Type

示例 4 B

//Java Program To Demonstrate A Static Inner Class Using JVM
class MBCSS {
   static class USB2022{
      int usb2 = 2;
      int usb3 = 1;
      int getTotalPorts(){
         return usb2 + usb3;
      }
   }

}
public class Main {
   public static void main(String[] args) {
       MBCSS.USB2022 usb = new MBCSS.USB2022();
       System.out.println("Total Ports Present Here In The System = " + usb.getTotalPorts());
   }
}

輸出

Total Ports Present Here In The System = 3

Java程式使用三層方法類演示巢狀方法

在本例中,我們編寫了一個特定方法的Java程式碼來演示使用三層方法類的巢狀過程。這裡,特定方法可以呼叫任何隨機方法。這裡也可以呼叫另一個方法。這意味著方法 1 可以呼叫方法 2,方法 2 的返回值可以呼叫方法 3。

示例 5

public class Nesting0{
   public void a1(int a, int b){
      a = a + 10;
      b = b + 20;
      System.out.println(
      	"******#### Inside the a1 method ####******");
      System.out.println("a = " + a + " "
                  + "b = " + b);
      a2(a, b);
	}
	public void a2(int x, int y){
      x = x + 100;
      y = y + 200;
      System.out.println(
      	"******@@@ Inside the a2 method @@@******");
      System.out.println("x = " + x + " "
                  + "y = " + y);
	}
	public void a3(int w, int z){
      w = w + 50;
      z = z - 50;
      System.out.println(
      	"******%% Inside the a3 method %%******");
      System.out.println("w = " + w + " "
                  + "z = " + z);
      a1(w, z);
	}
	public static void main(String[] args){
      Nesting0 ARBRDD = new Nesting0();
      
      int a = 100, b = 200;
      
      ARBRDD.a3(a, b);
	}
}

輸出

******%% Inside the a3 method %%******
w = 150 z = 150
******#### Inside the a1 method ####******
a = 160 b = 170
******@@@ Inside the a2 method @@@******
x = 260 y = 370

結論

在這裡,我們討論了巢狀方法以及一些可能的Java程式碼,這些程式碼遵循語法和演算法。希望本文能幫助您瞭解此處提到的各種巢狀方法的操作方式。

更新於: 2023年4月13日

5K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.