当前位置:网站首页>Internal class learning notes
Internal class learning notes
2022-06-25 15:28:00 【BlackPenguin】
Class is nested in a class , It's called an inner class
Member inner class
The member of a class is a class , The inner class of a member can access all the properties and methods of the outer class , Even if it is private、static Of
Members in inner class are private when , Can only be accessed inside an external class ; The members are public when , You can visit anywhere
Member inner class cannot have static Method ; There can be static Domain , Must be final Of
Depends on external classes , So you have to create an external class to access the internal class
public class Test {
public static void main(String[] args) {
Car car = new Car();
car.run();
// Create inner class objects . Access internal class members , You must first have an object of an external class
Car.Bike bike1 = car.new Bike();
bike1.run(); // days = 15
Car.Bike bike = new Car().new Bike();
bike.run(); // days = 5;
}
}
class Car {
// External class
class Bike {
// Inner class
public int value;
public void run() {
days += 5;
System.out.println("bike run " + days);
}
}
private int days;
public void run() {
days = 10;
System.out.println("car run " + days);
}
}
Local inner classes
Nested in method 、 Internal classes in the scope , Access is only in its method or scope . A local inner class is actually a local variable of a method , So it can't be used public、private、protected、static modification
Anonymous inner class
A class without a name , Can only be used once . Anonymous inner classes must be inherited parent classes or implement interfaces
There are several methods in the interface , You have to rewrite a few methods ; How many abstract methods does an abstract class have , You have to rewrite these abstract methods
Appearing in ordinary use Most frequent Of
Anonymous inner classes cannot have access modifiers 、static, Static members cannot be defined 、 Method 、 class
abstract class Per{
public abstract void eat();
public abstract void run();
}
public class tt {
public static void main(String[] args) {
Per per = new Per() {
@Override
public void eat() {
}
@Override
public void run() {
}
};
}
}
such as Android When listening for events in , Anonymous inner classes are used .
Static inner class
static Decorated inner class —— Static inner class
Static inner classes do not depend on the creation of outer classes , You cannot use non static members of an external class
Static inner classes can create instances directly , Instead of using external classes
// Create a static inner class instance directly
new OuterClass.InnerClass().run();
public class Test {
public static void main(String[] args) {
OuterClass.InnerClass innerClass = new OuterClass.InnerClass();
innerClass.run();
}
}
class OuterClass {
static class InnerClass {
public void run() {
System.out.println("static InnerClass run");
}
}
}
边栏推荐
- Statistical analysis - data level description of descriptive statistics
- (2) Relational database
- The robot is playing an old DOS based game
- One code per day - day one
- How to deal with mining process
- Single user mode
- QT source code online view
- Weka download and installation
- Pytorch | how to save and load pytorch models?
- Solve valueerror: invalid literal for int() with base 10
猜你喜欢

Design and implementation of timer

Completabilefuture of asynchronous tools for concurrent programming

Stack and queue

Fishing detection software

Record the time to read the file (the system cannot find the specified path)

Kali SSH Remote Login

QT loading third-party library basic operation
![[C language] implementation of magic square array (the most complete)](/img/b2/2595263b77e0abac667972bbfe0c8a.jpg)
[C language] implementation of magic square array (the most complete)
![[paper notes] rethinking and improving relative position encoding for vision transformer](/img/6b/8b1c192e0ce715789465fa9ccaabfc.jpg)
[paper notes] rethinking and improving relative position encoding for vision transformer

JS select all exercise
随机推荐
Source code analysis of synergetics and ntyco
QT pop up open file dialog box QFileDialog
Using R language in jupyter notebook
Kali SSH Remote Login
About?: Notes for
(translation) json-rpc 2.0 specification (Chinese version)
Learning C language today is the first time to learn C language. In college, C linguistics is not good, but I want to make progress, so I found a beep video on the Internet to learn C language
Qmake uses toplevel or topbuilddir
Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detection
Common operations in VIM
Pytorch distributed test pit summary
If multiple signals point to the same slot function, you want to know which signal is triggered.
Automatic correlation between QT signal and slot
QT set process startup and self startup
Completabilefuture of asynchronous tools for concurrent programming
A deformation problem of Hanoi Tower
p1408
System Verilog — interface
basic_ String mind map
Stderr and stdout related standard outputs and other C system APIs