Last time I wrote for you “process and object oriented similarities and differences”, as well as a more graphic way to describe the “class and object relationship” I do not know if anyone understands thoroughly, if not thoroughly, please open the following article to watch oh

How to understand object orientation, classes, and objects

Let’s define a class and create objects in it directly. Finally entered the code writing stage. This section focuses on the basic definitions of classes and objects, and the basic usage of attributes and methods.

Learning tutorials recommended:

  • 1. Beijing Gaoqi Java300 Set (Java strongly recommended)

    Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners
  • 2.JavaSE Basics – Starting from scratch (recommended)

    Object Oriented Java Basics /JavaSE/ Object-oriented programming /OOP programming _bilibili
  • 3.Java Full Course – basic prerequisite

    JAVA Full course _Java introduction _Java Zero Foundation prerequisite _Java Programming course _Java Core Foundation _EasyUI_SSM Integration framework _redis_High Concurrency — Full course
  • 4.Java common class basic practice

    Java common class actual combat basic tutorial _Java eight common class core foundation _Java common class basic /Java wrapper class /String class
  • 5. Basic Mathematics knowledge for Java [Data Structure and Algorithm] (recommended)

    Data structure and algorithm _Java data structure and algorithm foundation to advanced /Java basic introduction to advanced /Java data structure analysis /Java data structure FAQ _ bilibili_bilibili
  • 6.Java object-oriented programming _OOP basic in-depth explanation

    OOP OOP OOP OOP OOP OOP OOP OOP OOP OOP OOP
  • 7.GOF23 kinds of design patterns – detailed explanation of design patterns in 23 courses

    Java GOF23 kinds of design patterns from singleton to memo mode 23 kinds of patterns in detail

.

Define classes and create objects

Properties (Field member variables)

Properties are used to define the class or the data or static characteristics that the class objects contain. The scope of the property is the entire class body.

Member variables can be initialized when they are defined; if they are not initialized, Java initializes them using default values.

Attribute definition format:

[modifier] Attribute type Attribute name = [default];

methods

Method is used to define the behavior characteristics and functionality implementations of the class or an instance of the class. Methods are abstractions of behavior characteristics of classes and objects. Methods are very similar to process-oriented functions. In procedural orientation, function is the most basic unit, and the whole program consists of function calls. In object oriented, the basic unit of the whole program is class, and methods are subordinate to class and object.

If you need it, you can watch the following article:

Dimple: Java entry pose [method] Introduction of initial methods and method concepts

2. Learn more about the method

Method definition format:

[modifier] method return value type method name (parameter list) {// n statements}Copy the code

Void means no return value; Method for: Reuse code, encapsulate functionality, easy to modify

.

[Example 1] : Define classes to simulate human behaviors: eating, working and resting

1) Object-oriented analysis OOA

Extract common content from Zhang SAN, Li Si, Wang Wu, extract a class, Person, containing common content

Static characteristics: name, age, gender, address

Dynamic behavior: EAT, rest, work

2) OOD Design

Class: the Person

Object: Zhangsan and Lisi

Let the object do its job

3) Object-oriented Programming

Public class Person {// static attribute: field String name; // name int age; Public void eat(){system.out.println ("---- "){system.out.println ("---- -- -- -- -- -- "); (1) {} public void the work System. Out. Println (name + "-- -- -- -- -- -- -- in the work -- -- -- -- -- -"); } public void rest(String site){system.out.println (name+" at "+site+" at "); }}Copy the code

[Example 2] : Simulate the characteristics and behaviors of specific people

public class Test { public static void main(String[] args) { //int n=5; int n; n = 5; System.out.println(n); Scanner input = new Scanner(System.in); Person person1 = new Person(); Person person1 = new Person(); Person person1; Person1 = new Person(); person1 = new Person(); person1.name="zhangsan"; person1.age = 23; person1.eat(); person1.work(); Person1. Rest (" dormitory "); Person person2 = new Person(); Person person2 = new Person(); person2.name="lisi"; person2.age=24; person2.work(); Person2. Rest (" site "); person2.eat(); }}Copy the code

Scanner Input = new Scanner(system.in); Person person1 = new Person(); New follows the constructor, which will be explained later

Summary 2: How to manipulate properties: object names. The property name person1. Name = “zhangsan”; person1.age = 23;

Summary 3: How to call methods: object names. Method name (argument list)

person1.eat();

person1.work();

Person1. Rest (” dormitory “);

.

Memory allocation diagram

2. Local variables

Variables defined in classes are member variables, while variables defined in methods, including method parameters, and variables defined in code blocks are called local variables. The differences between the two are mainly reflected in the following aspects:

Local variables [Example 1] : Add and subtract using a calculator

Public class Calculator {// Private String brand; // private double size; Public int add(int num1,int num2){int result; result = num1+num2; return result; } public int sub(int num1,int num2){ return num1-num2; } public void show(){system.out.println (" I buy a calculator "+brand+","+size); } public static void main(String[] args) {// Buy Calculator calc = new Calculator(); Calc. Brand = "association "; Calc. Size = 6.5; calc.show(); Int num1 = 10; int num2 = 20; int result = calc.add(num1,num2); System.out.println(result); result = calc.sub(num1,num2); System.out.println(result); }}Copy the code

Local variables: The memory allocation for code execution is shown below:

The above is all the content of this chapter, I will update the follow-up oh, like the partner support oh ~

Thanks for watching ~