The inheritance part of Java key knowledge points

Summary: Android Xiaobai's Growth Path_Knowledge System Summary【Continuously updated...】

Classes, Parents and Subclasses

  • super is different from this reference, super is not a reference to an object, super cannot be assigned to another object variable, it is just a special keyword instructing the compiler to call the parent class method

  • The keyword this serves two purposes:

    • reference implicit parameter
    • call other constructors of the class

    super also has two purposes:

    • Call the method of the parent class
    • call the constructor of the parent class
  • Polymorphism: The parent class reference points to the subclass object, which is divided into runtime polymorphism and compile-time polymorphism. Runtime polymorphism is realized through dynamic binding, such as rewriting the parent class method, and deciding which subclass to call at runtime method after writing. Compile-time polymorphism is achieved through static binding, such as method overloading, and the method with the same name and different parameters that needs to be called has been selected at compile time

    • Static binding: private method, static method, final method or constructor, the compiler can know exactly which method to call
    • Dynamic binding: depends on the actual type of the implicit parameter when calling, dynamically selected at runtime
  • Inlining: If a method is not overridden and is short, the compiler will optimize it. This process is called inlining. For example, the inline call a.getName() will be replaced by accessing the a,.name field

  • Mandatory type conversion: Different data types can be converted to each other. Assigning a reference of a parent class to a subclass variable must also perform type conversion, but if the conversion is unsuccessful, an exception will be reported, so it is generally necessary to add a judgment. In this a instantceof Astatement , if a is null, no exception will be generated, only false will be returned

  • Abstract class: A class containing one or more abstract methods must itself be declared abstract, and an abstract class can also contain concrete data and methods

  • Enumeration class: enumeration class enumis used to declare and define, and cannot be inherited. All members in it are equivalent to instances of the current class

Object class

  • Object class: the base class of all classes, each class is extended from it, if the parent class is not clearly indicated, then the parent class defaults to the Object class
  • equals method: used to detect whether an object is equal to another object, in the Object class, this method determines whether two objects have the same reference
  • hashCode method: each object has a default hash code, its value is the storage address of the object, and should return an integer value, null returns 0
  • If you override the equals method, you must redefine the hashCode method. If a.equals(b) returns true, then a.hashCode() and b.hashCode() must have the same value

Automatic boxing and unboxing

  • Object wrappers: All primitive types have a corresponding class, such as Integer. Long, Float. Double, Short, Byte, Character, Boolean, the object wrapper class is immutable, so it is not possible to define subclasses, nor to change the value in it
  • Autoboxing: Automatically convert the basic type into a wrapper type, for example, it Integer a = 99;is actually executed Integer a = Integer.valueOf(99);. It should be noted that Integer.valueOfthe method will first judge the size of the parameter. If it is between [-128~127], it will return the created one. Integer objects, these objects are stored in an SMALL_VALUESarray called. If it exceeds the range, an Integer object will be created, which leads to two situations: the same object is returned within the range, and different objects are returned outside the range, and there will be differences when judging equality
  • Automatic unboxing: Automatically convert wrapper types into primitive data types, such asInteger a = 99; int b = a;
  • equals(Object o): The parameter type in the original equals method is an encapsulation type, and the parameter type (a) passed in is a basic data type, which will be automatically boxed, otherwise, it will be unboxed
  • When two different types are ==compared, the wrapper class needs to be unboxed, and when the same type is compared ==, it will be automatically unboxed
  • Variable parameters: void a(Object... args){}, the ellipsis here indicates that this method can receive any number of objects, this parameter will be bound to the array, and will be automatically boxed when necessary, for example, it becomes like this: , here you can also directly pass an a(new Object[]{new Integer(n),"name"})array as Arguments for variadic methods

reflection

  • During the running of the program, the Java runtime system always maintains a type identifier called runtime for all objects. This information tracks the class to which each object belongs. The virtual machine uses the runtime type information to select the corresponding method to execute. This information can also be accessed through a dedicated Java class. The class that holds this information is called Class. This name is confusing. The methods Objectin the class getClass()will return an Classinstance of the type

  • A Class object actually represents a type, and this type is not necessarily a class, for example, int is not a class, but int,.class is an object of Class type

  • java.lang.reflectThere are three classes in the package Field, Methodand Constructorthe fields, methods and constructors used to describe the class

  • Field[] getFields(): Returns an array containing Filed objects that record the public fields of this class or its superclasses

  • Field getField(String name): Returns the public domain of the specified name

  • Field[] getDeclaredFields(): It also returns an array containing Filed objects, but records all fields. If there is no field in the class, or the Class object describes a basic type or an array type, these methods will return an array with a length of 0

  • Field getDeclaredField(): returns the domain with the specified name

  • Method[] getMethods(): Returns an array containing Method objects, and returns all public methods, including public methods inherited from superclasses

  • Method[] getDeclaredMethods(): Returns all methods of this class or interface, but does not include inherited methods

  • Constructor[] getConstructors(): Returns all public constructors of the class containing the Class object

  • Constructor[] getDeclaredConstructors(): Returns all constructors of the class that contains the Class object

  • void setAccessible(boolean flag): Sets the accessible flag for reflective objects

  • Object get(Object obj): Returns the field value represented by the Field object in the obj object

  • void set(Object obj, Object newValue): Set the field represented by the Filed object in the Obj object with a new value

  • An example of using reflection to modify field values:

    Class cl = a.getClass();
    Object obj=cl.newInstance()
    Field f = cl.getDeclaredFiled(name);
    f.setAccessible(true);
    f.set(obj,"aaa");
    

Guess you like

Origin blog.csdn.net/Nbin_Newby/article/details/120554469