Java object-oriented notes (continuous update to be completed)

Class and object

An object is an instance of a class. An
object is a specific type of data.
Attributes: the various static characteristic
classes of the object .
From the perspective of data structure, a class creates a new data type by itself.
Classes are also called "custom types".
The same name is not allowed in a java program.

//类名必须和文件名相同
public class Merchandise {
    
    
    String name;
    String id;
    int count;
    double price;
} 

Use member variables

publi static void main(String[] args){
    
    
Merchandise m1=new Merchandise();
m1.name="茉莉花茶";
m1.id="1115";
m1.count=100;
m1.price=99.9;
}

Dot operator
Dot operator is used to access/manipulate the attributes of the previous entity, similar to "the"
Merchandise.name can be read as the name of Merchandise.

Reference (reference) data types The data types in
java are divided into basic data types and reference data types

m1是一个Merchandise类型的引用,只能指向Merchandise类型的实例;
引用数据类型变量包含两部分信息:类型和实例。
每一个引用数据类型的变量(简称引用),都是指向某个类(class/自定义类型)
的一个实例/对象(instance/object)。不同类型的引用在java的世界但是引用。
引用的类型信息在创建时就已经确定,可以通过给引用赋值,让其指向不同的实例。

Merchandise m1;
m1=new Merchandise();

-Declare the object Merchandise m1 -Instantiate
the object m1=new Merchandise();
Use the new operator to create an instance of a certain class. When the java program is running, all these created instances are stored by java in a place called a heap, which is similar to an advertising board.
To create an instance is to make a book based on the definition of the class and the "paper" needed for the point, and hang it on the bulletin board. The instance itself can be regarded as a small book.
The information stored in the reference is equivalent to the address of the bulletin board where a certain book is located.
The relationship between a class and an object A
class is a template of an object, and an object is an instance of
a class. There can only be one class with the same class name in a java program, that is, the type will not have the same name.
A class can have many objects and
an object can only be based on one Class to create

The relationship between references and classes and objects.
References must be, and can only be, references to one class. References
can only point to objects of the class to which they belong
. References of the same type can be assigned values.
Only references to one object can be used to manipulate one Object, such as accessing a member variable

Packages and access modifiers
in JAVA In order to avoid confusion of classes together, class classes can be placed in folders. At this time, you need to use the package statement to tell java which package the class is in. The package statement must correspond exactly to the directory of the source file, and the case must be the same.
package is read as a package. Generally speaking, the class will be in the package, and not directly in the root directory.
Different packages can have classes with the same name.
A class can only have one package statement. If there is a package statement, it must be the first line of valid code of the class.

import import package
When using a class in another package, you need to bring the package name.
It is very cumbersome to bring the package name every time you use it. You can use the import statement on the used class to solve the problem once, and then you can use the class directly. It's like the Scanner class.
There can be multiple import statements.
If you need to import many classes in a package, you can use the * wildcard.
The order of loading classes has nothing to do with the position of the import statement

   import包名.*; 只能访问指定包名下的类,无法访问子包下的类。

Public
attributes modified by public can be accessed by classes in any package.
Attributes without access modifiers are called default access modifiers, which can be used by other classes in this package and their own objects.
Access modifier is a modifier that restricts or allows attribute access.

The fully qualified name of the
class package name + class name = the fully qualified name of the class. It can also be referred to as the class of the whole people.
The fully qualified name cannot be repeated in the same java program.

Function overloading
A class can have multiple constructors, as long as their parameter lists are different.
Given different parameter values ​​when creating an object, different constructors will be called automatically.
You can also call other constructors through this().
Functions with the same name but different parameter lists in a class constitute an overload relationship.

Single Responsibility Principle
There is one and only one cause of functional changes

This
accesses the member methods of the
current class. Accesses the member properties of the
current class. Accesses the construction method of the current class. It
cannot be used in static methods.

Encapsulation

Hide some information of the class inside the class, and do not allow external programs to directly access it.
Use the methods provided by this class to realize the operation and access to the hidden information. The information of the
hidden object is
reserved. The access interface is reserved.

