Internal and abnormal classes of Java program development and learning

(Study reference book: Java2 practical tutorial fifth edition)

One, internal class

Java supports the definition of a class in another class, such a class is called an inner class, and the class contained in the inner class is called an outer class of the inner class. The relationship between inner class and outer embedded class is as follows:

  • The member variables of the external class are still valid in the internal class, and the methods of the internal class can also call the methods of the external class
  • Class variables and class methods (static) cannot be declared in the body of an inner class. The external class can use the internal class to declare the object as a member of the external class
  • The inner class is only used by its outer class, other classes cannot use the inner class of a certain class to declare objects

The bytecode file of the internal class generated by the Java compiler is different from the usual class (class name.class), but the external class name $internal class name.class
has and only internal classes can be modified as static classes, and the program can Use the static inner class to create objects in other classes. Static internal classes cannot manipulate instance member variables of external embedded classes.

Two, anonymous class

Java allows you to create a subclass object directly using the class body of a subclass of a class. It means that in addition to the construction method of the parent class, there are also class bodies that can create subclass objects. This class body is considered to be a subclass without class declarations, and is called an anonymous class. An anonymous class is a subclass . Since no name is available, it is impossible to declare an object using an anonymous class, but an object can be created directly. The following is to create an object with an anonymous class:

new 父类名() {
    
    
	匿名类的类体
};

The object created by the anonymous class can only be used as the upper transformation object as a method parameter to simplify the code.

(1) Features of anonymous classes

  • An anonymous class can inherit the methods of the parent class, or override the methods of the parent class.
  • When an anonymous class is used, it must be directly used to create an object in a certain class, so the anonymous class must be an inner class.
  • Anonymous classes can access member variables and methods in external classes, and static member variables and methods cannot be declared in the class body of anonymous classes.
  • Since the anonymous class is a subclass, but there is no class name, so when you create an object with the anonymous class, you should directly use the construction method of the parent class.

(2) The role of anonymous classes If
users want to pass subclass parameters to the method, they can create anonymous class objects directly in the method's parameter list.

(3) Anonymous classes related to interfaces
Java also allows to create an anonymous object directly with the interface name and a class body. This class body is considered to be the class body that implements the interface without the class declaration. The class body must override all methods in the interface. as follows:

new 接口名() {
    
    
	实现接口的匿名类的类体
};

Three, abnormal class

Java uses throw关键字objects that throw an Exception subclass to indicate that an exception has occurred.

(1) Try-catch statement
Java uses try-catch statement to handle exceptions, and places possible abnormal operations in the try part of the try-catch statement. Once try throws an exception object or calls a method that may throw an exception object ( And the method throws an exception object), then the try part will immediately stop execution (out of the try block) , and then execute the catch part. A try-catch can consist of several catches.

try {
    
    
	可能发生异常的语句
}
catch(ExceptionSubClass1 e) {
    
    
	...
}
catch(ExceptionSubClass2 e) {
    
    
	...
}

(2) Custom exception class
When writing a program, you can extend the Exception class (system exception class) to define your own exception class , and then specify which methods to generate these exceptions according to the needs of the program. When a method is declared, you can use throws关键字several exceptions to be generated by the declaration (method declaration throws custom exception class name), and specify the operation that generates the exception in the method body of the method, that is, create an object with the corresponding exception class. And use the throw keyword to throw the exception object, causing the method to end execution. The program must call the method of the possible exception in the try-catch statement block, where the role of the catch is to capture the exception object thrown by the throw keyword.

throw:用在方法体中表示抛出异常
throws:用在自定义异常方法声明时表示这个方法会抛出异常。

Four, assertion

Assertion statements are very effective in the debugging phase of the code, and are generally used for errors that the program is not prepared to handle by catching exceptions.

(1) Syntax format
Use the keyword assert to declare an assertion statement. There are two formats:

assert 返回值为boolean类型的表达式;

If the expression returns true, the program continues to execute, otherwise the program stops immediately.

assert 返回值为boolean类型的表达式:执行表达式;

If the boolean type expression returns true, the program continues to execute. If the return value is false, the program stops execution from the assertion and outputs the value of the execution expression.

(2) Enable and disable assert statements

When using the Java interpreter to run the application directly, the assertion statement is turned off by default. When debugging the program, you can use -ea to enable the assertion statement:java -ea mainClass

Guess you like

Origin blog.csdn.net/YCF8746/article/details/112999025