Java程序展示方法的嵌套

2023年 8月 28日 19.9k 0

Java程序展示方法的嵌套

Nesting of methods is a hybrid function calling method in Java, which can call another method in the same class. There are two types of nested classes are available in a Java environment.

将方法嵌套视为Java中的混合函数调用方法,它可以调用同一类中的另一个方法。在Java环境中有两种类型的嵌套类可用

  • 非静态嵌套类(也称为内部类)

  • 静态嵌套类

一个非静态嵌套类(或者内部类)是在一个特定类内定义的类。它还包含一些具有一些访问权限的外部类。在这种方法中,我们可以使用“.”运算符通过使用外部类来创建内部类的实例。

另一方面,静态嵌套类是在类内部创建的特定类。但是这种类型的嵌套类无法访问任何非静态的方法和成员。它们只能被外部类访问。

在今天的文章中,我们将学习Java虚拟机环境中存在的各种嵌套方法。并且我们将使用下面提到的语法和算法编写一些可能的代码。

在Java中显示嵌套方法的算法

在这个可能的算法中,我们将学习嵌套方法在Java环境中的实际工作原理。通过按照这些步骤,我们将根据问题陈述构建一些Java代码。

  • 第一步 - 开始。

  • 第二步 - 输入数据样本。

  • 第三步 - 初始化输入权重。

  • 第四步 - 初始化隐藏节点的偏置。

  • 第五步 - 选择一个函数进行定义。

  • 步骤6 - 如果方法满足逻辑,则继续。

  • 步骤7 - 否则,返回第三步和第四步。

  • 步骤8 - 如果该方法符合代码要求,则选择隐藏节点的最佳输入权重和偏置。

  • 第9步 - 通过应用经验方法确定初始隐藏节点的数量。

  • 第10步 - 确定最佳隐藏节点数量。

  • 第11步 - 训练样本。

  • 第12步 - 如果准确度达到标记,则终止该过程。

  • 第13步 - 获取嵌套模型。

  • 第14步 - 进行所有预测。

  • 步骤15 - 否则,再次根据经验方法确定隐藏节点的初始数量。

显示方法嵌套的语法

Process Syntax:

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

登录后复制

Class Syntax:

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代码来解释和展示各种嵌套方法。

展示不同嵌套方法的途径

  • 使用Java的嵌套方法找到球的面积

  • 通过在Java中调用main()和swap()方法找到两个数字的总和

  • Approach 3 − Java程序展示嵌套方法以找到周长值

  • 方法4 - Java内部类和嵌套类

  • 通过使用三个方法类显示嵌套方法的Java程序

使用Java的嵌套方法来计算球体的面积

让我们使用Java的嵌套方法来计算球体的面积。这里我们使用了两个类,包含Area()和Volume(),用于计算球体的面积和体积。

Example 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()方法来展示两个数字的相加过程。

Example 2

的中文翻译为:

示例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代码中,我们尝试展示方法的嵌套来找出周长的值。

Example 3

的中文翻译为:

示例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

登录后复制

Example 4 A

的翻译为:

示例 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

登录后复制

Example 4B

//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。

Example 5

的翻译为:

示例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代码。希望本文能帮助您理解这里提到的各种嵌套方法的操作方式。

以上就是Java程序展示方法的嵌套的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论