Object Oriented (I)

Variable number parameter method

Allows you to define parameters that can match multiple arguments. The number of parameters passed in the call can be 0,1,2,3…. Methods with variable-number parameters cannot be overloaded with functions with the same name as array parameters:

public void show(String ... strs){ } show("123"); show("123","234"); // Both calls can match morphable parameter functionsCopy the code
Permission modifier

The constructor

When a constructor is explicitly defined in a class, you need to manually create the default constructor

JavaBean

A javaBean is a reusable component written in the Java language. Javabeans are Java classes that meet the following criteria:

  • Classes are public
  • There is a common constructor with no arguments
  • There are properties and corresponding get and set methods
This keyword

Used to get the current object. Properties or methods of the current object can be called as this. properties or methods. At the same time, one constructor can be called to another constructor as this(argument), executing the constructor’s code. This () must be written on the first line of the current constructor

The MVC pattern

inheritance
class Student extends Person{
}
Copy the code

Java does not allow multiple inheritance

Object class

If a class’s parent is not explicitly declared, it inherits from the Object class. All classes in Java inherit directly or indirectly from the Object class and can use the functions of the Object class. Object equals ==

  • When applied to primitive data types, values are compared and true is returned if they are equal; When applied to a reference data type, == compares the address values of two objects to determine whether they refer to the same entity
  • Equals is a method that can only be used on reference data types. It works by theta = theta. But String, Date, File, etc. overwrite equals to achieve the desired goal. Custom classes override equals themselves.
rewrite

In a subclass, you can modify the methods in the parent class as needed, and the subclass overrides the methods in the parent class as the program executes.The difference between overloading and overwriting:

  • Differences in definitions
  • Overloading is statically bound, and at compile time the compiler can decide which function to call; Rewritten to dynamic binding, only the runtime can determine whether a subclass or superclass method is called
The super keyword

When a subclass overrides a method of the parent class, you can use super to call the unoverridden method of the parent class in the subclass: super.xxx (). The constructor of the parent class can be called in a subclass constructor using super(parameter) and must be declared on the first line of the subclass constructor. Also, if the subclass constructor does not show a call to this or super, the superclass constructor is called by default using super() (the superclass must have an empty parameter constructor).

polymorphism

A reference to a parent class refers to an object of a child class and can only call a method declared in the parent class. If the method has been overridden by a child class, the overridden method will be called. Example: Person p=new Man() virtual method call:Conditions for using polymorphisms:Class inheritance and method rewritingThe utility of polymorphism: The use of superclass objects to accommodate various subclass objects facilitates uniform processing and reduces code redundancy. For example, man and woman inherit from Person. If you want to write a method that calls a method in man or woman, then if you don’t use polymorphism, you need to write two methods with different parameters. But if the parameter is defined as a Person object, the overridden method can be called directly in the function body and only one method needs to be written, reducing code redundancy.

The instanceof keyword

A instanceof a: determines whether a is an instanceof a

A wrapper class

A wrapper class for the eight basic data types, making the basic data types have class characteristics

Conversions between primitive data types, wrapper classes, and Strings

Basic data type –> Wrapper class: Call the wrapper class constructor

Integer i=new Integer("123");
Integer i=10;// Automatic boxing
Copy the code

Wrapper class –> Base data type: call xxxValue of the wrapper class

Integer i = new Integer(a);
int b=i.intValue();
/ / or
int c=i;// Automatic unpacking
Copy the code

Basic data types, wrapper classes –>String: add “” or call string.valueof ()

int a=10;
String s=a+"";
/ / or
String s1=String.valueOf(a);
Copy the code

String– > Basic data type, wrapper class: call parseXXX() of the wrapper class

String s="100";
int a=Integer.parseInt(s);
Copy the code