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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.