What is object-oriented?
Object object, Oriendted... oriented, Programming programming
Object-oriented means using objects for programming, abbreviated as OOP.
Comparison between SP and OOP
Three major principles of object-oriented encapsulation and inheritance polymorphism
Object
Objects are the core part of object-oriented programming, they are actual specific entities with clearly defined states and behaviors;
Objects are actually encapsulations of "data" and "functions", in which:
Data represents its own status, also known as "attributes" or "member data";
Functions represent their own functions, also known as "methods" or "member functions".
kind
In order to better understand the world, people divide things (objects) in real life into categories;
Things in the same category always have some commonalities;
Classes define entities with common characteristics and behaviors;
A class is a collection of a group of objects with the same properties and behaviors.
property
The characteristics of things are represented by variables in the class;
Each property of each object has its specific value;
The attribute name is shared by all objects of the class;
Features owned by objects or entities are called attributes when represented in a class
method
The behaviors and actions of things are represented by functions in the class;
Each object has the same actions and behaviors;
The actions performed by an object are represented as methods in the class.
The difference between a class and an object
Classes are "templates" or "prototypes" used to describe entities;
Objects are actual entities, and each object is a concrete instance of the class;
Classes are used to define all the properties and methods of an object, and all objects of the same class have the same characteristics and operations;
Classes can be understood as molds that produce products, and the object is products produced based on this mold.
Classes and structures
Package
Package something together and present it in a new and complete form;
The way to handle hidden attributes, methods, or implementation details is called encapsulation;
Encapsulation is actually selectively exposing or hiding certain information, which solves the security issues of data.
inherit
Inheritance is a feature of reusing existing classes to generate new classes;
In layman's terms, it is the process of creating a new class (subclass or derived class) from an existing class (i.e., a parent class or base class);
In real life, inheritance can achieve the purpose of property reuse, while in Java, inheritance can make code reuse.
Polymorphic
Polymorphism refers to the different implementations of the same function in different classes;
The advantage of polymorphism is that it makes classes more flexible and easier to expand.
There is also an "abstract" that I have to talk about here
abstract
The process of classifying the same or similar objects into the same category is abstraction, so abstraction is a method of analyzing problems;
The basic principles of abstraction:
Only care about the main issues, not the secondary issues;
Only care about the main contradiction, not the secondary contradiction;
Just care about the same things, not the different things;
Just care about what the problem is and what can be accomplished, but not how to accomplish it.
The abstract process is actually the core idea of object-oriented programming.
Defining classes in Java
Create an object in Java
Syntax for creating an object
Similar to arrays, objects also refer to data types, and can only allocate memory from the heap using the new operator;
General syntax for creating objects:
Class name reference name = new class name();
Using already defined classes, the process of creating objects in that class is called "instance".
Member operator " . "
Only by instantiating the object of the class first can members (properties and methods) be accessed in the class;
Use member operators (.) to access member properties or member methods;
The general syntax is:
Object name. Member name such as:
std.age = 18; //Assign a value to the member attribute std.dining(); //Calling member method
Access permissions: public and private
Members of structures in C language can be accessed from anywhere, which will leave great hidden dangers to data security;
In order to avoid data corruption caused by directly accessing class members from outside the class, Java sets constraints on access to class members;
The keywords public and private are access modifiers to indicate whether a member can be accessed from outside the class;
Members of public modifications can be accessed anywhere without any restrictions;
Members modified by private can only be accessed by other members in this class, and cannot be accessed from outside the class.
Unable to access private members from outside the class;
Private members of other classes are also hidden from the current class.
Access permission example
class Student { private String name; // Name, private, private, private, private, private, private, private, private, private, private, private, directly access from outside the class; // Weight, private, directly access from outside the class// Method of eating, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, public, //The walk method is an internal member of the class, and you can directly access the private members of this class} } public class Test { public static void main(String[] args) { Student std = new Student(); //Instantiate a Student object std.age = 18; //Try to access private members from outside the class, an error will be reported std.dining(); //Accept access to public members} }
Access permissions (continued)
Adding access modifiers may sometimes cause inconvenience to operate data, but can ensure the security of data to a large extent;
Generally, we declare member attributes as private and member methods as public, but doing so is not absolute;
Sometimes, some private data members may need to be operated outside the class, so a public method can be added, and this method can be used to operate private data to avoid data corruption caused by misoperation outside the class;
Because the main method must be called by a virtual machine outside the class, the main method must be declared public.
For example: modify the Student class
class Student { //Define the student class private String name; //Name, private private int age; //Age, private private float weight; //Weight, private public void setName(String name) { //Method for assigning values to names, public this.name = name; } public void setAge(int a) { //Method for assigning values to age, public age = a; } public void setWeight(float w) { //Method for assigning values to weight, public weight = w; } public void display() { //The method to print out all information is publicly owned System.out.println("name: " + name + ",Age: " + age + ",Weight: " + weight); } public void dining() {...} //The method to eat, public, code slightly public void walk() {...} //The method to walk, public, code slightly} public class Test { public static void main(String[] args) { Student std = new Student(); //Instantiate the student class object std.setName("Zhang San"); //Assign the name std.setAge(18); // Assign std.setWeight(55); // Assign std.dining() to weight; // Call the method of eating std.display(); //Print out the information} } Object Initialization
In the above example, you can only assign values to data members one by one. If you want to initialize member attributes while instantiating the object, you use the construction method;
The constructor is a special member method, which has the same name as the class and is automatically called by the virtual machine when the object is instantiated;
Please note: The constructor has no return value type and cannot have a return value.
Example of construction method:
/*Define the ConstructorDemo class and test the constructor method */ class ConstructorDemo { /*Constructor, the method name is exactly the same as the class name, there is no need to specify the return value type, nor can there be a return value */ public ConstructorDemo() { System.out.println("This is the constructor"); } } /*Test class, used to accommodate the main method, generally declares the class containing the main method as public*/ public class Test { /*main method, program entry */ public static void main(String[] args) { /*Instantiate the object of the ConstructorDemo class*/ ConstructorDemo cd = new ConstructorDemo(); } } //It will output "This is the constructor method" Construction method
It is precisely because the constructor method is automatically called while instantiating the object, so the constructor method is generally used to allocate resources to data members or initialize data members;
General form of construction method:
Access permission class name (formal parameter list) {
Method body
}
Because the constructor is called by the virtual machine, the constructor should generally be defined as public.
For example: Add a constructor to the Student class
class Student { //Define the student class private String name; //Name, private private int age; //Age, private private float weight; //Weight, private //Construction method, assign values to data members according to the passed parameters public Student(String n, int a, float w) { //Assign initial values to each data member name = n; age = a; weight = w; } public void setName(String n) {...} //Assign values to name, public, code slightly public void setAge(int a) {...} //Assign values to age, public, code slightly public void setWeight(float w) {…} //Methods to assign weight, public, code slightly public void display() {…} //Methods to print out all information, public, code slightly public void dining() {…} //Methods to eat, public, code slightly public void walk() {…} //Methods to walk, public, code slightly public void walk() {…} //Methods to walk, public, code slightly public class Test { public static void main(String[] args) { //Use the construction method to specify the initial value for data members Student std = new Student("Zhang San", 18, 55); std.display(); //Print the information} }
Constructing method (continued)
Each object must execute a constructor when generated, and can only be executed once;
If the constructor method call fails, the object cannot be created;
The constructor cannot be called directly explicitly;
Without a constructor defined, the class will automatically generate a default constructor without parameters, which does nothing;
Once the constructor is explicitly defined, the default constructor disappears automatically. Therefore, two construction methods are generally defined: no parameters and no parameters.
Summarize