当前位置:网站首页>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
边栏推荐
- Official website of Unicode query
- Export the entire Oracle Database
- 4. 无线体内纳米网:电磁传播模型和传感器部署要点
- 十条职场规则
- Adult adult adult
- Sword finger offer03 Repeated numbers in the array [simple]
- 初入职场,如何快速脱颖而出?
- 公纵号发送提示信息(用户微服务--消息微服务)
- Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
- [download attached] password acquisition tool lazagne installation and use
猜你喜欢

电压环对 PFC 系统性能影响分析

并网-低电压穿越与孤岛并存分析

剑指Offer04. 二维数组中的查找【中等】

剑指Offer06. 从尾到头打印链表

T430 toss and install OS majave 10.14

How to convert a decimal number to binary in swift

Powerful avatar making artifact wechat applet

Sword finger offer10- I. Fibonacci sequence

The future of cloud computing cloud native
![[download attached] password acquisition tool lazagne installation and use](/img/21/eccf87ad9946d4177b600d96e17322.png)
[download attached] password acquisition tool lazagne installation and use
随机推荐
01_ Using the concurrent tool class library, is thread safety safe
Solve the problem of VI opening files with ^m at the end
JVM memory model
Oh my Zsh + TMUX installation
Keep learning swift
Sword finger offer05 Replace spaces
Use bloc to build a page instance of shutter
How to convert a decimal number to binary in swift
Implement verification code verification
如何在微信小程序中获取用户位置?
Dix règles de travail
elastic_ L02_ install
studio All flavors must now belong to a named flavor dimension. Learn more
Swift5.7 扩展 some 到泛型参数
2020-09_ Shell Programming Notes
OpenStack节点地址改变
Airflow installation jump pit
Xctf mobile--app3 problem solving
Kung Fu pays off, and learning is done
The solution to change the USB flash disk into a space of only 2m