Java Object-oriented

Source: Internet
Author: User

All things are objects

How do we know the world?

Human beings have been constantly exposed to various types of life in the world, and then through the public characteristics of things to classify them, so later will not appear to see Cats called Tigers. Then we in the real life, is through the concrete certain things to summarize their public characteristics and then produce the class so that the general description of the thing, the equivalent of the blueprint for the creation of things, we can according to this drawing to make concrete physical objects.

Object : There is a specific thing in real life.

class: actual is the common attribute and behavior of a certain type of thing extraction.

Human knowledge of the World: Objects----> Classes.

In Java: Class-----> Objects.

Using computer language is the constant description of things in real life.

The description of things in Java is embodied in the form of classes, which are abstract, conceptual definitions of specific things.

An object is an individual that has a real existence for that kind of thing.

An overview of object-oriented

"Object-oriented" (English: Object oriented, or OO) is a thing-centric programming idea.

Object-Oriented Programming (English: object-oriented programming, abbreviation: OOP), is a method of program development. It takes the object as the basic unit of the program, encapsulates the program and data to improve the reusability, flexibility and extensibility of the software.

Object-oriented is relative to the process-oriented (c is a typical process-oriented language), the object-oriented perspective to see the problem, you are the object of the action of the conductor. If you look at the problem from a process-oriented perspective, you are the performer of the action.

Object-oriented versus process-oriented comparison

"All things are objects".

1: Buy a computer

1: Process oriented

1: Check Information

2: Computer City Bargain

3: Be Black

4: The Return of pain

1: Object-oriented

1: Find the object. Teacher

2: the teacher. Bargain

3: The teacher. Testing the computer

4: Computer successfully purchased

2: Eat

1: Process oriented

1: Do It Yourself

2: Buy Vegetables

3: Wash Vegetables

4: Cooking rice and stir fry

5: Hard to eat, wasting time

2: Object-oriented

1: Find a Professional object

2: Restaurant. Order a meal

3: Restaurant, cooking

4: Good food, save time, energy

4: Find the Object

1: Seek introductions, blind dates, find ready-made objects. (Object-oriented thinking first to find objects, directly to use)

2: Not satisfied, no object, build one by oneself. (Sun does not provide, own objects)

Another example: People open the door, people turn on TV, people painting garden.

Process oriented

Emphasis is on functional behavior, process-oriented "is a process-centric programming idea. "Process-oriented" they do not support rich "object-oriented" features (such as inheritance, polymorphism), is to analyze the steps required to solve the problem, and then use the function to implement the step by step, the use of a one-time call in turn. Process-oriented in the implementation of this series of work, the emphasis is on the implementation of the work.

Object

object represents an entity that can be clearly identified in the real world. For example: A student, a table, a classroom, a computer can be regarded as an object. Each object has its own unique state identity and behavior

The properties of the object (attribute, or state), students have names and student numbers, and the student's unique name and school number are the attributes of the student (object).

The behavior of the object (behavior), which is defined by the method, is a method of invoking the object, which is actually sending a message to the object that requires the object to complete an action. You can define the behavior of the student object to learn. The student object can call the learning method and perform the learning action.

Object-oriented features

Package (encapsulation)

Inheritance (inheritance)

Polymorphic (polymorphism)

The process of development: in fact, it is constantly creating objects, using objects, directing objects to do things.

The process of design: In fact, the relationship between managing and maintaining objects.

Use Java to describe things

Case: Define a car class through the Java language, and produce cars, color, number of tires, there are functions to run.

Analysis:

How to describe something in the real world, describe the property and behavior of the thing, the car has the property of color and number of tires, has the behavior of running.

How do I convert using the Java language?

According to the corresponding relationship:

Properties: member variables in a class

Behavior: member functions in a class

The definition of a Java class is defined as a member of a class. The automobile class has the member: color, the tire number, the Operation method.

The car class defines the process:

    1. Define classes using the Class keyword,
      1. Class name. The class name is the identifier, the naming convention, the first letter of the word capitalized, and the first letter of multiple words. Note: Not a rule, but it's best to obey
      2. The class name immediately follows a pair of {} to indicate the start and end of the class.
    2. The car has a tire number int num;
      1. Num initialization value is not required because the number of car tires is uncertain, 4, 6, 8.
    3. has a color String color
      1. Why use String for example to define the color "red" is a string type.
      2. There is no need to initialize the value.
    4. Run behavior (method, function) void Run () {}
      1. Executes the output statement in the method. SYSO ("Run ....") ");

Public class Car {

String color;//member variable

int num; Member variables

member functions

void Run () {

System. out. println (color + "car, number of tires:" + num + "One, run up");

}

}

Creation of objects

Create a Car object

    1. Use the new keyword, just like the new array
    2. Need to give a name to the type of car, car
    3. Variables are of a type, what type of car belongs to car type, called class type
    4. Car car=new car ();

The drawing is ready and the class definition is successful. How to produce a car according to drawings, how to produce an object in Java according to the class.

The production of cars in Java is relatively simple, through a keyword "new" Through the new car (); An entity is created in memory, and the car object is produced.

