This is the first day of my participation in Gwen Challenge

Introduction: Object orientation is the core of the JAVA development language. What is object orientation?

First, object-oriented thought:

Object oriented is one of the most popular programming methods, almost all applications are now oriented to the object, the earliest concept of object oriented is actually proposed by IBM, in the 1970s Smaltalk language was applied, later according to object oriented design ideas, C++ was formed, From C++ came Java, an object-oriented programming language.

But before object-oriented design, widely used is process oriented, process oriented only for themselves to solve the problem. The basic function of process oriented operation is based on the program implementation is given priority to, after the implementation is done, also do not consider the possibility of modification, object-oriented, more is to be the son of modular design, each module needs to exist alone, and can be reused, so the object-oriented development is more like a standard development mode.

In the object oriented definition, there are also some basic characteristics:

  • Encapsulation: to protect internal operations from damage;
  • 1. To continue to expand upon an original foundation;
  • Polymorphism: Conversion of concepts within a specified range.

Object-oriented development is also divided into three processes:

  • OOA: Object-oriented analysis
  • OOD: Object oriented design
  • OOP: Object-oriented programming

Ii. Class and Object Concepts:

1. The object:

Object is a kind of personality representation, represents an independent individual, each object has its own independent attributes, rely on attributes to distinguish different objects

Everything in the world can be regarded as objects, such as the objects around us:

2. Categories:

It is a collection of abstract concepts, representing a common product. The attributes and behaviors (methods) defined in the class are extracted from the common attributes and behaviors of objects existing in objective fact:

If we extract the common attributes of our different cars, we form the sedan class;

The common attributes of Zhang Hao and Li Ming are extracted to form customer class.

3. Properties:

The various characteristics of objects (each object has a specific value for its properties)

Each property of each object has a specific value

Zhang Hao and Li Ming are different in age and name

4. Methods:

The operation performed by the

For example, As a cashier, Li Ming can carry out the cashier’s check, print bills, swipe cards and other operations.

5. Class and object relationship:

Classes are templates for objects, and objects are instances of classes.

Classes can only be used through objects, and in development you should generate classes first and objects later.

Classes cannot be used directly; objects can be used directly.

A class is an abstract concept, just a template. For example, a “person” object is a concrete entity that you can see and touch

Class and object usage:

1. Define class (steps) :

  • Define the name of the class
  • Write the properties of the class
  • Write the methods of the class
public classThe name of the class{
    // Define attributes section (member variables)attribute1Type attribute of1; attribute2Type attribute of2; . The type attribute n of attribute n;// Define the method sectionmethods1; methods2; . Methods m; }Copy the code

Define a student class, the demo code is as follows:

package cn.hz;

/** * Defines the student's class *@author hz
 * @version1.0 * /
public class Student {
    public int id;        // Define attributes: student number
    public String  name;  // Define attribute: student name
    public int age;       // Define attributes: student age
    // Define a method for printing student information
    public void printInfo(a){
        System.out.println("Name:"+name+", age:+age); }}Copy the code

2. Create object:

Once the class definition is complete, it cannot be used directly. If you want to use it, you have to rely on objects. Since classes are reference data types, you can use them in the same way as variables:

Method 1: Declare and then instantiate the object:

Create a student object:

Student student=null;      // Declaration: class name object name =null;
student=new Student();     // Instantiate object name =new class name ();
Copy the code

Method 2: Create objects while declaring:

Create a student object:

Student student=new Student();   // Class name object name =new class name ();
Copy the code

Summary:

The biggest difference between reference data types and basic data types is that reference data types require memory allocation and usage. Therefore, the main function of keyword new is to allocate memory space, that is, whenever a reference data type is used, the keyword new is used to allocate memory space.

3. Use objects:

When an instantiated object is generated, operations on the class can be performed as follows:

Property: represents a property in the calling class; Object.method () : represents calling a method in a class.

Create a student object and assign the name of the student to Zhang SAN and the age to 18, then print the information of the student, the code is as follows:

package cn.hz;

/ * * *@author hz
 * @version1.0 * /
public class StudentTest {
    public static void main(String[] args) {
        // Create a student object:
        Student student=new Student();
        // Assignment: object. attribute
        student.name="zs";
        student.age=18;
        // Print: object. methodsstudent.printInfo(); }}Copy the code

The print result is as follows:

Question: Assignment is too cumbersome. Can I assign at creation time?

Answer: Yes, object initialization can be done later through the constructor

Conclusion: