Java learning tutorial from scratch -- six: object-oriented programming

First knowledge of object-oriented

Process oriented & object oriented

Process oriented thought

  1. The steps are clear and simple, what to do in the first step and what to do in the second step
  2. Facing the process, it is suitable to deal with some relatively simple problems

Object oriented thought

  1. The thinking mode of classification is to first solve the problem of which classification is needed, then think about the classification separately, and finally think about the details of a classification process oriented
  2. Object oriented is suitable for dealing with complex problems and problems requiring multi person cooperation

For describing complex things, in order to grasp them macroscopically and analyze them reasonably as a whole, we need to use object-oriented thinking to analyze the whole system. However, when it comes to micro operation, we still need process-oriented thinking to deal with them.

What is object oriented

  1. Object oriented programming (OOP)
  2. The essence of object-oriented programming is to organize code by class and encapsulate data by object
  3. Object oriented is abstract
  4. Three major object-oriented features (these will be summarized in the later study)
    • encapsulation
    • inherit
    • polymorphic
  5. From the perspective of epistemology, there are objects before classes. Objects are concrete things, classes are abstract, and objects are abstract
  6. From the perspective of code operation, there are classes before objects, and classes are the template of objects.

Review and deepening of methods

Definition of method


  1. Public: public, which can be used by this package and other packages
  2. Static: static, which enables methods to be loaded together with classes

Return type

And the type of return value after the method body is executed
If there is no return value, it can be written as return; Or don't write a return statement

Method name

  1. Named after the hump principle
  2. Ask to see the name

parameter list

That is, parameter type, number, order, etc

Exception throw

We'll talk about this later
Just look at the format first

public void readfile(String file) throws IOException{

(at present, we have encountered the problem of array subscript out of bounds)

Method call

Non static method

Instantiate before calling

Static method

Are you curious about how my interface is so convenient and easy, as shown in the figure above:

The difference between the two

Let's look at some code first
This code does not report an error in idea

public class Demo01 {
    public static void main(String[] args) {
    public void a(){
    public void b(){       

The code does not report an error in idea

public class Demo01 {
    public static void main(String[] args) {
    public static void a(){
    public static void b(){

This code does report an error

public class Demo01 {
    public static void main(String[] args) {

    public static void a(){
    public  void b(){


Because static modified methods are loaded with classes, while methods without static modification only exist after instantiation

Formal and argument

We analyze formal parameters and arguments in the code

public class Demo01 {
    public static void main(String[] args) {
         int add=Demo01.add(1,2);
    public static int add(int a,int b){
        return a+b;

In this code, a and b play the role of receiving 1 and 2 values. They are not actual variables, which are called formal parameters

public class Demo01 {
    public static void main(String[] args) {
         int a=1;
        System.out.println(a);//Output 1
        System.out.println(a);//Output 1
    //No return value
    public static void change(int a){

Both outputs are 1 because a in the change method is not an actual parameter

Value passing and reference passing

The above code is an example of value passing
For reference passing:
Note: a Java file can have multiple classes, but only one public class

import java.sql.SQLOutput;
//Reference passing: object, essence or value passing
public class Demo01 {
    public static void main(String[] args) {
        Person person=new Person();
    public static void change(Person person){"wqy";
    Defines a Person Class, there is one name attribute
    static class Person{
        String name;//null

this keyword

//Student class
public class Student {
    //Properties: Fields
    String name;//null
    int age;//0
    public void study(){
        System.out.println("I'm learning");// here points to a name with a null value

this will be explained in more depth in the future

Object creation analysis

Relationship between class and object

  1. Class is an abstract data type. It is the overall description / definition of something, but it can not represent a specific thing.
    1. Animals, plants, mobile phones, computers
    2. Person class, Pet class and Car class can all define and describe the characteristics and behavior of a specific thing
  2. Objects are concrete instances of abstract concepts
    1. Zhang San is a concrete example of man
    2. It is a concrete instance rather than an abstract concept that can reflect the characteristics and functions

Creating and initializing objects

  1. Create an object using the new keyword
  2. When using new to create an object, in addition to allocating memory space, it also initializes the created object by default and uses the constructor in the class
  3. Constructors in classes, also known as construction methods, must be called when creating objects, and constructors have the following two characteristics:
    1. Must be the same as the class name
    2. There must be no return value and void cannot be written

Code demonstration

Note: there should be only one main method in a project. And the values of name and age cannot be initialized in the Student class, otherwise the values of all objects in this class will be specified


First, let's observe a phenomenon:
The Person in the figure is a class we created

Open project structure:

Add content root:

Select the out directory at the same level as the project we created:

Find the Person file generated by decompilation:

Compared with the original class file, it is found that there is an additional piece of code formed by decompilation:
public Person(){}
It's actually a constructor
This tells us that even if a class doesn't write anything, there will be a method
We can also define a constructor ourselves:

The keyword this can also be used here:

There is also a constructor with parameters:

You can also define a constructor with multiple parameters

Constructor summary

  1. characteristic
    1. Same as class name
    2. No return value
  2. effect
    1. The essence of new is to call the constructor
    2. Initializes the value of the object
  3. Shortcut key: alt+insert new constructor
    Note: after defining a parameterized constructor, if a parameterless construct is used, the definition of a parameterless construct will be displayed

Explanation of memory in Java

Recommend an article:
Because we haven't learned many things, we can look at them slowly in the future.

Keywords: Java JavaSE

Added by Naug on Mon, 11 Oct 2021 20:47:24 +0300