After the production of car objects, there is no name, in order to facilitate the use of a name. Use lowercase c to indicate the name of the new car.

Variables in Java require a type. So what kind of C is it? C is the car type, so C is also called the class type variable.

class Cardemo {

Public Static void Main (string[] args) {

Create objects in Java, using the new keyword. Opens up space in heap memory. An entity was created.

Car c = new car ();

To make it easier to use the cars that were produced, a name was given.

So what type of C is the car type. is called a class-type variable.

Note that C is a reference that is held, the newly produced car is not directly assigned to C, and only holds a reference. C Just like the TV remote control.

C.run (); Use the functionality of the object.

}

}

Invocation of an object member

With the car object, call the object member

    1. Member variables
    2. Member Methods

Public class Cardemo {

Public Static void Main (string[] args) {

Car c = new car ();

The object name. The member variable name returns the value stored in the member variable

int num=c.num;

System. out. println (num);

The name of the object, the name of the member variable, and the value assigned to the member variable

C.num = 4;

C.color = "BLACK";

The name of the object. Member method ();

C.run ();

}

}

Local variables and member variables

member variables: defining variables in a class

Local variables: defining variables in methods

The difference between a member variable and a local variable:

    1. Application scope
      1. Member variables are valid within the entire class
      2. A local variable is only valid within the method it declares
    2. Life cycle
      1. Member variable: It belongs to the object, which is created as the object is created and disappears as the object disappears
      2. Local variables: Free up space immediately after use.

void show (int id) {

for (int i=0;i<10;i++) {

for (int j=0;j<10;j++) {

SYSTEM.OUT.PRINTLN (ID);

}

}

}

At this time the ID,I,J is declared within the method, all local variables

J when the inner for loop executes its life cycle begins, when the inner for ends, J disappears

I when the outer for loop executes its life cycle begins, when the outer for ends, J disappears

ID begins when the method is called, and the ID disappears when the method ends.

    1. The storage location member variable belongs to the object, which is stored in the heap inside the heap, and when no reference is directed to it, the garbage collection cleanup local variable exists in the stack memory and is immediately freed when not in use.
    2. Initial value

The member variable is stored in the heap, and it has a default value if it is not assigned the initial values.

    1. Integer byte, short, int, long = 0;
    2. Char= ' \uoooo ';
    3. Boolean =flase;
    4. String =null;
    5. class type =null;
    6. Array =null;

Local variables that must be initialized manually if you want to use them.

    1. I. method, in the argument list, in the statement.
    2. II. Must give initialization value, no initial value, cannot use
    3. III. In stack memory
Anonymous objects

2.1 Anonymous object: An entity that has no name, that is, the entity does not have a corresponding variable name reference.

2.2 Purpose of Anonymous objects

1, the code can be simplified using an anonymous object when the object makes a call to the method.

Why only the method, not the property? Because anonymous objects call properties, it doesn't make sense.

If an object is to be called more than one member, you must give the object a name. You cannot use an anonymous object.

2, an anonymous object can be passed as an actual parameter.

2: A simple demonstration of anonymous objects

1:new Car (). run ();

3: Memory structure diagram

1:new Car (). num=5;

2:new Car (). clor= "Blue";

Two new is two different objects that have different spaces in the heap memory and do not interfere with one another.

4: Use of Anonymous objects

1: You can use anonymous objects when you use them only once. The object becomes garbage after the execution has finished.

New Car (). run ();

2: When executing a method, an anonymous object can be passed in as an actual parameter.

5: Repair Black Cars

1: Demand

A black car that has been converted into 3 wheels.

1: Car class.

2: Auto repair shop

/*

Anonymous objects

Anonymous letter

Repair Black Cars

Automobile class

Black Cars Factory Class

Turn the car into a black 3-wheeled car.

*/

class Car {

String name = "smart";

String color = "Red";

int num = 4;

void Run () {

System. out. println (name + ":" + Color + ":" + num + ": Running up ....) ");

}

}

class blackcarfactory {

String name;

String addr;

Car Repaircar (car c) {

C.num = 3;

C.color = "BLACK";

System. out. println ("The conversion succeeded ... ");

}

}

class Demo1 {

Public Static void Main (string[] args) {

Blackcarfactory BCF = new blackcarfactory ();

Bcf.name = "Happy shop";

bcf.addr = "Tianhe District Tong Dong East Road Yu Fu Electronics Park a building 206";

Non-anonymous objects

Car c = new car ();

C.run ();

Modified

Bcf.repaircar (c);

Pick up the car

C.run ();

Anonymous object One, used only once:

2 objects were created as follows

/*

* New Car (). run ();

*

* New Car (). run ();

*/

Anonymous object two, passed as the actual parameter

Car C2 = Bcf.repaircar (new car ());

C2.run ();

System. out. println ();

}

}

Summarize:

1. Properties for anonymous object settings are never available? No reference variable points to that object.

2. Any two anonymous objects use = = to compare, always return false.

3. Anonymous objects are primarily applied to arguments.

Java Object-oriented

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.