JAVA basics/Lesson 6: Object-oriented/JAVA classes and objects, Object-Oriented java
2013-0
I. process-oriented and object-oriented
1. process-oriented programming: Starting from every step of solving the problem, it is suitable for solving small and simple problems. Program = Algorithm + data.
2. object-Oriented Programming: manages complex things according to the characteristics of the real world, abstracts them into an object, has its own State and behavior, and completes certain tasks through message response.
Program = Object + message.
2. Three main features of object-oriented architecture:
1. encapsulation: Hide the information attributes and methods. Only interfaces for calling methods are provided, which are encapsulated.
2. Inheritance: retain the attributes and methods of an existing class, and add its own unique attributes and methods to form a new class, which is inherited.
3. Polymorphism: multiple methods with the same name but different implementations exist in one program at the same time. There are two methods: overload and overwrite.
A Class is an abstract and conceptual definition of a group of similar things with the same attributes, operations, and relationships.
An Object is an entity that actually exists in this class of things. Therefore, it is also called an instance ).
Iii. Class Definition:
Class defines the attributes (variables), methods, and relationships of a class.
A class consists of a class declaration and a class body. format:
// Class Declaration public class Hero {// class body String name; // name float hp; // volume float armor; // armor int moveSpeed; // moving speed public static void main (String [] args) {Hero garen = new Hero (); garen. name = "Galen"; garen. hp = 616.28f; garen. armor = 27.536f; garen. movespeed= 350; Hero teemo = new Hero (); teemo. name = "Timo"; teemo. hp = 383f; teemo. armor = 14f; teemo. moveSpeed = 330 ;}}
Class is like a template. Based on such a template, you can create specific Heroes one by one.
Specific heroes are called objects.
New Hero () means creating a Hero object in java.
Iv. Object declaration:
Object Instantiation and initialization
Person p1 = new Person ("Tom", 0 );
Class Name: Person
Object Name: p1
New keywords instantiate declared objects and allocate memory space in the heap;
When an object is created, various types of member variables are automatically initialized and assigned values.
Except for the basic data type, all variable types are reference types, such as the Person and the array mentioned above.