1. Factory Method
What is the factory method?
Get Object by Method Call
Scenarios:
1) Users cannot create objects directly. Creating objects that are too complex can be effortless in a factory (mobile phone) (singleton)
2) Users do not want to participate in object creation
Comparison of abstract classes and interfaces
2. Internal Classes
Members of the class
Attributes: Describe the characteristics of things
Method: Describe the behavior of things
Pseudo members: constructors, statement blocks
Internal Class
What is an internal class?
Declare a class inside another class. The preceding class is called the inner class, and the latter is called the outer class.
Memory variable
Variables are scored by location
1. number variable: Variables declared outside of methods in a class that have a wide range and a long lifetime
1) Class variables: method areas with static modifications, subordinate to class templates, permanent lifetime, and stored in the heap
2) Instance variable: GC area stored in the heap with no static modification, life as long as object
2. Local variables: Variables declared in the method have a small range, short lifetime, and are stored on the stack along with the stack of the method
Classification of internal classes
1. Member internal classes: internal classes declared outside methods in classes
1) Nested Classes: static-modified, not strongly dependent, at the same level as external classes
Use is indistinguishable from internal classes
class Outer{ public static class Inner2{//Nested classes, members of objects of external classes cannot be used directly public static int no = 10; public static void test4(){//Create a static method in a nested class System.out.println("static test4()..."); } private String name = "Nested Class"; public Inner2(){//This is a parameterless constructor for nested classes System.out.println("inner2()..."); } public void test3(){//A non-static method was created in a nested class System.out.println("test3()..."); //System.out.println(Outer.this.name);//Members of objects of external classes cannot be used directly System.out.println(name); } } } public static void main2(String[] args) { Outer.Inner2.test4();//Static methods in nested classes can be called directly // Outer.Inner2.test3();//Non-static methods cannot be called directly in a static environment System.out.println(Outer.Inner2.no);//Static properties in nested classes can be called directly Outer.Inner2 inner2 = new Outer.Inner2();//Objects that can create nested classes inner2.test3();//Use this nested class object to invoke its non-static methods }
2) Ordinary internal class: no static modification, subordinate to external class objects
Members of external class objects can be freely used in internal class objects to replace common object associations
class Outer{ private String name = "External Class"; protected class Inner1{//Common internal class, subordinate to external class objects private String name = "I am an internal class"; //Ordinary internal classes do not allow ordinary static members, why?? //Because adding a static will belong to both classes and objects, both modifications will cause it to change and conflict // public static int id = 2; public int id3; public static final int id1 = 3;//If it is static, it can only be modified by final, because final is unique and will not be modified by changes in external classes public Inner1(){ System.out.println("Inner()...."); } public void test1(){ //Internal and external classes must be qualified if they have members with the same name System.out.println(this.name);//Proximity principle System.out.println(Inner1.this.name);//The principle of proximity may or may not be limited System.out.println(Outer.this.name);//The this of an external class must be qualified } } } public static void main1(String[] args) { Outer outer = new Outer(); outer.teat2(); //Create internal class objects directly, through external class objects.new Outer.Inner1 oi1 = new Outer().new Inner1(); oi1.test1(); }
Differences between ordinary object associations and internal classes
Internal classes are deeper and more exaggerated object associations. Internal classes associate external classes for the purpose of replacing common object associations so that objects are closely related (interoperable)
Object association, only the public part of the associated object can be used
Internal classes are not as associated with objects
2. Local internal classes: internal classes declared in methods
1) Common Local Internal Class
2) Anonymous Internal Class (Super Focus): Temporarily use an interface to implement subclass objects, use up or scrap.
Parent Reference= new Parent () {
The class body is the class body of the parent's subclass.
};
99.99% of cases are used with interfaces, which usually have only one abstract method.
Interface Reference= new Interface () {
A class body is the class body of an interface's entity subclass and must implement all abstract methods in the interface
}
Use scenarios
Lazy to formally write a specific subclass to implement an interface.
Absolute singleton of created objects
Can only be used polymorphically
Note: $denotes the relationship between internal and external classes
interface I7{ void hello(String s); } public static void main(String[] args) { Object o1 = new Object();//create object System.out.println(o1); Object o2 = new Object() {};//Anonymous inner class, which is an anonymous subclass of Object System.out.println(o2); //This writing omits declarations of specific subclasses and is suitable for one-time creation and use of objects I7 i7 = new I7() { //The class body part is the class body of the implementation subclass of the interface @Override public void hello(String s) { System.out.println("hi"+s); } }; i7.hello("fdasf"); }
Create internal class object
Qualification cannot be omitted in a test class
Local variable cannot be weighted limit modifier
3. Enumeration
What is an enumeration?
Enumerations are a special type of object countability, using the enum keyword
Can enumeration types create objects?
Enumeration types cannot create objects because the underlying layer encapsulates the constructor
Three ways to get enumerated objects
1) Get points directly
2) Name acquisition, based on a given constant object name, to get a reference to the corresponding object
3) Get an array of objects that hold all enumerated objects
Method of enumeration
Enumerate possible scenarios with switch
Constructors in enumerations must be private because they cannot create objects
4. Exception handling
What is exception handling?
Exception: An abnormal condition that occurs during operation. If not handled, the program crashes
Note: Grammar errors and logic errors are not exceptions during development!
Exception Classification
1. Score by degree
1) Error: Serious error.Serious issues that cannot be resolved by the java virtual machine.
2) Exception: General problem.Exceptions caused by programming errors or accidental accidents
2. Divide them according to the way they are handled
1) Checked exceptions: compile-time exceptions.Exceptions that must be checked and handled in the program, if not handled, compilation errors
Exceptions and their subclasses (except RuntimeException s): general issues that cannot be ignored
2) Non-Checked Exceptions: Runtime Exceptions.Exceptions that can be checked and handled are not accepted, and if not handled, compilation errors will occur, but runtime errors will occur
Error and its subclasses: Too severe,
RuntimeException and its subclasses: too mild, too common
Both checked and non-checked exceptions can cause a program to crash, so it should be handled
Exception stack throw mechanism
Exception handling
Applicable to all exceptions
1. Capture
try{
Statements that may throw exceptions;
}catch (exception type reference){
If an object of this exception type is actually thrown in the statement, it is caught here
}....{
}catch(Exception e){
//Make sure nothing goes wrong
}finally{
Statements that must be executed regardless of what happened in the previous try catch;
The main function is to release hardware resources outside the GC zone here
}
Can there be multiple catch es in exception handling?
Exception handling actually protects the back, with multiple catches to prevent incomplete types, or with Exception, a super-invincible omnipotent clip that ensures absolute certainty, but it's better to use multiple different catches
Benefit: Reduce the scope of abnormal damage
What is finally?Why finally?
finally is a statement that must be executed regardless of what happened in the previous try catch;
Because its main function is to release hardware resources outside the GC zone, otherwise a resource leak will occur
This statement intercepts finally
public class TestException { public static void main(String[] args) { System.out.println("main begin"); try{ int n = Integer.parseInt(args[0]); int m = Integer.parseInt(args[1]); System.out.println(n/m); }catch (NumberFormatException e){ System.out.println(e.getMessage()); }catch (ArithmeticException e){ System.out.println(e.getStackTrace()); }catch (ArrayIndexOutOfBoundsException e){ System.out.println(e); }catch (Exception e){ e.printStackTrace(); } System.out.println("main end");//Core Code } }
The only statement that intercepts finally
Combination method:
try catch
try catch finally
try finally
Understanding a small example of finally
summary
What I'm learning today:
1) Plant method;
2) Internal classes;
3) Enumeration;
4) Exception handling