Features:
1. You can only access the data through the specified method
. 2. Hide the instance details of the class, which is convenient for modification and implementation

Step
1. Modify the attribute visibility-private limit can only be accessed within the current class
2. Create get/set method
Add the attribute limit in the get/set method

static keyword

Static static members
share the same storage space
1. Class object sharing.
2. Generated when the class is loaded, and released when the class is destroyed, with a long life cycle.
Static member access method:
1. Object. Member
2. Class. Member

static+methods. Class methods, static methods, and
static methods cannot directly access non-static members in the same class, but can only directly call static members in the same class.
Non-static members can only be accessed through the object. method after the object is instantiated.

In the method, the object is used as a parameter, and its reference is passed, and all information of the object can be obtained by reference.

inherit

A relationship between classes.
Use the definition of an existing class as a basis to create a new class.
The definition of a new class can add new data or new functions, and can also use the functions of the parent class, but it cannot selectively inherit the parent class.

extends

class Dog extends Animal{
    
    
}
class Cat extends Animal{
    
    
}

The construction of the parent class is not allowed to be inherited or rewritten, but it will affect the
method rewriting of the subclass object’s instantiation process .
Syntax rule:
return value type The method name and parameter list
of the subclasses with continuation relationship are the
same (parameters Type, order, number,), the return value of the method can be allowed to be a subclass type.
Access modifier, the access range needs to be greater than or equal to the access range of the parent class. It
has nothing to do with the parameter name of the method.

Access modifier
private: only allow access in this class
public: allow access at any location
protected: allow in the current class, the same package subclass/non-subclass, cross-package subclass call, cross-package non-subclass not allowed,
default: It is allowed to call in the current class, the same package subclass/non-subclass; cross-package subclass/non-subclass is not allowed to call

super
super on behalf of the parent class reference
access to the parent class member method super.print();
access to the parent class attribute super.name;
access to the parent class construction method super();
subclass construction defaults to call the parent class no-argument construction method
through super() Calling other constructors allowed to be accessed by the parent class
super() must be placed in the first line of the valid code of the subclass constructor

After the initialization sequence inherit
the parent class static member
sub-static member
parent object constructor
subclass object constructor

Object class
Object class is the parent class of all classes. If
a class does not use the extends keyword to clearly identify the inheritance relationship, it will inherit the Object class (including arrays) by default
. Each class in java can use the methods defined in Object.

final
final class: not allowed to be inherited.
Final method: This method cannot be overridden by subclasses, but it can be inherited and used normally by subclasses.
Final method class local variables: mainly assign values ​​before they are specifically used. Once assigned, they are not allowed to be modified.
Member variables in the class: Assignment process: 1. Define direct initialization 2. Construct method 3. Construct code block

Modified variable means that it is not allowed to modify
the variable of the basic data type-it cannot be changed after the initial assignment
. The variable of the reference type-it cannot point to another object after initialization, but the content of the object is variable

Can be used with static Use
final modification can improve performance, but it will reduce scalability.

Annotations
can be declared in front of packages, classes, attributes, methods, local variables, method parameters, etc., to explain, annotate, and

Design Patterns

Solutions to general problems faced by software developers in the software development process.

Singleton Mode
Purpose: Make an object of a class the only instance in the system.
Definition: A class has one and only one instance, and self-instantiate provides the entire system.
Points
1. A class can only have one instance
2. You must create an instance yourself
3. You must provide this instance to the entire system yourself.
Implementation
1. Only provide private construction methods
2. Contain a static private object of this class
3. Provide a static public method for creating and obtaining static private objects

Advantages
1. There is only one object in memory, saving memory space
2. Avoid frequent creation and destruction of objects, improve performance
3. Avoid multiple occupation of shared resources
Disadvantages
1. Expansion is more difficult
2. If the instantiated object is not conducive for a long time , The system will default to garbage for recycling, resulting in loss of object state.
Scenario
1. Too many resources are occupied when creating an object, but at the same time this type of object needs to be used
2. Uniform read and write requirements for resources in the system, such as reading and writing configuration information
3. When multiple instances exist, it may cause program logic errors, such as number generator

