JAVA basics/Lesson 6: Object-oriented/JAVA classes and objects, Object-Oriented java

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.