Relationship / attribute / member variable / method / UML diagram of Java objects and classes

The difference between process oriented and object-oriented

sketch

  1. The process oriented idea is suitable for simple transactions that do not need cooperation. For example, simply driving a car does not need to cooperate with others.
  2. The object-oriented idea is suitable for complex transactions, and 1234 steps cannot be listed. Like how to build a car? Building a car is too complicated and requires a lot of cooperation.

summary

  1. Object oriented and process oriented are both ways of thinking to solve problems and ways of code organization.
  2. Process oriented can be used to solve simple problems.
  3. Solving complex problems: object-oriented is used in macro processing, and process oriented is still used in micro processing

Concepts of objects and classes

sketch

  1. Objects have different methods, properties, variables, etc
  2. Class can be regarded as a template or drawing. The system creates objects according to the definition of class.
  3. Class: call it class. Object: called object, instance.
  4. An object of a class is equivalent to an instance of a class. Same meaning.

Class definition

  1. Each source file must have one and only one public class, and the class name and file name must be consistent.
  2. A Java file can define multiple class es at the same time
  3. For a class, there are generally three common members: attribute field, method and constructor. All three members can define zero or more.

Code example: how classes are defined

// The classes defined below are empty and have no practical significance. You also need to define the specific information of the class.
// Each source file must have one and only one public class, and the class name and file name must be consistent.
public class SxtStu {
}
// A Java file can define multiple class es at the same time
class Tyre{
}
class Emage{
}
class Try{
}

Code example: simple student class writing

// For a class, there are generally three common members: attribute field, method and constructor. All three members can define zero or more.
public class SxtStu {
    // Properties (member variables)
    int id;
    String sname;
    int age;
    // method
    void study(){
        System.out.println("I'm fishing and learning!");
    }
    // Construction method
    SxtStu(){
    }
}

Attribute (field)

sketch

  1. Property is used to define the data or static characteristics contained in the class or class object.
  2. Attribute scope is the entire class body.
  3. When defining a member variable, you can initialize it. If not, Java initializes it with the default value.

Attribute definition format

[Modifier ]  Property type property name = [Default value];

Member variable defaults

method

sketch

  1. Method is used to define the behavior characteristics and function implementation of the class or an instance of the class. For example, a student class can have the behavior characteristics or function realization of learning or playing games
  2. Methods are abstractions of the behavioral characteristics of classes and objects. Methods are very similar to procedure oriented functions.
  3. In process oriented, function is the most basic unit, and the program is composed of function calls.
  4. In object-oriented, the basic unit of the whole program is class, and the method is subordinate to class and object.

Method definition format

[Modifier ]  Method return type method name(parameter list ){
    // n statements
}

Code example:

public class SxtStu {
    // method
    void study(){
        System.out.println("I'm fishing and learning!");
    }
    void play(){
        System.out.println("I was playing games! LOL Mobile Games!");
    }
    // Construction method
    SxtStu(){
    }
}

Code example: Call of complete object and class methods

  1. The program must have a main method as the entry for program execution
  2. Constructor: the object used to create this class. The construction method without parameters can be automatically created by the system.
  3. For a class, there are generally three common members: attribute field, method and constructor. All three members can define zero or more.
public class SxtStu {
    // Properties (member variables)
    int id;
    String sname;
    int age;
    // method
    void study(){
        System.out.println("I'm fishing and learning!");
    }
    void play(){
        System.out.println("I was playing games! LOL Mobile Games!");
    }
    // Constructor: the object used to create this class. The construction method without parameters can be automatically created by the system.
    SxtStu(){
    }
    public static void main(String[] args) {
        // Defines the object that creates a class
        SxtStu stu = new SxtStu();
        // Object calls the play() method
        stu.play();
        // Object calls the study() method
        stu.study();
    }
}

Code example: simulate students using computers to learn

  1. Constructor: the object used to create this class. The construction method without parameters can be automatically created by the system.
  2. For a class, there are generally three common members: attribute field, method and constructor. All three members can define zero or more.
// Simulate students using computers to learn
class Computer {
    String brand;  // Computer brand
}

public class SxtStu1 {
    // Define attributes (member variables)
    int year;
    String sname;
    int age;
    // Define an object of Computer class in SxtStu1 class
    Computer comp;

    // Definition method
    void study() {
        System.out.printf("I'm using%s This brand of computer fishing learning!%n",comp.brand);
    }
    // Constructor: the object used to create this class. The construction method without parameters can be automatically created by the system.
    SxtStu1(){
    }

    public static void main(String[] args) {
        // Object defining SxtStu1 class
        SxtStu1 stu1 = new SxtStu1();
        // stu1 object calls the property (member variable) of SxtStu1 class and assigns a value
        stu1.year = 2021;
        stu1.sname = "Ah jun";
        stu1.age = 0;
        // Object that defines the Computer class
        Computer comp1 = new Computer();
        // The Computer1 object calls the attribute (member variable) of the Computer class and assigns a value
        comp1.brand = "Shenzhou God of war";
        // stu1 object calls comp class attribute (member variable) and assigns value to comp1
        stu1.comp = comp1;
        // Object call method
        stu1.study();
    }
}

Execution results:

UML diagram example

Keywords: JavaSE

Added by amazing on Tue, 02 Nov 2021 16:32:14 +0200