first, Static
1, attributes: class variables defined within the class, outside of the method, all classes valid, all class public one property
Class variables are independent of object creation, with default value of 0
Usage class Name. class variable System.out.println ();
Role: Counter Generation ID
Class user{
static int count;
Public User () {
count++;
}
}
Class account{
Long id;
static Long num = 10000L;
Public account () {
id = num++;
}
}
public class teststatic{
public static void main (string[] Args) {
New User ();
New User ();
New User ();
System.out.println (user.cout);
Account A1 = new Account ();
Account A2 = new Account ();
System.out.println (a1.id);
System.out.pringln (a2.id);
}
}
/*
New Account (); Call the construct method to assign the id attribute ID 10000L num++ 10001L
New Account (); ID 10001L num++ 10002L
ID 10002L num++
*/
2. Method: static method
The common method of adding static, the tool method, the whole class share a function
It is not possible to define static methods by themselves, usually using static methods written in the JDK
How to Use: class Name. static method name () math.sqrt ();
Attention:
1 "override method name, return value, parameter Table. Modifiers are the same as, or more broadly than, the parent class
2 "static methods can only be overridden by static methods, and there is no polymorphism,
What type is a reference type and what type of method is called?
3. Initialize the code block
Static initialization of code base
Function: Assigning a value to a static property
Class is executed once at load time, only once
Class Loading: when a virtual machine uses a class for the first time, it needs information about the class to be loaded into memory and stored in the process
Initializes a static property when the class loads, and executes a static initialization block of code
Class loading Time:
New object for class loading, access to static members, subclass object class loading Trigger parent class object class loaded First
Final and final
Variables: constants cannot modify values stored in variables (primitive type values cannot be changed, reference type addresses cannot be changed)
Final retouching of instance variables and class variables without a default value, requiring manual assignment of values
Instance variable constructor method assignment or initial value
Class variable initial value or static initialization code block assignment value
public static final Good partner
Method: cannot be overwritten (inheritable, callable, can be overloaded)
Class: cannot be inherited
Abstract abstraction (proper use of constrained polymorphic grammars)
Class cannot be a new object
method does not implement part only declaration part
Note: classes with abstract methods must be abstract classes
Subclass inherits the abstract class, or chooses to implement all the abstract methods,
If the corresponding abstract method is not implemented, the class must also be an abstract class
Interface
Grammar:
Special abstract classes, for polymorphic services, can only declare references, not new objects
Keyword interface
Implementing interfaces (like inheriting abstract classes) implements
All properties public static final (default)
All methods public abstract (default)
No construction method
Role:
Support Multiple inheritance
Interfaces and interfaces direct multiple inheritance extends
Classes and Interfaces direct multiple inheritance implements
Note: A class can inherit only one parent class, enabling multiple interfaces (drop-coupling Tool)
Standard: (polymorphic)
Interface Callback: after the interface has been defined, the existing interface consumer, and then the implementation of the definition interface
====================================================
The difference between =============== interface and abstract class ============================================================
- The same point of the abstract class and interface
①. In the Java language, interfaces and abstract classes cannot be instantiated, and are located at the top of the inheritance tree for implementation and inheritance by other Classes.
②. Both interfaces and abstract classes can contain abstract methods.
③. Ordinary subclasses that implement interfaces or inherit abstract classes must implement these abstract methods.
- Different points of abstract classes and interfaces
1) Abstract classes can provide implementation details of member methods, and only public abstract methods exist in interfaces ;
2) member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types;
3) The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;
4) A class can inherit only one abstract class, while a class may implement multiple Interfaces.
5) A static method cannot be defined in an interface, and a static method can be defined in an abstract class.
6) The interface does not contain constructors, and abstract classes can contain Constructors. Constructors in abstract classes are not used to create objects, but rather to have their subclasses call these constructors to complete initialization operations that are part of an abstract class. No configuration can be defined in the Interface.
method, the system does not provide a default construction method.
7) The interface cannot contain initialization blocks, but abstract classes can contain initialization blocks.
- Application (adapter Mode)
Objective:
transforms the interface of one class into another interface that the customer Wants. a D a P T e r mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work Together.
Principle:
Target Interface ( Target): The interface that the customer Expects.
a class that needs to be adapted ( Adaptee): A class or adapter class that needs to be adapted.
Adapter ( Adapter): Converts the original interface into a target interface by wrapping an object that needs to be adapted.
Instance:
If a normal class implements an interface, it overwrites all the abstract methods in this Interface. If there are too many methods defined in one port, but not so many abstract methods in the subclass, then it must be cumbersome to
To a middle over, but the over class does not want to be used directly, so the excessive class is defined as the most appropriate abstract class (adapter), and in this abstract class to implement all the methods in the interface, the method body is empty, then the subsequent subclass directly inherit
This abstract class allows you to selectively override methods of the parent class as Needed.
Three major modifiers in Java