阶层算法_内部阶层

阶层算法

In Java, an inner class is also known as nested class. Inner classes are part of nested classes. When a non-static class is defined in nested class then it is known as an inner class. It is defined inside the class or an interface. Inner classes are mostly used to logically group all the classes and the interface in one place, which makes the code more readable and manageable. Inner classes can access members of the outer class including all the private data members and methods.

在Java中,内部类也称为嵌套类。 内部类是嵌套类的一部分。 当在嵌套类中定义非静态类时,则称为内部类。 它在类或接口内部定义。 内部类通常用于将所有类和接口逻辑组合在一个位置,这使代码更易读和易管理。 内部类可以访问外部类的成员,包括所有私有数据成员和方法。

Syntax:

句法:

class OuterClass
{  
 //code  
 		class InnerClass
{  
  			//code  
 		}  
}

嵌套类的类型 (Types of Nested classes)

  1. Non-static nested class

    非静态嵌套类

    1. Member inner class
    2. Anonymous inner class
    3. Local inner class
  2. Local inner class

    本地内部阶级

Following are the examples in which inner classes can be defined

以下是可以定义内部类的示例

1.带有静态方法的静态内部类 (1. static inner class with a static method)

Example:

例:

class outer
{
	static class inner
	{
		public static void add(intx,int y)
		{
			int z = x + y;
			System.out.println("add = " + z);
		}
	}
}
class innerclass_demo1
{
	public static void main(String args[])
	{
		outer.inner.add(15,10);
	}
}
inner-class

2.带有非静态方法的静态内部类 (2. static inner class with non-static method)

Example:

例:

class outer
{
	static class inner
	{
		public void add(intx,int y)
		{
			int z = x + y;
			System.out.println("add = " + z);
		}
	}
}

class innerclass_demo2
{
	public static void main(String args[])
	{
		outer.innerob = new outer.inner();
		ob.add(12,13);
	}
}
static-inner-class

3.使用非静态方法的非静态内部类 (3. non-static inner class with a non-static method)

Example:

例:

class outer
{
	class inner
	{
		public void add(intx,int y)
		{
			int z = x + y;
			System.out.println("add = " + z);
		}
	}
}

class innerclass_demo3
{
	public static void main(String args[])
	{
		outer ot = new outer();
		outer.inner in = ot.new inner();
		in.add(34,56);
	}
}
non-static-inner-class

4.使用静态方法的非静态内部类 (4. non-static inner class with a static method)

Note: it is an illegal combination. Only static variables are allowed and should be final.Note: it is an illegal combination. Only static variables are allowed and should be final.

Example:

例:

class outer
{
	class inner
	{
		/* Illegal combination */
		/*public static void add(intx,int y)
		{
			int z = x + y;
			System.out.println("add = " + z);
		}*/
		public static final int a = 45;
	}
}

class innerclass_demo4
{
	public static void main(String args[])
	{
		outer ot = new outer();
		outer.inner in = ot.new inner();
		System.out.println("Value of a = "+in.a);
	}
}
non-static-inner-class

5.本地方法中的嵌套内部类 (5. Nested inner class in local method)

Example:

例:

class outer
{
	public void display(intx,int y)
	{
		class inner
		{
			public void add(intx,int y)
			{
				int z = x + y;
				System.out.println("add = " + z);
			}
		}
		
		inner in = new inner();
		in.add(x,y);
	}	
}

class innerclass_demo5
{
	public static void main(String args[])
	{
		outer ob = new outer();
		ob.display(23,56);
	}
}
nested-inner-class

翻译自: https://www.studytonight.com/java/inner-class.php

阶层算法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值