8. static
8.1 general
It is hoped that the attribute or method is not owned by the specific object, but belongs to the class,
Loads as the class loads
In the memory pool, save a copy in the static field of the method area
8.2 static properties
static can be used to decorate: attributes, methods, code blocks, internal classes
class chinese{ private static String contory; //One copy of the whole memory is shared by all objects private String name; }
8.3 static method
Call relationship, see life cycle
this, super cannot be used in static methods
8.4 singleton mode
Singleton: only one object can be created for a single instance of a class
The constructor permission is private, so it cannot be created externally with new
Internally, a static method is used to create an object. At the same time, because static methods can only access static member variables, all members of the class must be static
8.4.1 two methods of creating singleton mode
Starving thread safety, but the object takes too long to load
Lazy delay object creation thread is not safe (two threads want to create a singleton object at the same time. If they judge that they are null, they will each create a new one)
//Hungry Han style class Singleton1{ //Private construction method private Singleton1(){ pass; } //Create object inside class private static Bank ins = new Singleton1(); //Provides an object of a common method return class public static Singleton1 getInstance(){ return ins; } }
//Lazy style class A{ private A(){ pass; } //Declare the current class object without initialization private static A ins = null; //Provides an object of a common method return class public static A getInsance(){ if(ins==null){ ins = new A(); } return ins; } }
8.4.2 usage scenarios
Database connection pool, website counter, log processing, profile class, windows Task Manager, recycle bin, Application
9. Code block (initialization block)
{code block} static {code block}
It is usually used to initialize classes or objects,
It can only be modified with static or not
There can be multiple code blocks, which are executed in sequence (there is no need to write two static code blocks.) (even, there is no need to write code blocks)
9.1 static code block
Execute with class loading!
Just once
9.2 non static code blocks
Executes as the object is created
Every time an object is created, it is executed once
Function: you can initialize attributes when creating objects
10. final
final can be used to modify: class (simply cut), method (can't be rewritten, your father gives it to you, but doesn't rewrite it to you), variable
10.1 final modifier methods and classes
Permission [final] class A {}
[permission] [final] [static] [final] return value method name ()
Classes modified by final: String, System, StringBuffer
Method modified by final: getClass() of Object
10.2 final modifier variables and attributes
Variable: becomes a constant
Attribute: assignment can be placed in: explicit initialization position, code block and constructor. Cannot be used at object. Property
Local variables: Method: become constant
Formal parameter: the method body can no longer be modified
10.3 static+final
static final modifier attribute: global constants are common in interfaces
11. Abstract class, abstract method
If there are too many sons, the parent class becomes an abstraction of public things, then the actual use of the parent object is less. Why do you want the parent class to be written as an abstract class instead of instantiation
abstract: used to modify classes and methods
abstract class Person{ String name; int age; public Person(){ } } class Sun{ }
11.1 abstract classes
There must be a construction method. Although it cannot be used by itself, it can be used by the subclass of the implementation
11.2 abstract methods
The class must be abstract
There can be no abstract methods, there can be ordinary member methods, and this member method can also call abstract methods
Cannot modify constructor, private method, static method, final method
11.3 anonymous subclasses of abstract classes
It's really common
There are too many methods of abstract classes, so don't use them!
abstract class Person{ public abstract void eat(); } //Method declaration public static void function(Person p){} //call function(new Person{ @override public void eat(){} })
11.4 design mode: template method
The internal part of the function is realistic and uncertain. At this time, the uncertain part can be exposed and left to subclasses for implementation
12. interface
The primary purpose of an interface is to implement multi inheritance, and the secondary purpose is to simply abstract the common features of multiple classes
implements implementation
An implementation class is an abstract class if it does not have all the methods in the implements interface
12.1 interface members:
JDK7:
Global constant: public static final
Abstract method: public abstract
JDK8+:
You can also define:
Static methods: can have method bodies (interfaces are more and more like classes)
However, there is an interface a {static func1()}, which implements class B. when calling b.func1()
Only A.func1();
Default method: the default method is decorated with default. It can be called through multiple class objects. It can be overridden, but the default should be removed
Construction methods must not be defined
interface Flyable{ //Global constant public static final int MAX_SPEED =7900; int MIN_SPEED =7900; //Ibid., omitted (including public) //Abstract method public abstract void fly(); void stop();//Omitted ibid //Static method public static void method1(){ } //Default method public default void method2(){} //Default default write default void method3(){} //ellipsis }
12.2 interface inheritance
java can implement multiple interfaces and inherit between interfaces!!!
12.3 name duplication caused by multiple inheritance
There are functions with duplicate names in the parent class and interface. It is specified that if the child class is not overridden, the class priority principle of the parent class will be used by default
class A extends B implements C{ }
There are functions with duplicate names in the two interfaces, which specify:
< 1> If the default method is defined, the implementation class must be overridden (interface conflict). If you want to use the default method of the interface, B.super.func()
class A implements B,C{ }
12.3 use of interfaces (polymorphic)
interface USB{ void start(); void stop(); } class Flash implements USB{ public void start(){} public void stop(){} } class Computer{ public void transferData(USB sub){ usb.start(); ... usb.stop(); } } main(){ Computer com =new Computer(); //An object that implements a class anonymously Flash flash =new Flash(); com.transferData(flash) //Anonymous object of anonymous implementation class comg.transferData(new USB{ public void start(){} public void stop(){} }); }
12.4 design mode: Agent Mode
Use more
Is to provide a proxy for other objects to control access to this object
An interface is implemented by two classes, a proxy class and a proxy class
interface NetWork{ public void browse(); } //Proxy class class Server implements NetWork{ @oiverride public void browse(){} } //proxy class class ProxyServer implements NetWork{ private NetWork work; public ProxyServer(NetWork work){ this.work = work; } public void check(){ sout("Detection before networking"); } @override public void browse(){ check(); work.browse(); } } main(){ Server server = new Server(); ProxyServer proxyServer = new ProxyServer(server); proxyServer.browse(); }
Application scenario: Security agent, which blocks direct access to real roles
Remote proxy: Handling remote method calls (RMI) through proxy classes
Delayed loading: load the lightweight proxy object first, and then load the real proxy object if necessary
12.5 design mode: factory mode
13. Internal class
The internal class TM only appears in the interview....
Do your own development. You have nothing to talk about writing this thing...
Isn't it good to live???
Classification: member inner classes (static member inner classes, and non static member inner classes)
Local inner class (within method, code block, construction method), anonymous inner class
class Person{ public void method(){ class A{}//Methods local innerclass in vivo } { class B{}//Local innerclass in code block } public Person(){ class C{}//Local innerclass in construction method } class Heat{//Member inner class } }
13.1 member internal class
As a member of a class:
> Things that can call external classes, but pay attention to static relationships
> this in innerclass refers to the internal class. If you want to call the properties of the external class, A.this.func()
> Can be modified by four permissions
As a class:
> What can be defined in a class: just an ordinary class
> It can be inherited, modified by final, and abstract
class A{ class B{ int age; public void show(){} } static class C{ int age } }
13.2 instantiation and differentiation of internal classes
class Person{ String name; public void eat(){sout("People eat")} static class Dog{ String name; public void eat(){sout("Dog eat")} } class Bird{ String name; public void eat(){sout("Birds eat")} //Distinguish attributes with the same name public void display( String name){ sout(name); sout(this.name); sout(Person.this.name); } } } main(){ Person.Dog dog = new Person.Dog(); //Create static member inner class dog.eat(); Person p = new Person(); Person.Bird bird = p.new Bird();//Create a non static member inner class bird.eat(); }
13.3 local internal class
Note: the internal class in the method body uses the local variable in the method body. This variable must be final. Even if you do not declare it, it will default to final. If you dare to change the value of the variable, he will dare to report an error
Before JDK6, errors were reported without writing final
why: the declaration cycle of local variables is gone except for functions, while classes, even local classes, are not destroyed until they are not used. The problem is, the classes are still there and the variables are gone. What should we do? Only final
class Person{ //Giant rare in development public void method(){ class A{}//Methods local innerclass in vivo } //In fact, an object of a class that implements the Comparable interface is returned //Method 1 public Comparable getComparable(){ class MyComparable implements Comparable{ ... } return new MyComparable(); } //Method 2, nonstandard public Comparable getComparable(){ return new Comparable(){ @override ... }; } { class B{}//Local innerclass in code block } public Person(){ class C{}//Local innerclass in construction method } }