当前位置:网站首页>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
边栏推荐
- Differences between initial, inherit, unset, revert and all
- 2.7 overview of livedata knowledge points
- 剑指Offer07. 重建二叉树
- The solution to change the USB flash disk into a space of only 2m
- Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
- Prompt unread messages and quantity before opening chat group
- 2021 autumn Information Security Experiment 1 (password and hiding technology)
- Solve the problem of VI opening files with ^m at the end
- Computer version wechat applet full screen display method, mobile phone horizontal screen method.
- 电压环对 PFC 系统性能影响分析
猜你喜欢

Detailed explanation of the most complete constraintlayout in history

【ManageEngine】IP地址扫描的作用

剑指Offer09. 用两个栈实现队列

Day 1 of kotlin learning: simple built-in types of kotlin

2.8 overview of ViewModel knowledge

社交社区论坛APP超高颜值UI界面

Togaf certification self-study classic v2.0

Xctf mobile--app1 problem solving

Display time with message interval of more than 1 minute in wechat applet discussion area

Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
随机推荐
Is it OK to open an account for online stock speculation? Is the fund safe?
CNN MNIST handwriting recognition
剑指Offer03. 数组中重复的数字【简单】
Nodejs+express+mysql realizes login function (including verification code)
Is it safe to open an account for online stock speculation? Who can answer
Self made pop-up input box, input text, and click to complete the event.
Project video based on Linu development
LeetCode 0556. Next bigger element III - end of step 4
Idea packages the web project into a war package and deploys it to the server to run
Adult adult adult
Unicode查询的官方网站
Sword finger offer09 Implementing queues with two stacks
Sword finger offer05 Replace spaces
context. Getexternalfilesdir() is compared with the returned path
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
4. 无线体内纳米网:电磁传播模型和传感器部署要点
Day 1 of kotlin learning: simple built-in types of kotlin
Solve the problem of VI opening files with ^m at the end
Nodejs+Express+MySQL实现登陆功能(含验证码)
Sword finger offer07 Rebuild binary tree