2021-03-11

Object Oriented Fundamentals

1. Enumeration

1.1 Before JDK1.5

How to declare enumeration class before JDK1.5?

  • Constructor plus private privatization
  • Create a set of constant objects inside this class, and add public static modifiers to expose these constant objects to the outside
1.2 After JDK1.5

Syntax format:

修饰符】 enum 枚举类名{
    
    
    常量对象列表
}

【修饰符】 enum 枚举类名{
    
    
    常量对象列表;
    
    其他成员列表;
}

Sample code:

public class TestEnum {
    
    
	public static void main(String[] args) {
    
    
		Season spring = Season.SPRING;
		System.out.println(spring);
	}
}
enum Season{
    
    
	SPRING,SUMMER,AUTUMN,WINTER
}

Requirements and characteristics of enumeration classes:

  • The list of constant objects of the enumeration class must be in the first line of the enumeration class, because it is a constant, it is recommended to uppercase.
  • If there is no other code behind the constant object list, ";" can be omitted, otherwise ";" cannot be omitted.
  • The compiler provides a private parameterless structure for enumeration classes by default. If the enumeration class requires a parameterless structure, there is no need to declare, and there is no need to add parameters when writing a list of constant objects.
  • If the enumeration class needs a parameterized structure, you need to manually define the private parameterized structure. The method of calling the parameterized structure is to add (argument list) after the constant object name.
  • The enumeration class inherits the java.lang.Enum class by default, so it can no longer inherit other types.
  • After JDK1.5, switch provides support for enumerated types, and enum constant names can be written after case.
  • If the enumeration type has other properties, it is recommended ( but not necessary ) that these properties are also declared as final, because the constant object should be immutable in the logical sense.
1.3 Commonly used methods of enumerated types
1.toString(): 默认返回的是常量名(对象名),可以继续手动重写该方法!
2.name():返回的是常量名(对象名) 【很少使用】
3.ordinal():返回常量的次序号,默认从0开始
4.values():返回该枚举类的所有的常量对象,返回类型是当前枚举的数组类型,是一个静态方法
5.valueOf(String name):根据枚举常量对象名称获取枚举对象

2. Packaging

2.1 Packaging

Java provides two type systems, basic types and reference types. The use of basic types is efficiency. However, when you want to use APIs or new features (such as generics) designed only for objects, then the data of basic data types need to use packaging classes Come to pack.

2.2 Packing and unpacking

Packing: Convert basic data types to packaging objects.

The object of the packaging class is to use the API and features specifically designed for the object

Unboxing: Unpacking the packaging objects into basic data types.

Converting to basic data types is generally due to the need for operations. Most operators in Java are designed for basic data types. Comparison, arithmetic, etc.

Basic value---->Package object

Integer i1 = new Integer(4);//使用构造函数函数
Integer i2 = Integer.valueOf(4);//使用包装类中的valueOf方法

Package object---->Basic value

Integer i1 = new Integer(4);
int num1 = i1.intValue();

After JDK1.5, boxing and unboxing can be automated.

Note: Automatic boxing and unboxing can only be realized between the types corresponding to oneself

3. Some APIs of the packaging class

3.1 Conversion between basic data types and strings

(1) Convert the basic data type to a string

int a = 10;
//String str = a;//错误的
//方式一:
String str = a + "";
//方式二:
String str = String.valueOf(a);

(2) Convert the string to the basic data type
String into the corresponding basic type. Except for the Character class, all other packaging classes have the parseXxx static method to convert the string parameter to the corresponding basic type.

4. Interface

The interface is the specification, which defines a set of rules, which embodies the idea of ​​"if you are/want... you must be able to..." in the real world. Inheritance is an is-a relationship of "is it right", while interface implementation is a has-a relationship of "can it".

4.1 Defining the format

Definition of the interface, which defined the class in a similar manner, but using interfacekeywords. It will also be compiled into a .class file, but it must be clear that it is not a class, but another reference data type.

引用数据类型:数组,类,接口。
4.2 Interface declaration format
【修饰符】 interface 接口名{
    
    
    //接口的成员列表:
    // 静态常量
    // 抽象方法
    // 默认方法
    // 静态方法
    // 私有方法
}

Sample code:

interface Usb3{
    
    
    //静态常量
	long MAX_SPEED = 500*1024*1024;//500MB/s
    
    //抽象方法
	void read();
    void write();
    
    //默认方法
    public default void start(){
    
    
        System.out.println("开始");
    }
    public default void stop(){
    
    
        System.out.println("结束");
    }
    
    //静态方法
    public static void show(){
    
    
        System.out.println("USB 3.0可以同步全速地进行读写操作");
    }
}
4.3 Member description of the interface

The interface defines the common behavioral norms common to multiple classes. These behavioral norms are channels for communication with the outside, which means that a set of public methods are usually defined in the interface.

Before JDK8, only allowed to appear in the interface:

(1) Public static constants: public static final can be omitted
(2) Public abstract methods: public abstract can be omitted

Understanding: An interface is a specification abstracted from multiple similar classes and does not need to provide specific implementation

In JDK1.8, default methods and static methods are allowed to be declared in the interface:

(3) Public default method: public can be omitted, it is recommended to keep, but default cannot be omitted
(4) Public static method: public can be omitted, it is recommended to keep, but static cannot be omitted
in JDK1.9, the interface Added:
(5) Private methods
In addition, there can be no other members in the interface, no constructor, no initialization block, because there are no member variables in the interface that need to be initialized.

4.4 Implementing the interface

The use of the interface, it can not create objects , but can be implemented ( implements, similar to being inherited).

The relationship between the class and the interface is the realization relationship, that is, the class implements the interface . This class can be called the realization class of the interface or the subclass of the interface. Implemented acts similar inheritance, the format is similar, but different keywords, implemented using implementskeywords.

4.5 Implementing the interface syntax format
【修饰符】 class 实现类  implements 接口{
    
    
	// 重写接口中抽象方法【必须】,当然如果实现类是抽象类,那么可以不重写
  	// 重写接口中默认方法【可选】
}

【修饰符】 class 实现类 extends 父类 implements 接口{
    
    
    // 重写接口中抽象方法【必须】,当然如果实现类是抽象类,那么可以不重写
  	// 重写接口中默认方法【可选】
}
4.5 How to call the corresponding method
  • For the static method of the interface, you can directly use the "interface name." to call
  • You can only use the "interface name." to call, not through the object of the implementation class
  • The abstract method and default method of the interface can only be called by implementing the class object
  • Interface cannot create objects directly, only objects of implementation classes can be created
4.5 Multiple implementations of interfaces

As I learned before, in the inheritance system, a class can only inherit one parent class. For interfaces, a class can implement multiple interfaces, which is called multiple implementations of interfaces . Moreover, a class can inherit from a parent class and implement multiple interfaces at the same time.

4.6 Default method conflict problem
  • Daddy first principle
  • Must make a choice
4.7 Multiple inheritance of interfaces

An interface can inherit other or a plurality of interfaces, the interface inheritance use extendskeyword, the parent child inherited interfaces to interfaces.

4.8 Polymorphic references of interfaces and implementation class objects

The implementation class implements the interface, similar to the subclass inheriting the parent class. Therefore, the variable of the interface type and the object of the implementation class can also constitute a polymorphic reference. Call the method through the variable of the interface type, and the final execution is the method body implemented by your new implementation class object.

public class TestInterface {
    
    
	public static void main(String[] args) {
    
    
		Flyable b = new Bird();
		b.fly();
		
		Flyable k = new Kite();
		k.fly();
	}
}
interface Flyable{
    
    
    //抽象方法
	void fly();
}
class Bird implements Flyable{
    
    

	@Override
	public void fly() {
    
    
		System.out.println("展翅高飞");
	}
	
}
class Kite implements Flyable{
    
    

	@Override
	public void fly() {
    
    
		System.out.println("别拽我,我要飞");
	}
	
}

5. Inner Class

5.1 Overview

1. What is an internal class?

Define a class A in another class B, the class A inside is called the inner class , and B is called the outer class .

2. Why declare inner classes?

When there is a part inside a thing that needs a complete structure to describe, and this complete internal structure only provides services for external things and is not used alone in other places, then the entire internal complete structure is best to use internal classes .

And because the inner class is inside the outer class, it can directly access the private members of the outer class.

3. What are the forms of internal classes?

According to the position of the internal class declaration (like the classification of variables), we can be divided into:

(1) Member inner class:

  • Static member inner class
  • Non-static member inner class

(2) Local inner class

  • Named local inner class
  • Anonymous inner class

6. Annotation

6.1 What is a comment

Annotations exist in the code as " @ annotation name ", and some parameter values ​​can also be added, such as

@SuppressWarnings(value=”unchecked”)
@Override
@Deprecated
@Test
@author
@param
....

Annotation Annotation is introduced from ***JDK5.0***.

Although comments are also a kind of comments, because they will not change the original logic of the program, but add some annotation information to the program. However, it is different from single-line comments and multi-line comments. For single-line comments and multi-line comments, it is shown to programmers, and comments are a kind of comments that can be read by compilers or other programs. Programs can also be based on different comments. , Make the corresponding treatment. So annotations are tags inserted into the code so that tools can process them.

A complete comment has three parts:

  • Annotated declaration: just like classes, methods, variables, etc., you need to declare before using
  • The use of annotations: used to annotate one or more of the 10 positions above the package, class, method, attribute, structure, local variable, etc.
  • Annotation reading: There is a section dedicated to reading these used annotations, and then corresponding processing is made according to the annotation information. This procedure is called the annotation processing flow, which is also the biggest difference between annotations and ordinary annotations.
6.2 The three most basic annotations predefined by the system
1、@Override

​ Used to detect that the modified method is an effective rewrite method, if not, a compilation error will be reported!

​ Can only be marked on the method.

​ It will be read by the compiler program.

2、@Deprecated

​ Used to indicate that the marked data is out of date and is not recommended.

​ Can be used to modify attributes, methods, structures, classes, packages, local variables, and parameters.

​ It will be read by the compiler program.

3、@SuppressWarnings

​ Suppress compilation warnings.

​ Can be used to modify classes, attributes, methods, constructions, local variables, and parameters

​ It will be read by the compiler program.

Guess you like

Origin blog.csdn.net/qq_37698495/article/details/114679775