Reusing code is one of Java's many compelling features. But for a language to be revolutionary, it's not enough to just be able to copy code and change it. It must be able to do more.
Composition syntax
Just place the object reference in the new class.
Initialize the reference Position
Where objects are defined, they can always be initialized before the constructor is called.
In the constructor of a class
Just before the objects are used, --- Lazy initialization
Instance initialization
Inheritance syntax
Unless it is explicitly stated that you want to inherit from other classes, you are implicitly inheriting from Java's standard root class Object is inherited.
Initialize base class
Proxy
The Java language does not directly support proxies. Many development tools do. .
Choose between collections and inheritance
Both composition and inheritance allow placing sub-objects in a new class. Composition is done explicitly, inheritance is Do it implicitly.
protected keyword
is private as far as the class user is concerned, for any exported class that inherits this class or any other class in the same class. For the classes in the package, he is accessible.
Upward transformation
The exported class you exported is converted to accumulation and moves upward on the inheritance graph, becoming Upcasting.
Upcasting is a conversion from a more specialized type to a more general type, so it is always safe.
final keyword
Cannot be changed
final data
A domain that is both static and final only occupies a storage space that cannot be changed.
For basic types, final makes the value constant. For object references, final makes the reference constant.
Once a reference is initialized to point to an object, it cannot be changed to point to another object. However, the object itself can be modified.
Java does not provide a way to make any object immutable.
This restriction also applies to arrays, which are also objects.
By convention, fields that are both static and final will be capitalized, with underscores separating the words.
Blank final
The so-called blank final means that it is declared A field that is final but has no initial value given.
In any case, the compiler ensures that a blank final must be initialized before use.
final parameters
Allows the parameter to be declared final in the parameter list. This means that you cannot change the object pointed to by the parameter reference in the method.
final method
1. Lock the method to prevent any inherited class from modifying its meaning.
2. Efficiency, in early implementation, if you specify a method as final, you agree that the compiler will target the method. All calls to methods are converted to inline calls.
final and private keywords
All private methods in a class are implicitly designated as final.
Since the private method cannot be used, it cannot be overridden.
You can add the final modifier to the private method, but it does not add any additional meaning to the method.
"Override" will only appear when a method is part of the interface of the base class.
Must upcast an object to her basic type and call the same method.
final class
is prohibited from being inherited and has no subclasses.
All methods in all final classes are implicitly designated as final and cannot be overridden.
Initialization and class loading
class Insect { private int i = 9 ; protected int j ; Insect(){ System.out.println("Insect constructor."); System.out.println("i= "+i+", j="+j); j = 39 ; } static int printInit(String s){ System.out.println(s); return 47 ; } } public class Beetle extends Insect { private int k = printInit("Beetle.k initialized . "); public Beetle(){ System.out.println("k = "+ k); System.out.println("j = " + j); } private static int x2 = printInit("static Beetle.x2 initialized ") ; public static void main(String[] args) { System.out.println("Beetle constructor."); Beetle b = new Beetle(); } } 输出: static Beetle.x2 initialized Beetle constructor. Insect constructor. i= 9, j=0 Beetle.k initialized . k = 47 j = 39
The above is the detailed content of JAVA Programming Thought Notes: Reuse Classes. For more information, please follow other related articles on the PHP Chinese website!