Java object-oriented and the like and objects

An overview of object Orientation

Java is full Object Oriented Programming, or OOP for short.

Object-oriented programming is a way of thinking that is more relevant to everyday life, because our real world is made up of all kinds of objects, from planes, trains, buildings, to a dog, a laptop, a book. However, each object has its own state and behavior, and we can accomplish certain tasks through objects.

For example, we take a plane from Shenzhen to Xiangxi to travel, we can directly use the flight ability of the plane, regardless of how the object of the plane is made. Just use it.

Using the object-oriented way to describe the world composed of objects, more in line with the way of human thinking;

Because software is designed to:

  • Describe the real world in computer language
  • Using computers to solve real-world problems

Java is currently the most popular object-oriented programming language (Python may not), with four object-oriented characteristics:

  • Encapsulation: hides internal implementation details and provides externally accessible methods
  • Inheritance: A child class automatically inherits all non-private properties and methods from its parent class
  • Polymorphism: multiple representations of the same thing, such as overloading and rewriting
  • Abstract: It is possible to define uniform abstract rules regardless of the implementation

Object-oriented programming is the core idea of Java, but for beginners, learning is still more abstract. So it is suggested that beginners can study together with books, repeatedly watch, so as to master the essence.

Second, the class

2.1 Overview of classes

Classes are essential units in Java applications; The keyword to create a class is :class; In fact, we have already been exposed to classes in the course of previous study, but we have not specifically come up with a class.

Such as:

  • The following creates a name calledTest01The class of
  • One is defined in the classmain()Method is also the entry point of the program
