当前位置:网站首页>Interview knowledge points
Interview knowledge points
2022-07-26 10:54:00 【qq_ forty-one million four hundred and eighty-two thousand and 】
1、 The difference between abstract class and interface
1.1、 The difference between members :
abstract class :
Construction method : There's a way to structure , Used for subclass instantiation .
Member variables : It could be a variable , It can also be a constant .
Member method : It can be abstract , It can also be non abstract .
Interface :
Construction method : There is no way to construct
Member variables : It can only be constant . Default modifier :public static final
Member method :jdk1.7 It can only be abstract . Default modifier :public abstract ( recommend : Please always give the default modifier manually ),jdk1.8 It can be written as default and static The specific method at the beginning
1.2、 The relationship between class and interface is different :
Class and class :
Inheritance relationships , Only inheritance . Multiple levels of inheritance .
Classes and interfaces :
Realization relationship , It can be realized by itself , It can also be realized more .
Class can also inherit a class and implement multiple interfaces at the same time .
Interface and interface :
Inheritance relationships , Can inherit alone , You can also inherit more .
1.3、 Reflect different ideas :
What is defined in an abstract class is the common content in an inheritance system .
An interface is a collection of functions , Is an additional function of the system , It's the exposed rules
When you focus on the nature of a thing , Use abstract classes ; When you focus on an operation , Use interface .
2、 What is the inner class , Use scenarios
2.1、 What is an inner class
Define a class in a class or a method
2.2、 Types of inner classes
Member inner class 、 Static inner class 、 Anonymous inner class 、 Local inner classes
2.2.1 Member inner class
Member inner class is the most common inner class , The inner class of a member can access all properties and methods of the outer class , But the external class accesses the properties and methods of the internal class , You must instantiate the inner class , Member inner classes cannot contain static properties and methods .
public class InnerClassTest {
public int outField1 = 1;
protected int outField2 = 2;
int outField3 = 3;
private int outField4 = 4;
public InnerClassTest() {
// Inside the external class object , Directly through new InnerClass(); Create inner class objects
InnerClassA innerObj = new InnerClassA();
System.out.println(" establish " + this.getClass().getSimpleName() + " object ");
System.out.println(" Of its inner class field1 The value of the field is : " + innerObj.field1);
System.out.println(" Of its inner class field2 The value of the field is : " + innerObj.field2);
System.out.println(" Of its inner class field3 The value of the field is : " + innerObj.field3);
System.out.println(" Of its inner class field4 The value of the field is : " + innerObj.field4);
}
public class InnerClassA {
public int field1 = 5;
protected int field2 = 6;
int field3 = 7;
private int field4 = 8;
// static int field5 = 5; // Compile error ! You cannot define static attribute
public InnerClassA() {
System.out.println(" establish " + this.getClass().getSimpleName() + " object ");
System.out.println(" Of its external class outField1 The value of the field is : " + outField1);
System.out.println(" Of its external class outField2 The value of the field is : " + outField2);
System.out.println(" Of its external class outField3 The value of the field is : " + outField3);
System.out.println(" Of its external class outField4 The value of the field is : " + outField4);
}
}
public static void main(String[] args) {
InnerClassTest outerObj = new InnerClassTest();
// Not inside the outer class , Use : External class objects . new Inner class constructors (); To create an internal class object
// InnerClassA innerObj = outerObj.new InnerClassA();
}
}
2.2.2 Static inner class
Static inner class is to add one more in the inner class of the member static keyword . Static inner class Can only Access static member variables and methods of external classes ( Including private static )
public class InnerClassTest {
public int field1 = 1;
public InnerClassTest() {
System.out.println(" establish " + this.getClass().getSimpleName() + " object ");
// Create static inner class objects
StaticClass innerObj = new StaticClass();
System.out.println(" Of its inner class field1 The value of the field is : " + innerObj.field1);
System.out.println(" Of its inner class field2 The value of the field is : " + innerObj.field2);
System.out.println(" Of its inner class field3 The value of the field is : " + innerObj.field3);
System.out.println(" Of its inner class field4 The value of the field is : " + innerObj.field4);
}
static class StaticClass {
public int field1 = 1;
protected int field2 = 2;
int field3 = 3;
private int field4 = 4;
// Static inner classes can define static attribute
static int field5 = 5;
public StaticClass() {
System.out.println(" establish " + StaticClass.class.getSimpleName() + " object ");
// System.out.println(" Of its external class field1 The value of the field is : " + field1); // Compile error !!
}
}
public static void main(String[] args) {
// No need to rely on external class objects , Create internal class objects directly
// InnerClassTest.StaticClass staticClassObj = new InnerClassTest.StaticClass();
InnerClassTest outerObj = new InnerClassTest();
}
}
2.2.3 Anonymous inner class
Anonymous inner classes take many forms , One of the most common forms is to create an interface object in a method parameter / Class object , And implement the interface declaration / Class :
Thread pool execution method execute Inside new Runnable,Runnable It's the anonymous inner class
public class InnerClassTest {
public int field1 = 1;
protected int field2 = 2;
int field3 = 3;
private int field4 = 4;
public InnerClassTest() {
System.out.println(" establish " + this.getClass().getSimpleName() + " object ");
}
// Custom interface
interface OnClickListener {
void onClick(Object obj);
}
private void anonymousClassTest() {
// In this process, an anonymous inner class object will be created ,
// This anonymous inner class implements OnClickListener Interface and override onClick Method
OnClickListener clickListener = new OnClickListener() {
// You can define properties in inner classes , But it can only be used in the current inner class ,
// Cannot use... In an external class , Because the external class cannot get the class name of the current anonymous internal class ,
// You can't create objects of anonymous inner classes
int field = 1;
@Override
public void onClick(Object obj) {
System.out.println(" object " + obj + " Clicked ");
System.out.println(" Of its external class field1 The value of the field is : " + field1);
System.out.println(" Of its external class field2 The value of the field is : " + field2);
System.out.println(" Of its external class field3 The value of the field is : " + field3);
System.out.println(" Of its external class field4 The value of the field is : " + field4);
}
};
// new Object() The procedure creates an anonymous inner class , Inherited from Object class ,
// Rewrite the toString() Method
clickListener.onClick(new Object() {
@Override
public String toString() {
return "obj1";
}
});
}
public static void main(String[] args) {
InnerClassTest outObj = new InnerClassTest();
outObj.anonymousClassTest();
}
}
2.2.4 Local inner classes
Local inner classes are rarely used , It is declared in a method body / Inside a code block , And it cannot be used if it is not within the definition domain of the definition class , The functions it provides can be implemented using anonymous inner classes , The anonymous inner class itself can be written more succinctly , Therefore, local inner classes are rarely used . Take a look at a small example of a local inner class :
public class InnerClassTest {
public int field1 = 1;
protected int field2 = 2;
int field3 = 3;
private int field4 = 4;
public InnerClassTest() {
System.out.println(" establish " + this.getClass().getSimpleName() + " object ");
}
private void localInnerClassTest() {
// Local inner classes A, Can only be used in the current method
class A {
// static int field = 1; // Compile error ! Cannot be defined in a local inner class static Field
public A() {
System.out.println(" establish " + A.class.getSimpleName() + " object ");
System.out.println(" Of its external class field1 The value of the field is : " + field1);
System.out.println(" Of its external class field2 The value of the field is : " + field2);
System.out.println(" Of its external class field3 The value of the field is : " + field3);
System.out.println(" Of its external class field4 The value of the field is : " + field4);
}
}
A a = new A();
if (true) {
// Local inner classes B, Can only be used in the current code block
class B {
public B() {
System.out.println(" establish " + B.class.getSimpleName() + " object ");
System.out.println(" Of its external class field1 The value of the field is : " + field1);
System.out.println(" Of its external class field2 The value of the field is : " + field2);
System.out.println(" Of its external class field3 The value of the field is : " + field3);
System.out.println(" Of its external class field4 The value of the field is : " + field4);
}
}
B b = new B();
}
// B b1 = new B(); // Compile error ! Not in class B In the domain of , Class not found B,
}
public static void main(String[] args) {
InnerClassTest outObj = new InnerClassTest();
outObj.localInnerClassTest();
}
}
3、 Internal class function
3.1、 Inner classes can be well hidden .
Non inner classes cannot be used private and protected Embellished , But inner classes can , So as to achieve the function of hiding . At the same time, you can also organize classes with certain logical relationships , enhance readability .
3.2、 Indirect implementation of multiple inheritance .
Each inner class can inherit independently from a ( Interface ) Realization , So whether or not the outer class has inherited a certain ( Interface ) Realization , No effect on inner classes . If there is no internal class to provide the ability to inherit multiple concrete or abstract classes , Some design and programming problems are hard to solve . So the inner class indirectly implements multiple inheritance .
边栏推荐
- Traversal recursion + iteration of binary tree
- Pengge C language sixth class
- ThreadPoolExecutor是怎样执行任务的
- Sword finger offer (43): left rotation string
- WinPcap packet capturing function pcap_ Loop (), stop the problem
- 2021-08-14 Sanzi chess
- Constructors, method overloads, object arrays, and static
- LIst和Dictionary实例应用(※)
- mysql20210906
- Bash shell学习笔记(二)
猜你喜欢

35. Search the insertion position

菜鸟看源码之ArrayDeque

pytest 用例执行顺序

看源码之LinkedList

用两个栈实现队列

RT thread learning notes (III) -- building a compilation environment with scons

242.有效的字母异位词

Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2

SCADA and three industrial control systems PLC, DCS and FCS

232.用栈实现队列
随机推荐
创建EOS账户 Action
13 managing resources by objects
Common classes (understand)
看源码之LinkedList
C语言命名空间的定义与使用
MySQL quick learning notes-2021-08-31
MultipartFil转为File
1837.K进制表示下的各位数字总和
Bash shell学习笔记(七)
232. Implement queue with stack
The assignment of member pointer defined in C structure and the use of structure pointer as member function parameter
菜鸟看源码之ArrayDeque
Bash shell learning notes (III)
@Validated 和 @Valid 的真正区别和使用方式
logging 高级使用
Pengge C language - minesweeping 2021-08-16
@The difference and use of jsonformat and @datetimeformat
Codepoint 58880 not found in font, aborting. flutter build apk时报错
Summary of the seventh class of pengge C language
微信公众号开发 获取openid时报错40029 invalid code 问题的解决