Polymorphism

Different types of objects are allowed to respond differently to the same message.
Compile-time polymorphism
Design-time polymorphism
Method overload
Run-time polymorphism The
program dynamically decides which method to call when the program is running
Necessary conditions
satisfy inheritance relationship
Parent class reference points to child class object

Up-casting The
parent class reference points to an instance of the child class. You can call the method of the child class to override the parent class and the methods derived from the parent class. You cannot call the unique method of the child class. The
small class is transformed into a large class.
Note: The static method in the parent class cannot be subclassed. Class rewriting, so after up-casting, you can only call the original static method of the parent class

Animal one=new Animal();
        Animal two=new Cat();
        Animal three=new Dog();

Down-casting The
subclass reference points to the parent class object, here must be forced to
convert , you can call the unique method of the subclass must meet the conversion conditions to be forced to convert
instanceof operator: return true/false

     
        if(two instanceof Cat){
        Cat temp=(Cat)two;

The abstract class abstract is
not allowed to be instantiated, and it can point to subclass instances through up-casting

Application scenario: A
parent class only knows what methods its subclasses should contain, but it is impossible to know exactly how these subclasses implement these methods.
Abstract method: It is not allowed to include method body; the subclass needs to override the abstract method of the parent class, otherwise, the subclass is also an abstract class.
Static final private cannot coexist with abstract.
Rules
1. Abstract defines abstract class
2. Abstract class cannot be directly instantiated It can only be inherited. Object instances can be completed through upward transformation.
3. Abstract defines abstract methods without specific implementation
. 4. Classes containing abstract methods are abstract classes.
5. Abstract classes can have no abstract methods.

Interface The
interface defines the specifications that a certain batch of classes need to comply with. The
interface does not care about the internal data of these classes or the implementation details of the methods in these classes. It only stipulates that certain methods must be provided in these classes.

The abstract method in the interface does not need to write the abstract keyword. The
access modifier defaults to public.
When a class implements an interface, you need to implement all the abstract methods in the interface, otherwise you need to set the class as an abstract class.

Interface can also implement inheritance, and can inherit multiple parent interfaces

Inner class
Member inner class
1. When the inner class is used externally, it cannot be instantiated directly, and the instantiation can be completed by the information of the external class.
2. The access modifier of the inner class can be arbitrary, but the access scope will be affected
. 3. Internal The class can directly access the members of the external class. If the attribute with the same name appears, the priority is given to the internal class.
4. You can use the external class.this. member to access the information of the same name in the external class.
5. External class access=internal class information, Need to pass the internal class instance, can not directly access
6. After the internal class is compiled. Class file naming: external class $ internal class.class

Static inner class
1. In the static inner class, you can only directly access the static members of the outer class. If you need to call the non-static member, you can use the object instance
2. When the static inner class object instance, you can not rely on the outer class object
3. Yes Access the static members in the inner class through the external class, internal class, and static members
. 4. When the internal class attribute has the same name as the external class attribute, the member in the internal class is directly called by default.
If you need to access the static attribute in the external class, You can use external classes. Attributes.
If you need to access non-static properties in external classes, you can use new external classes (). Attributes.

Method inner class/local inner class
1. Defined in the method, and the scope is also in the method.
2. Same as the rules for the use of internal members of the method, public, private, protected, static cannot be added before the class
3. Static members cannot be included in the class
4. The class can contain final and abstract modified members

Anonymous inner class
1. Anonymous inner class has no type name, instance object name
2. Compiled file name: external class $ math.class
3. Cannot use private, public, protected, abstract, static modification
4. Cannot write construction method, You can add structure to the code block
5. Static members cannot appear
6. Anonymous inner classes can implement interfaces or inherit parent classes, but they cannot have both

Guess you like

Origin blog.csdn.net/weixin_42403632/article/details/105849644