JAVA ninth day

Advanced Language Features ... high-level language features
a .static modifier
static is static modification of this thing that is static, that is, belong to a class (best to use the class name in the form of static resources to access, eg.: Prople.age; Prople.show ();), it will be created when the class is created.
eg: private int age; call member variables
private static int age; called static member variables
Note: static things belong to the class -> You can not use this keyword resulted in a static method
non-static objects belong to what is
1.1: with where.
1.1.1: Modified member variable: Private static int Age;
1.1.2: Modified-member method: public static void Show () {}
1.1.3: a modified code block: static {}

1.2: static code block / non-static code block (anonymous block)
1.2.0: anonymous block: will be executed when the object is created.
Down sequentially executed from
each create an object will be executed once.
1.2.1: Static block: when the class loader will be called once, and called only once.
Uses: generally used class {} static attribute assignment.
1.2.2: non-static code block: every time creating an object is executed.

Two: all kinds of static resource
2.1: Static variables: Modifier
2.1.1: Disclaimer: declare member variables and similar:
public static int Age;

2.1.2: Memory mechanisms:
static resources are [belong to the class],
stored in static [method area] in memory,
the class is loaded: time (eg java Hello jvm will load the Hello class) will create a static memory resources space.
Therefore, the static resource [a class has only one],
then any instance of an object of this class of static resources to operate this type of operation is essentially the same resources.
-> [static resource plurality example operation,
operating the same resources].

Conclusion: static variables only one, all instances of the class object is shared in memory.
Note: jvm allocate memory for static variables at the time the class is loaded.
After the class instance variables are loaded, create an object when it allocates memory.

2.1.3: memory mechanism Test:
Test:
Prople attribute class has a static int age;
using 3 Prople created instance of an object
with three instances of objects in the static operation Prople operation age (age ++) respectively,
each operation is completed, respectively Print View age value of
the code:
class Prople {
public static int age;
}
class T {
public static void main (String MSG []) {
Prople P1 = new new Prople ();
Prople P2 = new new Prople ();
Prople P3 = Prople new new ();
p1.age ++;
System.out.println (p1.age);
p2.age ++;
System.out.println (p2.age);
p3.age ++;
System.out.println (p3.age);
}
}
2.2: static methods: one member methods (review: the member method static method = + instance methods)
2.2.1: statement: statement and similar methods, but need to add the static keyword (eg: public staitc void show ( ) {})
2.2.2: memory mechanism:
1, when a static method is class loading area of the memory space allocation method. ---> You can not use this keyword in a static method -> think about why?
2, static methods can not use the super keyword: Because the super keyword is used to access the current instance inherited from the parent class methods and properties. Specific examples of related keywords super class;

2.2.3: Method Static characteristics:
1, static methods must be implemented: a static method used to represent the class-specific features, to achieve this function does not depend on the specific example of the class, nor on its subclasses.

2, the static method does not exist, said override
3, static methods can not be overridden parent class non-static method
4, the non-static methods of the parent class is not overridden by a subclass static methods
can not have the same name as the parent class method wherein sub [ is a static modification]

5, only one static method in memory,
no matter how many instances of the class, share a memory.

6. static methods can be used directly, using the full name of the class call the
class name method name ();.
.. The full package name class name method name ();
7. static methods only allow access to static members.

8. The static methods can not be used in this (because this is associated with the instances).

2.3: static block of code: class static block of code, which does not exist in any method.
1, in the class java virtual machine executes the loaded static code blocks. If multiple code blocks static class, it will be performed sequentially from top to bottom.
2, static block of code which can only be defined in the class, the method can not be defined on the inside.
3, block static local variables in variables are only valid in the current code block.
4, static code block can only access the static member of the class, but does not allow access instance members.
5, the static code block is used to assign an initial value to a static variable.
6, the main code block initializing a static class. It only once, and executed before the main function.
Student {class
public static String name = Show ();
Private static int Age;
main () {

}
public Student(){
age = 20;
}
static{
2
age = 10;
}
public static String show(){
3
return "tom";
}
}


2.4: memory mechanism (spoken) class execution
2.4.1: jvm Are there any memory are stored
2.4.1.1: Virtual Machine stack (Stack Memory): Eight elementary data type object references + + returnAddress
features: 1. A method performed you need to perform in the stack is the method to execute need to stack. The method is called stack stack frame. -> Each method is executed creates a stack frame in the current stack.
2, each thread corresponds to a stack.
3, the default memory allocation 1M.
The main stack store some basic types of variables (int, short, long, byte , float, double, boolean, char) , and the object handle.
int a = 3; these variables called automatic, automatic variables are literals

2.4.1.2: stack: object instance (and an array of all the object instances to be allocated on the heap)
2.4.1.3: Method Area
2.4.1.3.1: method area: storing the virtual machine has been loaded class information, field information, the method of information, static variables, class classloader reference, the reference class class, code compiler like the immediate data.
2.4.1.3.2: runtime constant pool: a constant, the compiler generates a variety of characters and the amount of surface symbolic references, the part will be stored in the constant pool after the class is loaded.
NOTE: In the process area, each type corresponds to a constant pool to store all the constants used by the type.
Here for the time being only think + "" String constant pool value = final modified variables.
2.4.1.4: native method stacks: Use the native method for virtual machine services.
2.4.1.5: Program Counter: The current thread bytecode execution line number indicator.
2.5: overall code execution in memory
EG:
public class {F.

public static int age=12;

public String name="小明";

{
System.out.println ( "F. \ T anonymous block");
}
static {
System.out.println ( "F. \ T static block of code");
}
public static int Show () {
System.out.println ( "F \ t static method Show");
return 10;
}
public void say () {
int = A. 1;
String = C new new String ( "ABC");
System.out.println ( "F \ t conventional method") ;
}
public F. () {
System.out.println ( "F. \ T constructor");
}
public static void main (String [] args) {
F. F = null;
F = F. new new ();
f.say ( );
}
}

1, java F 1.1, class loading, obtain a binary byte stream by the full class name (package name + class name) F class will save byte stream to the region and in the method of generating a java heap Representative of this class java. lang.Class objects, as a method for accessing the data entry region
1.2, the preparation phase: a class variable in the process zone (static variable) memory
Note: instance variables will be assigned together with the java heap object when the object is instantiated in
2, public static int age = 12 2.1: preparation phase: in the area of open space required method int assigned to 0.
2.2: Initialization: age = 12; (the current class initialization will first initialize its parent -> static resource parent class will be loaded preferentially)
2.3: Initialization: static methods of the current block of code static class is loaded into memory area method.
2.4: class loading is complete. Starts executing the main method
2.5: main push Method

3, F f = null; 3.1 : in the stack frame of the main declare variables F f.

4, f = new F () ; 4.1: First, the heap create an instance of the class F,
4.2: initialize member variables in the heap
4.3: Method members in the heap initialization
4.4: reference instance of an object is assigned to the F

. 5 , f.say () 5.1: f by the memory address pointed to find the heap of say () method.
5.2: call say () method, say () method into the stack.

6, int a = 1; 6.1 : declare variable a: open space in the stack memory declared a, 1 is a basic data type (also holding the stack).
7, c = String
new new String ( "abc"); 7.1: String declared open memory c c a statement on the stack, new String ( "abc") is a reference data type in a heap create a String instance, due to the special value of String so we will first find out whether there is in the constant pool abc if there is a direct reference, not just the constant pool abc created and then assigned to objects in the heap.
8, the implementation of System
. 9, say () method executed, popping (the stack)
10, main method executed, popping (the stack), the end of the code execution


2.6: new object when a JVM things are done:
no class loader 1. Before
1. class loading, while static class initialization attribute (assigned default value)
2. Perform static code block
3. The allocation of memory space, while the non-static initialization attribute (assigned default values)
4. If there are simultaneously displayed assignment statement attribute, then assign the default value display cover
5. Run anonymous block
6. execution constructor
7. return memory address
static> non- static> constructor
Class F. {
public static String name = Show ();
static {
syso ( "FFFF")
}
{
syso ( "nnnffff")
}
public F. () {
syso ( "ffffggg")
}
public static String Show ( ) {
syso ( "Show F");
return "Tom"
}
}
Class F. the extends the Z {
public static String name = Show ();
static {
syso ( "FFFF")
}
{
syso("nnnffff")
}
public Z(){
syso("ffffggg")
}
public static String show(){
Syso("f show");
return "tom"
}
}

2. Before class has been loaded
1. Allocate memory space, while initializing non-static properties (assign default values)
2. If there is an assignment statement while displaying attribute, then display the default value assigned to cover
3. Perform anonymous block
4. perform builder
The return memory address



2.7: static import
2.7.1: Operating since: JDK5.0
2.7.2: Use: Import static java.lang.Math.random;
Import static package static methods Name / static attribute name;
2.7.3: action: in this class in can be used to import static properties or static method (using a direct method name, not the class name method name to call.);

 

Three: final qualifier
3.1: final sense: a "unchangeable" meaning it can be modified non-abstract class, non-abstract methods and member variables.

3.1.1: final modified Class: This class can not be inherited, without subclassing.
Final {} class Student
Final class String {
}

3.1.2: final modification methods: the method can not be overwritten.
Public Final void Show () {}
Object: getClass ();

3.1.3: final modifying variables: This variable is constant (constant pool), [value] can only be assigned once.
private int AGE = 10;

* 3.1.4: private final type of method is the default type ---> final can not be used to modify the construction method, because the "cover method" member method applies only to the concept of class, not for the class constructor covering relationship does not exist between the constructor methods and subclasses of the parent class. Thus a final modification constructor is meaningless. Private modifier with the parent process does not cover the method of the subclass.

 

3.2: final class
Review: inheritance is to break the package, a subclass of the parent class can access methods, and ways to be able to cover the method of modifying the implementation details.
3.2.1: sub-class is likely to incorrectly modify the implementation details of the parent class;
3.2.2: For security implementation details, the class does not allow any changes;
3.2.3: When you create an object model, this class is not convinced will again be extended;
issue: String class can not be inherited?

