Java object-oriented programming basics summary





  • Java is an object-oriented programming language. Object-oriented programming, English is Object-Oriented Programming, OOP for short.

What is object-oriented programming?

Object Oriented Programming (OOP) is a computer programming framework. A fundamental principle of OOP is that computer programs are composed of individual units, or objects, that can function as subroutines. OOP achieves three major goals of software engineering: reusability, flexibility, and extensibility. OOP = Object + Class + Inheritance + Polymorphism + Message, where the core concepts are classes and objects.



Guyu

Guyu
Guyu







Object Oriented Basics



① Method

The syntax for defining a method:

Modifier method return type method name (method parameter list) { several method statements; return method return value; }


The return value of the method is realized through the return statement. If there is no return value, the return type is set to Void, and return can be omitted.
A complete method usually includes method name, method body, method parameters and method return value type.



② Construction method

When creating an instance, the instance is actually initialized through the constructor.

Because constructors are so special, the name of the constructor is the name of the class. There is no limit to the parameters of the construction method, and any statement can also be written inside the method. However, compared with ordinary methods, the construction method has no return value (no void), and the new operator must be used to call the construction method.

A constructor can call other constructors to facilitate code reuse. The syntax for calling other constructors is this(…);



③ Method overloading

Methods with the same name but different parameters are called method overloading.
The return type of method overloading is usually the same;



④ Inheritance

Java uses the extends keyword to implement inheritance;
a characteristic of inheritance is that subclasses cannot access the private fields or private methods of the parent class.

Inheritance is a powerful code reuse method for object-oriented programming;
Java only allows single inheritance, and the final root class of all classes is Object;
protected allows subclasses to access the fields and methods of the parent class;

In inheritance, if a subclass defines a method with the same signature as the parent class method, it is called Override.
If the method signatures are the same and the return value is the same, it is Override.



⑤ Polymorphism

Polymorphism means: For a method call of a certain type, the actual execution method depends on the actual type of method at runtime;
Java's method call always acts on the actual type of the object at runtime, this behavior is called polymorphism.



⑥ abstract class

If the method of the parent class itself does not need to implement any functions, just to define the method signature, the purpose is to allow the subclass to override it, then the method of the parent class can be declared as an abstract method (abstract).
If a class defines a method, but does not specifically execute the code, this method is an abstract method, and the abstract method is modified with abstract. Because abstract methods cannot be executed, this class must also be declared as an abstract class.

A class modified with abstract is an abstract class. We cannot instantiate an abstract class.



⑦ interface

The so-called interface (interface): It is a pure abstract interface that is more abstract than an abstract class, because it cannot even have fields. Because all methods defined by the interface are public abstract by default, so these two modifiers do not need to be written out (the effect is the same whether they are written or not).
When a specific class implements an interface, the implements keyword needs to be used.

In Java, a class can only inherit from another class, not from multiple classes. However, a class can implement multiple interfaces.

Java's interface specifically refers to the definition of interface, which means an interface type and a set of method signatures, while programming interface generally refers to interface specifications, such as method signatures, data formats, network protocols, etc.
An interface can inherit from another interface. interface inherits from interface and uses extends, which is equivalent to extending the method of the interface.



⑧ Static fields and methods

Fields modified with static are called static fields. Where there are static fields, there are static methods. A method decorated with static is called a static method.
Because the static method belongs to the class and not to the instance, therefore, inside the static method, neither the this variable nor the instance field can be accessed, it can only access the static field.
Static methods are also often used in helper methods. Notice that main(), the entry point of the Java program, is also a static method.



⑨ bag

In Java, we use packages to resolve name conflicts.
Java defines a namespace called a package: package. A class always belongs to a certain package, the class name (such as Person) is just an abbreviation, the real full class name is packagename.classname.
When the Java virtual machine is executed, the JVM only looks at the full class name, so as long as the package name is different, the class is different. Packages can be multi-layered, separated by . For example: java.util.

Classes in the same package can access package-scoped fields and methods. Fields and methods not modified by public, protected, or private are package scopes.



⑩ scope

public:

Classes and interfaces defined as public can be accessed by any other class;

private:

Fields and methods defined as private cannot be accessed by other classes;

protected:

Fields and methods defined as protected can be accessed by subclasses, as well as subclasses of subclasses; protected acts on inheritance relationships.

package:

Package scope refers to a class that allows access to the same package without public, private modified class, and without public, protected, private modified fields and methods.
As long as they are in the same package, they can access the class, field and method of the package permission.



⑪ Inner class

