当前位置:网站首页>About inner classes
About inner classes
2022-07-29 14:24:00 【xiaokaikaa】
一、内部类是什么
在java语言中,可以把一个类定义到另外一个类的内部,This class inside a class is called an inner class,The outer class is called the outer class.
在这情况下,This inner class can be seen as a member of the outer class.
二、如何定义内部类
1.成员内部类
The most common inner class is the member inner class,也称作普通内部类;
代码如下:
public class Outer{
private String str1="外部类";
public class Inner{
//成员内部类
String str2="内部类";
public void test(){
System.out.println(str1);
System.out.pprintln(str2);
}
}
public static void main(String[] args){
Outer o=new Outer();
Inner i=o.new Inner();//使用外部类对象,创建内部类对象
i.test();
}
}
2.静态内部类
静态内部类就是用static修饰的内部类,这种内部类的特点是:
1、静态内部类不能直接访问外部类的非静态成员,但可以通过new 外部类().成员的方式访问;
代码如下:
public class Outer{
String name="zhangsan";
public static class Inner{
public void show(){
System.out.println("外部类的name为:"+new Outer().name);
}
}
}
2、如果外部类的静态成员与内部类的静态成员相同,可以通过"类名.静态成员"来访问外部类的静态成员;如果不同,可以直接调用外部类的静态成员名.
代码如下:
public class Outer{
static String name="zhangsan";
public static class Inner{
static String name="lisi";
public void show(){
System.out.println("外部类name:"+Outer.name);
System.out.pprintln("内部类name:"+name);
}
}
public static void main(String[] args){
Inner i=new Inner();
i.show();
}
}
public class Outer{
static String name="zhangsan";
public static class Inner{
static String name1="lisi";
public void show(){
System.out.println("外部类name:"+name);
System.out.pprintln("内部类name:"+name1);
}
}
public static void main(String[] args){
Inner i=new Inner();
i.show();
}
}
3.方法内部类
1、The method of the inner class is defined in the method of the outer class,Method inner classes are only available within that method;
代码如下:
public class Outer{
public void show(){
final int a=25;
int b=10;
class Inner{
int c=5;
public void print(){
System.out.println("外部类方法中的常量a:"+a);
System.out.pprintln("内部类中变量c:"+c);
}
}
Inner i=new Inner();
i.print();
}
public static void main(String[] args){
Outer o=new Outer();
o.show();
}
}
2.由于方法内部类不能在外部类的方法以外的地方使用,Therefore methods inner classes cannot use access control characters and static 修饰符.
边栏推荐
猜你喜欢

第4章_1——SQL语句实现MySQL增删改查

human pose estimation-DEKR2021CVPR

The Location object of BOM series

EA&UML日拱一卒-活动图::StartClassifierBehavior和StartObjectBehavior

84.(cesium之家)cesium模型在地形上运动

中国互联网科技企业群狼围攻,谷歌终于遭受重挫导致利润大跌,它为推动鸿蒙的发展而后悔...

【模板引擎】微服务学习笔记六:freemarker模板引擎的常用命令介绍

How to merge the code when there is a code conflict in the collaborative development of multiple people?

阿里巴巴 CTO 程立:开源是基础软件的源头!

R Error in :missing values are not allowed in subscripted assignments of data frames
随机推荐
如何编辑CAD图库里的图纸
【论文阅读】异常检测的视频通过Self-Supervised和多任务学习
关于内部类
程序员入门的第一个程序,打印输出 “ HelloWorld “
【微信小程序】全局配置
浅谈如何在渗透测试中快速搞定webshell
Understand the yolov7 network structure
使用云服务器从0开始搭建云端Jupyter Lab|Notebook
这么多年了,还搞不懂正则语法?
中国电信首发全新加密通话产品!有效防止网络监听
rk3399驱动添加电池adc开机检测功能
app小程序开发的营销优势有什么?
基于变胞机构的移动机器人构型设计研究综述
1184. 欧拉回路
[局域网劫持]如何搞懵蹭网的同学-详细过程
深开鸿:万物智联的大江上,升起一轮开源鸿蒙月
题目 1125: C语言训练-委派任务*
通过二维顺序表实现杨辉三角
【Postman】Download and installation (novice graphic tutorial)
一文搞懂JS的原型链