3.3: final method
3.3.1: In some cases, for security reasons, does not allow the parent child class overrides a method, this method can now be declared as final type.
3.3.2: final method can not be rewritten (s final modification of the method can be overridden).
eg: getClass Object class () method is the final type ---> call any class object getClass () method, you can see the original face of the object.

3.4: final variable
[3.4.1: final modification of non-static member variables must be assigned, and can only be assigned in one of three places, can not be changed after the assignment.
3.4.1.1: a statement that the assignment
3.4.1.2: within the anonymous block assignment
3.4.1.3: any class constructors assignment
3.4.2: final modified static static member variables
3.4.2.1: a statement that assignment
3.4.2.2: the static assignment block
final modification of the class can not be inherited
final modification method can not be rewritten]

3.4.3: final modified variables conclusion
3.4.3.1:final can modify any of the variables.
3.4.3.1:final modified variables must be assigned an initial value
after 3.4.3.3:final variable is modified with an assignment can not be changed

Four: abstract qualifier
4.1: Definition: abstract abstract (incomplete), and can be used to modify the class member method.

4.2: abstract modification classes: abstract class. } abstract class A {public
4.2.1: This class can not be instantiated (not create object). But there are constructors.
4.2.2: This class can have abstract methods, you do not. There may be a non-abstract methods.

4.2.3: abstract class can not abstract methods,
but the method comprising abstract class must be defined as an abstract class.
4.2.4: There is no abstract construction method, there is no abstract static methods;
4.2.5: abstract class can have member variables, member methods ......

Question 1: Are abstract class can be final modification?

4.3: abstract methods: abstract method modification.
4.3.1: There is no method body. To describe the function, but not implemented.
4.3.2: Normal methods are modified abstract abstract method is
an abstract method must not have a body of code
public abstract void Show ();
4.3.3: contains abstract methods must be abstract class
4.3.4: abstract class is born inheriting and
subclasses need to override the abstract method in the parent class,
subclass If you do not want to override the abstract methods of the parent class,
then subclass this method should be declared as abstract methods,
subclass must be declared abstract class.

Five: Interface (interface type)
5.1: Interface is to solve the problem of multiple inheritance in java.
EG:
5.2: abstract class abstract to the extreme is the interface. Another form of the interface is an abstract class.
5.3: When to use abstract class and when an interface with
5.3.1: Interface focuses on the method, if the abstract does not attribute to use interface.
5.3.2: abstract class focuses on the property, there is abstracted attributes you can use the abstract class
5.3.3: You can also use the place where you can use an abstract class can use the interface, use the interface: the abstract understanding when the object-oriented abstract class.
5.4: defining an interface: interface keyword used define the interface
eg: public interface A {} is the A interface.
5.5: What interface can write
5.5.1: member variables: The default type is a member variable public static final type must be explicitly initialized

5.5.2: a member method:
5.5.2.1: not have a method body .eg: public void Show ();
5.5.2.1: can do not have permission qualifier, the default is public abstract
question: can I use the final modification methods in the interface?

5.5.3: can not have any form of code block
5.5.4: constructor interface is not and can not be instantiated -> interface name is not new ();
5.6: multiple interface inheritance
5.6.1: a class inherits a smart parent class, but can implement multiple interfaces, separated by commas.
eg: class Student implements A, B {}
class implements a [plurality] interfaces, the interface methods will again not achieved, or compile error.
5.6.2: a plurality of interfaces can inherit an interface
eg: interface C extends A, B {}
Conclusion: java class no multiple inheritance, multiple interface inheritance.
5.7: Purpose Interface
5.7.1: Definition specification.
5.7.2: extension java only single inheritance between class and class, can be more like a software upgrade is not possible to achieve such change between classes and interfaces but can define multiple source. interfaces enhancements, abstract methods interface after replication.
* 5.7.3: Oriented Programming Interface:
advantages: the separation of interface from implementation, suitable for collaborative development team. In order to achieve the main loosely coupled system, to facilitate future upgrades, extended.
Cons: Design is difficult, at the time did not write achieve, you have to think a good interface, the interface for a change, all hell broke loose, this is difficult to achieve than the so-called design.
5.7.4: General Interface using an interface with the polymorphic A a = new interface type B ();
5.7.5: Interface I generally named beginning interface implementation class, create packages in the same directory impl interface, with Interface to store all the packets to change, add name suffix impl
eg: IBookDao the interface
implementation class impl / BookDaoImpl
5.8: abstract classes and interfaces Comparative
1. similarities:
A not be instantiated;.
B can contain abstract methods;.
2. Different points;
A abstract class provides default may be implemented as part of the method, in order to avoid sub. class repeated to achieve them, to improve the reusability of code, and the interface can only contain abstract methods;
. a class B inherits only one direct parent, the parent may be the abstract class; but can be a multi-class interfaces, this is the advantage of the interface.

Guess you like

Origin www.cnblogs.com/zjw19971001/p/11305057.html