当前位置:网站首页>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
边栏推荐
- 2.6 preliminary cognition of synergetic couroutines
- 1-2 project technology selection and structure
- alright alright alright
- 02_ Lock the code, and don't let the "lock" become a worry
- 记录自己vulnhub闯关记录
- Lambda expression
- Solve the problem of VI opening files with ^m at the end
- Export the entire Oracle Database
- Kotlin notes - popular knowledge points asterisk (*)
- Ten workplace rules
猜你喜欢
TOGAF认证自学宝典V2.0
idea将web项目打包成war包并部署到服务器上运行
Record your vulnhub breakthrough record
Ali & ant self developed IDE
How to convert a decimal number to binary in swift
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
并网-低电压穿越与孤岛并存分析
Display time with message interval of more than 1 minute in wechat applet discussion area
剑指Offer05. 替换空格
Analysis of a music player Login Protocol
随机推荐
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
双链笔记·思源笔记综合评测:优点、缺点、评价
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
十条职场规则
Sword finger offer05 Replace spaces
02_ Lock the code, and don't let the "lock" become a worry
elastic_ L01_ summary
[download attached] password acquisition tool lazagne installation and use
最新版抽奖盲盒运营版
Adult adult adult
Adult adult adult
公纵号发送提示信息(用户微服务--消息微服务)
Is it safe to open an account for online stock speculation? Who can answer
studio All flavors must now belong to a named flavor dimension. Learn more
Adult adult adult
Write a simple nodejs script
Ali & ant self developed IDE
2.9 overview of databinding knowledge points
Glide 4.6.1 API initial