当前位置:网站首页>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 .
边栏推荐
- @JsonFormat和@DateTimeFormat的区别和使用
- Flutter jni混淆 引入.so文件release包闪退
- Sword finger offer (49): convert a string to an integer
- Definition and use of C language namespace
- Halcon模板匹配之Shape
- Bash shell learning notes (III)
- Add touch screen driver for stemwin 5.22 on Shenzhou IV development board
- @NotBlank、@NotNull 、@NotEmpty 区别和使用
- Happens-Before原则深入解读
- Fragment 懒加载
猜你喜欢
随机推荐
Many people don't know whether they are looking for Kanban software or Kanban software
c结构体中定义的成员指针赋值与结构体指针作为成员函数参数的使用
logging 学习最终版-配置的不同级别日志打印的颜色
创建EOS账户 Action
RT thread learning notes (VIII) -- start the elmfat file system based on SPI flash (Part 2)
During the interview, how did the interviewer evaluate the level of rust engineers?
使用Selenium抓取zabbix性能监控图
2021-08-13 learn C language with pengge - array
Successfully transplanted stemwin v5.22 on Shenzhou IV development board
242.有效的字母异位词
Sword finger offer (43): left rotation string
Halcon模板匹配之Shape
Sword finger offer (V): queue with two stacks
访问权限——private,public,protected
@Notblank, @notnull, @notempty differences and uses
Sql Server 数据库之数据类型
232. Implement queue with stack
The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
像素和内存的关系
Fragment lazy load







![[dectectron2] follow the official demo](/img/aa/03e46897234c309415b336ac39b7d9.png)

