当前位置:网站首页>内部类与静态内部类区别及举例
内部类与静态内部类区别及举例
2022-07-27 05:01:00 【圆师傅】
1.内部类与静态内部类的区别
静态内部类相对与外部类是独立存在的,在静态内部类中无法直接访问外部类中的变量、方法。如果要访问的话,必须要new一个外部类的对象,使用new出来的对象来访问。 但是可以直接访问静态变量,调用静态的方法。
普通内部类作为外部类一个成员而存在,在普通内部类中可以直接访问外部类属性,调用外部类的方法。
如果外部类要访问内部类的属性或者调用内部类的方法,必须要创建一个内部类的对象,使用该对象访问属性或者调用方法。
如果其他的类要访问普通内部类的属性或者调用普通内部类的方法,必须要在外部类中创建一个内部类的对象作为一个属性,外部类可以通过属性调用普通内部类的方法或者访问普通内部类的属性。
如果其他的类要访问静态内部类的属性或者调用静态内部类的方法,直接创建一个静态内部类对象即可。
2.举例说明
2.1 内部类
public class Father {
private String name;
public Father(String name) {
this.name = name;
}
public void say(String dogName) {
Dog dog = new Dog(dogName);
System.out.println("i am " + name + ", i have a dog " + dog.dogName);
}
public void feed() {
System.out.println("feed in Father");
}
public class Dog {
private String dogName;
public Dog(String dogName) {
this.dogName = dogName;
}
public void bark() {
feed();
System.out.println("i am " + dogName + ", i am belong to " + name);
}
public String getDogName() {
return dogName;
}
}
}
对于第2条,普通内部类作为成员,可以直接访问外部类的变量,调用其方法。可以看到Dog内中的bark()方法可以直接使用变量name,可以调用feed方法。
对于第3条,我们在Father类中,想要使用Dog类中的属性dogName,必须先实例化一个dog对象,然后通过该对象调用方法,获取变量。
对于第4条,那先创建一个外部类
public class YYTest {
public static void main(String[] args) {
final Father zhangsan = new Father("zhangsan");
Father.Dog gg = zhangsan.new Dog("GG");
gg.getDogName();
gg.bark();
}
}
可以看到,通过对象zhangsan.new Dog()方法实例化一个内部类对象gg,通过该内部类对象可以访问变量,调用方法。
2.2 静态内部类
public class Father {
private String name;
private static String sex = "female";
public Father(String name) {
this.name = name;
}
public void say(String dogName) {
Dog dog = new Dog(dogName);
System.out.println("i am " + name + ", i have a dog " + dog.dogName);
}
public void feed() {
System.out.println("feed in Father");
}
public static class Dog {
private String dogName;
public Dog(String dogName) {
this.dogName = dogName;
}
public void bark() {
Father lisi = new Father("lisi");
lisi.feed();
System.out.println("i am " + dogName + ", i am belong to " + lisi.name + ", his sex is " + sex);
}
public String getDogName() {
return dogName;
}
}
}
对于第1点,可以看到bark()方法中想要使用name属性,需要先实例化Father对象,通过对象调用。而访问静态变量则不需要通过实例化对象,可以直接访问。
对于第1点
public class YYTest {
public static void main(String[] args) {
Father.Dog hh = new Father.Dog("HH");
hh.bark();
}
}
直接实例化静态内部类即可。注意实例化方法即可。
静态类只能是内部的。
边栏推荐
- ERP system brand
- JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程
- During its low-level period, this slave edge causes the instruction number to make a corresponding model
- A math problem cost the chip giant $500million
- Card drawing program simulation
- Create datasource using Druid connection pool
- 实用小工具: Kotlin 代码片段
- JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈
- B1021 个位数统计
- Bean's life cycle & dependency injection * dependency auto assembly
猜你喜欢

JVM Part 1: memory and garbage collection part 12 -- stringtable

集合框架的使用

JVM上篇:内存与垃圾回收篇五--运行时数据区-虚拟机栈

Event

ERROR! MySQL is not running, but PID file exists

Gradio quickly builds ml/dl Web Services

How to test the payment process?

Introduction to Web Framework

2021 OWASP top 6-10 collection

How idea creates a groovy project (explain in detail with pictures and texts)
随机推荐
JVM Part 1: memory and garbage collection part 10 - runtime data area - direct memory
JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈
B1023 组个最小数
Static and final keyword learning demo exercise
Bean's life cycle & dependency injection * dependency auto assembly
How to test the payment process?
JVM Part 1: memory and garbage collection part 9 - runtime data area - object instantiation, memory layout and access location
What is the future development direction of software testing engineers?
智慧展厅设计的优势及适用行业分析
The project connects with Alipay payment, and the intranet penetration realizes the monitoring of asynchronous callback notification of successful payment of Alipay
Summary of knowledge points (I)
Typescript details
使用ngrok做内网穿透
Dialog data transfer
使用Druid连接池创建DataSource(数据源)
简化JDBC的MyBits框架
JVM上篇:内存与垃圾回收篇三--运行时数据区-概述及线程
Standard dialog qmessagebox
idea远程调试debug
Slashes / and backslashes involved in writing code\