What is object oriented
Object oriented refers to defining a class by ourselves. There are some methods in the class. We all instantiate the object of the class and then call the method of the object.
In terms of image, we regard "oriented" as "use" and "object" as "tool", so object-oriented programming is to use tool programming.
Class structure
There are five structures in the class:
attribute
The variables or constants we define are the properties of this class
public class User { private String name; private String password; }
method
A method is a collection of code blocks designed to implement a specific function. The grammatical functions of the method mainly include the following two: organize the code according to the function, so that the structure of the code is relatively clear, easy to read and modify, that is, the maintainability of the program is strong.
public class User { private String name; private String password; public String toString() { return "User{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
constructor
Constructor is a special type of method, which is responsible for the initialization of member variables (domains) in a class. The constructor is used to perform initialization when creating an object. When creating an object, the system will initialize the instance of the object by default.
public class User { private String name; private String password; public User() { } public User(String name,String password) { this.name = name; this.password = password; } public String toString() { return "User{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
Code block
The so-called code block is to encapsulate multiple lines of code with braces ({}) to form an independent data body, which is used to implement a specific algorithm Generally speaking, a code block cannot be run alone. It must have a running body. Code blocks can be created selectively according to the role of classes.
There are four types of code blocks: ordinary code block, static code block, synchronous code block and construction code block. Let's take ordinary code block as an example:
public class User { private String name; private String password; { name = "zhangsan"; password = "123" ; } public User() { } public void setName(String name) { this.name = name; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", password='" + password + '\'' + '}'; } }
Inner class
If a Class A does not have enough methods in this class A to complete its work, it needs to be completed by another class B, but class B cannot function in other classes. If we define this class externally, it will lead to bloated files. Therefore, we write the special class of a class as an internal class. The most classic example is Map (no code here)
Characteristics of class
Encapsulation
Encapsulation is a kind of hiding technology. Encapsulation combines all the components of an object. Encapsulation defines how the program references the data of the object. Encapsulation actually uses methods to hide the data of the class and control the degree of users' modification of the class and access to the data. Encapsulated keywords include public, produced, private and default
public class Animal { public String name; //open protected String password; //under protection private String phone; //private String gender; //default }
inherit
Inheritance is a mechanism for an object to obtain all the properties and behaviors of its parent object. It is an important part of object-oriented programming system (OOP). The idea of inheritance in Java is to create new classes based on existing classes. Inheriting from an existing class, you can reuse the methods and fields of the parent class. In addition, you can add new methods and fields to the current class.
public class Dog extends Animal{ public void show(){ System.out.println("Dog can dance"); } }
polymorphic
The specific type pointed to by the reference variable defined in the program and the method call issued through the reference variable are not determined during programming, but only during the program running, that is, the instance object of which class a reference variable will point to and the method call issued by the reference variable must be determined during the program running Because the specific class is determined only when the program is running, so that the reference variable can be bound to various class implementations without modifying the source program code, resulting in the change of the specific method called by the reference, that is, the specific code bound when the program is running can be changed without modifying the program code, so that the program can select multiple running states, which is polymorphism.
public class DogTest { public static void main(String[] args) { Animal cat = new Cat(); } }
Necessary conditions for polymorphic use
- Inherit or implement
- Override parent class or abstract method
- The object of the parent class refers to the reference of the child class
Bean class
JavaBeans is a special class in Java, which can encapsulate multiple objects into one object (bean). It is characterized by serializability, providing parameterless constructors, and providing getter and setter methods to access the properties of objects. "Bean" in the name is a common name for reusable software components for Java.
public class Person { private String name; private String password; public Person() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Order of variable initialization
Static initialization -- > code block initialization -- > constructor initialization -- > dynamic initialization