当前位置:网站首页>概述静态内部类与非静态内部类
概述静态内部类与非静态内部类
2022-07-27 12:32:00 【涟涟涟涟】
区分 概念:
按照是否有static修饰分为静态内部类和非静态内部类,
有static修饰的为静态内部类,
没有static修饰的为非静态内部类
静态内部类
静态有名内部类
跨类使用则须“外部类类名.内部类名”
本类使用则无须使用外部类类名
例:
Body类:
package com.jd;
/**
* 外部类
*/
public class Body {
/**
* 静态有名内部类
*/
public static class Heart{
public void beat(){
System.out.println("正在跳动...");
}
}
}
Test类:
package com.jd;
import com.jd.Body.Heart;
public class Test {
public static void main(String[] args) {
Heart heart = new Body.Heart();
heart.beat();
}
}
中的
Heart heart = new Body.Heart();
heart.beat();
静态内部类仅能直接访问外部类的静态成员变量和方法,
可以通过创建外部类的对象间接使用非静态的成员变量和方法。
public class OutClass {
private double weight=72;
public static void print(String name) {
System.out.println(name);
}
static class InClass{
{
double weight = new OutClass().weight;
//由于weight是非静态的,所以在静态内部类中使用时必须先创建外部类对象
print("Tom");
//由于print方法为静态方法,所以可以直接使用。
}
}
}
在非外部类中定义的内部类和局部变量一样,其使用范围从定义的位置开始到其所在直接语句块结束。
public class OutClass {
public static void main(String[] args) {
if(args!=null) {
class InClass{
}
}
InClass inClass = new InClass();
//无法创建对象,因为内部类作用范围无法作用到这里
}
}
只有有名静态内部类中才允许有静态成员(静态属性、静态代码块和静态方法)。
静态匿名内部类
只需在匿名内部类前面加上static即可。
class Father {
public void eat() {
System.out.println("筷子吃饭....");
}
}
/**
* 外部类
*/
public class OutClass {
/**
* 匿名内部类
*/
static Father son = new Father(){
@Override
public void eat() {
System.out.println("筷子吃饭....");
}
};
}
注:
如果为static内部类只能直接定义在外部类中。不可在方法,代码块中。
public class OutClass {
public static void main(String[] args) {
/**
* 静态有名内部类
*/
static class InClass {
public void printInfo() {
System.out.println("我是有名内部类");
}
}
}
}



非静态内部类
直接在类中定义的非静态内部类,在静态方法中创建对象,必须“显式”创建外部类对象
边栏推荐
- The use of omitempty in go
- Check the number of file descriptors opened by each process under the system
- Bishi journey
- USB network card drive data stream
- 4. Analysis of the execution process of make modules for general purposes
- No matching distribution found for flask_ A solution to compat
- Conversion between multiple bases
- J9 number theory: how long is the mainstreaming of decentralized identity?
- You haven't connected to the proxy server. There may be a problem or the address is incorrect (how to check the proxy server IP)
- Detailed explanation of flask framework
猜你喜欢

An overview of kernel compilation system

评价自动化测试优劣的隐性指标

Dominoes staged: the beginning and end of the three arrow capital crash

Watermelon book chapter 3 (first & second)

Self built personalized automatic quotation system to cope with changeable quotation mode

Why does MySQL index use b+ tree instead of jump table?

【数据库数据恢复】SQL Server数据库所在磁盘分区空间不足报错的数据恢复案例

II. Analysis of the execution process of make menuconfig
Ali II: what if the AOF file in redis is too large?

CMD Chinese garbled code solution
随机推荐
评价自动化测试优劣的隐性指标
Detailed explanation of deeplab series (simple and practical annual summary)
开关量输入输出模块DAM-5055
Guangdong's finance has taken many measures to help stabilize the "ballast stone" of food security
Log4j2.xml configuration details
[网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
POJ1988_ Cube Stacking
SQL question brushing: find out the current salary of all employees
An overview of kernel compilation system
STS download tutorial (the solution cannot be downloaded on the include official website)
二分查找判定树(二分查找树平均查找长度)
II. Analysis of the execution process of make menuconfig
JVM memory layout detailed, illustrated, well written!
Julia beginner tutorial 2022
POJ1988_Cube Stacking
POJ1611_The Suspects
(10) STM32 - systick tick timer
Wechat applet session holding
Alibaba cloud RDS exception during pool initialization
Ali II: what if the AOF file in redis is too large?