Public class Test01{public static void main(String args[]){// create and assign int score2[] = {100,90,88,85,66}; for(int i = 0; i<score2.length; i++) { System.out.print(score2[i]+"\t"); }}}

So creating classes, it’s not that complicated. Learn the basic syntax and create it according to your needs.

As a high-level language, Java has many pre-defined classes built in, in addition to the ability to create our own classes.

Such as:

  • Scanner
  • Math
  • String

2.2 Predefined Classes

Predefined classes are defined by the Java language according to the actual business scenario. We just need to learn how to use it. We don’t need to create it.

Common predefined classes:

Such as:

  • Scanner: Keyboard input
  • Math: Math related classes
  • String: String class
  • Date: Time and Date related class
  • List: Collection class

Java provide predefined classes is a bit much, fortunately, provides the help document, we can according to the documentation to see: https://www.matools.com/api/java8

The following section is devoted to the commonly used predefined classes.

2.3 Process of discovering classes

Pets — real life objects

How do you describe it in a computer?

There are so many specific pet objects in the world. So how do we use computer language to describe so many pet objects, one by one we can’t do it; So we need to understand the pattern in order to divide it.

Abstracting classes from reality is a three-step process:

  1. Step 1: Find out what species they are
  2. Step 2: Find out their properties
  3. Step 3: Find out what they do

Step 1: Find out what they are

Based on the above information, find out the type of dog;

Java uses the class keyword to describe classes;

// Create a class named Dog. Class Dog{}

Step 2: Discover the characteristics of the class

We can extract the same characteristics (attributes) from each object; But any one object can have multiple characteristics

A Dog is a Dog

Although there are many features, we actually only store the features (properties) that we need:

Class Dog {String name = ""; Int health = 100; // Health value int love = 0; String strain = "labrador "; / / varieties}

Step 3: Discover the methods of the class

The methods of a class, also known as behavior, are dynamic. Each method can be seen as a separate function

Just like properties, we store the behavior that we care about

Class Dog {String name = ""; Int health = 100; // Health value int love = 0; String strain = "labrador "; Public void print() {// print the code for the dog}}

After these three steps, we can create a complete class. And specify the properties and methods contained in the class.

2.4 Composition of classes

Through the process of class discovery above, we can see the basic composition of a class:

/ / syntax format: [< modifier >] class < class name > {[< > attribute statement] [< > constructor statement] [< > method statement] [{} a code block]}

1, properties,

Define attributes: syntax

// Syntax [modifier] Data type attribute name = [initial value]; // For example, public String name = "Lifu "; // Create a property and give it an initial value

Decorator:

  • public: public, which has the largest access permission and can be accessed throughout the project
  • private: represents private, least accessible, and can be accessed in the current class

Data type:

  • The data types can be either the eight basic data types or reference data types
  • The initial value of the property depends on the data type.Int defaults to 0,String Defaults to NULL

Attribute name:

  • Properties are also called global variables
  • You just need to follow basic naming rules and conventions

2, methods,

Also called behavior, normally a method represents a specific function. For example: run, eat, sleep

// Syntax: [modifier] Return value type method name ([argument list]){// method body. Public void show(String name){system.out.println (" my name is: "+name); }

The method consists of four elements:

  • The return value
  • Method names
  • The list of parameters
  • Method body

The four elements of the method are indispensable.

2.1 the return value

When the method finishes executing, the value is returned to the caller of the method

  1. You must define the exact type of the return value. If there is no return value, use void
  2. If there is a specific return value, the return statement must be used in the statement block
// there is no return value //1. Public void add(int num1,int num2) {int sum = num1+num2; //2. The return keyword must be used after the body of the method to return a specific number //3. Public int desc(int num1,int num2) {int d = num1-num2; return d; }

2.2 Method Name

It’s just an identifier, according to naming conventions and conventions

Specification:

  • Hump naming rules; Lowercase the first word and uppercase every other word getUserByName();
  • See of knowledge meaning

2.3 Parameter List

When defining a method, the parameters are optional.

  1. Parameter list :(formal parameters), which is just responsible for defining the parameters, as well as the types and names of the parameters. But the actual value of this parameter per page
  2. Argument list: when the method is called
Public int desc(int num1,int num2) {int d = num1-num2; return d; } // acb = p1.desc(50,20); // acb = p1.desc(50,20);

2.4 the method body

{} represents the method body and is also the concrete business implementation of the method

Public int desc(int num1,int num2) {// The service depends on the scenario. Int d = num1-num2; return d; }

3, construction method

3.1 Features of construction methods

It has the same name as the class; It has no return value, and there is no need to use the void keyword to qualify that a class can have more than one constructor

Public class Person {public Person () {/ / the default no arguments structure} public Person (String name) {/ / define and construct}}

3.2 Function of construction method

According to the specified rules to create an object, complete the initialization of the object salary; (Assign values to all attributes)

// Note: constructors cannot use void; otherwise, they are normal methods. The constructor function is lost // The constructor is called with the new keyword. Cannot be called from an object: new Person(); New User("zhangsan", "shenzhenhuamju ", "110"); // The call has a parameter construct

3.3 Construction Method Attention

  • In the Java language, every class has at least one constructor;
  • If the class definer does not explicitly define any constructors, the system automatically provides a default constructor:
  • In a Java class, once the class definer explicitly defines one or more constructors, the system no longer provides a default constructor;
  • The main purpose of a constructor is to initialize the properties of an object using constructor parameters.
public Person(String name,int age){
    this.name = name;
    this.age = age;
}

2.5 class diagram

Describes information about the entire class;

  • Used to analyze and design “classes”
  • Intuitive and easy to understand

Three, objects,

1, the introduction of

  • Everything is an object, everything is an object
  • An object is a concrete implementation of a class. A class can create any number of objects
  • Each object is actually a copy of the object and the objects do not interfere with each other
  • Key words:new
  • Use:

    • Object name. Property name –>student.username
    • Object name. Method name () –>student.eat();

2. Syntax for creating objects

New + constructor () : for example, new Dog(); new Person(); Special methods: a. No return value is required b. The method name is the same as the class name;

3. Access mechanism of class

  • Access mechanism within a class: methods in the class can directly access member variables in the class
  • Access mechanism in different classes: first create an object to access the class, and then use the object to access the members defined in the class.

4. Anonymous objects

  • We can also call methods on an object without defining a reference to the object. Such objects are called anonymous objects, such as:

- Anonymous objects can be used if only one method call is required on an object. - We often pass anonymous objects as arguments to a function call