当前位置:网站首页>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 修饰符.
边栏推荐
猜你喜欢

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

EA&UML日拱一卒-活动图::Object actions(续)

力扣 206.反转链表--递归解决

Vscode builds ESP32-C3 development environment

EA&UML日拱一卒-活动图::CallOperationAction(续)

Project Manager: Not bad!The SSO single sign-on code is written, and the sequence diagram is also drawn?

The Location object of BOM series

教育部等五部门联合推荐优质课外资源,腾讯产品青少年模式首发

深开鸿:万物智联的大江上,升起一轮开源鸿蒙月

Some thoughts on paying for knowledge
随机推荐
【JS面试题】面试官问我:遍历一个数组用 for 和 forEach 哪个更快?
Understand the yolov7 network structure
新来技术总监:谁在用 isXxx 形式定义布尔类型,明天不用来了!
2022开放原子全球开源峰会数据库分论坛圆满召开
【论文阅读】Anomaly Detection in Video via Self-Supervised and Multi-Task Learning
Bika LIMS 开源LIMS集—— SENAITE的使用(用户、角色、部门)
小程序开发模板设计怎么做?
全开放式耳机怎么样?不塞耳朵的蓝牙耳机推荐
inner join 与 left join 之间的区别
这么多年了,还搞不懂正则语法?
性能优化竟白屏,难道真是我的锅?
中国互联网科技企业群狼围攻,谷歌终于遭受重挫导致利润大跌,它为推动鸿蒙的发展而后悔...
2022年了!还在用定时器实现动画?赶紧试试requestAnimationFrame吧!
PAT 甲级 A1021 Deepest Root
leetcode134. 加油站
[10:00 Open Class]: Application Exploration of Kuaishou GPU/FPGA/ASIC Heterogeneous Platform
全面质量管理理论
无线传感器网络定位综述
FPGA刷题——跨时钟域传输(FIFO+打拍+握手)
【JS高级】js之闭包对象_04