当前位置:网站首页>Quickly learn member inner classes and local inner classes
Quickly learn member inner classes and local inner classes
2022-07-03 12:45:00 【Persia_ Pepper】
Member inner class
First of all, we need to learn the basic points of inner classes of members
1. Class composition : attribute 、 Method 、 Constructors 、 Code block ( Common block 、 A static block 、 Tectonic block 、 Synchronized block )、 Inner class
2. One class TestOuter Inner class of SubTest It's called inner class , Inner class :SubTest External class :TestOuter
3. Inner class : Member inner class ( Static , Not static ) and Local inner classes ( Location : In the way , block , In the constructor )
4. Member inner class :
contain : attribute 、 Method 、 Constructors, etc ;
Modifier :private、default、protect、public、final、abstract
public class TestOuter {
// Non static member inner class
public class D{
int age = 20;
String name;
public void method(){
//5. The inner class can access the contents of the outer class
/*System.out.println(age); a();*/
int age = 30;
//8. How to call... When the internal class and external class properties have the same name
System.out.println(age);//30
System.out.println(this.age);//20
System.out.println(TestOuter.this.age);//10
}
}
// Static members come from within
static class C{
public void method(){
//6. Static inner classes can only access objects in outer classes static Decorated content
/*System.out.println(age); a();*/
}
}
// attribute
int age = 10;
// Method
public void a(){
System.out.println(" This is a a Method ");
{
System.out.println(" This is an ordinary piece ");
class B{
}
}
class A{
}
//7. What the external class wants to access the internal class , You need to create an object of the inner class and then call
D d = new D();
System.out.println(d.name);
d.method();
}
static{
System.out.println(" This is a static block ");
}
{
System.out.println(" This is the building block ");// Building blocks run before ordinary blocks in inner classes
}
// Constructors :
public TestOuter(){
class C{
}
}
public TestOuter(int age) {
this.age = age;
}
}
class Demo{
public static void main(String[] args) {
// Create an external class object
TestOuter to = new TestOuter();
to.a();
//9. Create inner class objects
// Static member inner classes create objects
TestOuter.C c = new TestOuter.C();
// Non static member inner class creates object
// error :TestOuter.D d = new TestOuter.D();
TestOuter t = new TestOuter();// Secondary creation of external classes , The building block will be loaded again
TestOuter.D d = t.new D();
}
}
Running results :
This is a static block
This is the building block
This is a a Method
This is an ordinary piece
null
30
20
10
This is the building block
Member inner class
On the first code
public class TestOuter {
//1. The variable accessed in the local inner class must be final Embellished
public void method(){
final int num = 10;
class A{
public void a(){
//num = 20; Report errors
System.out.println(num);
}
}
}
//2. If a class B Use it only once in the project , Then there is no need to create a separate B class , Just use inner classes
public Comparable method2(){
// Use the interface as the return value of the method , Return a concrete implementation class object , A polymorphic Application
class B implements Comparable{
// Implement an interface at will , Don't worry about implementing interfaces , Rewrite what should be rewritten first
@Override
public int compareTo(Object o) {
return 100;
}
}
return new B();
}
}
public Comparable method3(){
// Anonymous inner class
return new Comparable() {
@Override
public int compareTo(Object o) {
return 200;
}
};
}
public void test(){
Comparable com = new Comparable() {
@Override
public int compareTo(Object o) {
return 300;
}
};
System.out.println(com.compareTo("abc"));
}
You can see the following two anonymous inner classes , This class has no name , We directly use this interface to create this object ; When receiving, directly use the interface as the object , Equivalent to the object created by the interface ... This piece of writing can be seen in many source codes , Beginners can tap slightly , Just know what it means
边栏推荐
- context. Getexternalfilesdir() is compared with the returned path
- 【ManageEngine】IP地址扫描的作用
- 十條職場規則
- 2020-11_ Technical experience set
- 云计算未来 — 云原生
- 基于Linu开发的项目视频
- [download attached] password acquisition tool lazagne installation and use
- Sword finger offer09 Implementing queues with two stacks
- The latest version of lottery blind box operation version
- Implement verification code verification
猜你喜欢
initial、inherit、unset、revert和all的区别
Eureka自我保护
The latest version of lottery blind box operation version
T430 toss and install OS majave 10.14
Sword finger offer05 Replace spaces
Low code platform international multilingual (I18N) technical solution
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
记录自己vulnhub闯关记录
最新版盲盒商城thinkphp+uniapp
如何在微信小程序中获取用户位置?
随机推荐
Analysis of a music player Login Protocol
studio All flavors must now belong to a named flavor dimension. Learn more
2020-11_ Technical experience set
剑指Offer05. 替换空格
Glide question you cannot start a load for a destroyed activity
Adult adult adult
Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
idea将web项目打包成war包并部署到服务器上运行
[download attached] password acquisition tool lazagne installation and use
Adult adult adult
Openstack node address change
Swift5.7 扩展 some 到泛型参数
Public and private account sending prompt information (user microservice -- message microservice)
Write a simple nodejs script
双链笔记·思源笔记综合评测:优点、缺点、评价
Record your vulnhub breakthrough record
Sqoop1.4.4原生增量导入特性探秘
Oh my Zsh + TMUX installation
Day 1 of kotlin learning: simple built-in types of kotlin
如何在微信小程序中获取用户位置?