Organize different classes under different packages. For the classes under a package, they are at the same level and have no parent-child relationship; there is also
a class that is defined inside another class, so it is called an inner class ( Nested Class)



⑫ classpath and jar

classpath is an environment variable used by the JVM to instruct the JVM how to search for classes.
The JVM determines the path and order of searching classes through the environment variable classpath;

The jar package is equivalent to a directory and can contain many .class files for easy download and use.



⑬ template

The purpose of modules introduced by Java 9 is to manage dependencies;
JRE can be packaged on demand by using modules;
access rights to classes are further restricted by using modules.



--------------------------



Java core classes



① String and encoding

String

Is a reference type, which itself is also a Class. One of its important features is that the string is immutable;
the Java string String is an immutable object;
the string operation does not change the content of the original string, but returns a new string;
common string operations: extract substring, search, replace , case conversion, etc.;

In early computer systems, in order to encode characters, the American National Standards Institute (American National Standard Institute: ANSI) developed a set of encodings for English letters, numbers and common symbols, which occupy one byte and range from 0 to 127 , the highest bit is always 0, which is called ASCII encoding.
For example, the character 'A' is coded 0x41, which is 65 decimal; the character '1' is coded 0x31, which is 49 decimal.

Java uses Unicode encoding to represent String and char;
conversion encoding is to convert String and byte[], and the encoding needs to be specified;
when converting to byte[], UTF-8 encoding is always given priority.



② StringBuilder

It is a mutable object, which is used to concatenate strings efficiently;
in order to concatenate strings efficiently, the Java standard library provides StringBuilder, which is a mutable object that can pre-allocate buffers. In this way, when adding characters to StringBuilder, no A new temporary object is created.
StringBuilder can support chain operations, and the key to realize chain operations is to return the instance itself;
StringBuffer is a thread-safe version of StringBuilder, which is rarely used now.



③ StringJoiner

It is more convenient to use StringJoiner or String.join() when joining string arrays with a specified delimiter;
when using StringJoiner to join strings, an additional "beginning" and "end" can be added.



④ Packaging type

To turn the int basic type into a reference type, we can define an Integer class, which only contains an instance field int, so that the Integer class can be regarded as a wrapper class (Wrapper Class) for int.



⑤ JavaBean

If the reading and writing method conforms to the following naming convention (get/set method), then this class is called JavaBean.

// Read method:
public Type getXyz() { }
// Write method:
public void setXyz(Type value) { }

The main function of JavaBean is to transfer data, that is, combine a set of data into a JavaBean for easy transmission.



⑥ enumeration class

In order for the compiler to automatically check that a certain value is in the set of enumerations, and enumerations for different purposes need to be marked with different types and cannot be mixed, we can use enum to define enumeration classes.

Java uses enum to define the enumeration type, which is compiled by the compiler into final class Xxx extends Enum { ... };
get the string defined by the constant through name(), be careful not to use toString();
return the order of the constant definition through ordinal() (No real meaning);
You can write constructors, fields and methods for enum.
The constructor of enum should be declared as private, and the fields are strongly recommended to be declared as final;
enum is suitable for use in switch statements.



⑦ Record class

What is defined using Record is an invariant class;
Compact Constructor can be written to verify parameters;
static methods can be defined.



⑧ BigInteger

java.math.BigInteger is used to represent integers of any size.
BigInteger is an immutable class and inherits from Number;
methods such as longValueExact() can be used to ensure accurate results when converting BigInteger to basic types.



⑨ BigDecimal

Like BigInteger, BigDecimal can represent a floating-point number of any size with full precision.
BigDecimal is used to represent precise decimals and is often used in financial calculations;
to compare whether the values ​​of BigDecimal are equal, you must use compareTo() instead of equals().



⑩ Common tools

Math

The Math class is used for mathematical calculations, and it provides a large number of static methods to facilitate our implementation of mathematical calculations.

random

Random is used to create pseudo-random numbers. The so-called pseudo-random number means that as long as an initial seed is given, the sequence of random numbers generated is exactly the same.

SecureRandom

SecureRandom is used to create secure random numbers.









Note:
Likes, comments, and reprints are welcome. Please give the link to the original text in an obvious place on the article page
. Those who know, thank you for reading my article in the vast crowd.
Where is the signature without personality!
For details, please follow me
and continue to update

Scan to have a surprise!
© 2021 04 - Guyu.com | 【Copyright All Rights Reserved】

Guess you like

Origin blog.csdn.net/weixin_49770443/article/details/115199723