Java Learning Note 8 (object-oriented one)

Source: Internet
Author: User

Process-oriented thinking: How do I solve this problem? And then fix it one step at a

Object-oriented thinking: When you meet one thing, think about it, who do I want to do, and how he does it, not what I need to think about, as long as it's done in the end.

Practical example: We're going to assemble a new computer.

Process-oriented: We search the Internet for various hardware parameters cost-effective, go to the Mall to observe the inquiry, and then analyze the most appropriate, buy a variety of accessories themselves assembled

Object-oriented: Ask an expert who knows how to assemble a computer, tell him I want a new computer, and then give it to me when he's assembled.

Object-oriented Benefits:

1. More in line with people's thinking habits

2. Object-oriented is more like a conductor, and process-oriented like a performer

3. Object-oriented thinking patterns can simplify complex problems

4. All things in the world are objects

Example: Describe a real car in a class way

Define car:

 Public class Car {    // define color     String;     // define the number    of tires int count;     // define the function    of running  Public void run () {        System.out.println (color + "+count+" wheel is Running ");}    }

Running the car:

 Public class cartest {    publicstaticvoid  main (string[] args) {        new Car ();         = "Black";         = 3;        C.run ();    }}

After running:

Memory analysis of the object:

1.main method runs into the stack

2. Create a car class, open a space in the heap memory, deposit the new object, the class member variable follows into the heap, and gives the default value (null,0)

3. This memory address is passed to the reference variable, C, which refers to the object in memory

4.c pointer to the in-memory variable color,count is modified

5. Method run is called by Object C and is executed in the stack

6.run method execution complete, free stack memory

7.main method end, free stack memory

8.C variable disappears, object in heap memory is cleared, memory is freed

Class and object relationships:

Class: An abstract description of a class of things

Object: An individual used to represent something in the real world

Class is used to describe common characteristics of multiple objects, which is equivalent to a template

Objects are created from a class, and a class can correspond to multiple objects

A variable written in a class is a member variable, and a variable written in a method of a class is a local variable, the difference:

Scope

Member Variable: The scope is the entire class

Local variables: Action within a method or within a statement

Default value

Member variable: has default value (null,0,0.0)

Local variable: No default value, no assignment cannot be used

Memory location

Member variables: Following objects into the heap memory for storage

Local variables: Follow your own method into the stack memory

Life cycle

Member variables: Follow objects, store in heap memory, wait for JVM cleanup, life cycle is relatively longer

Local variables: Follow the method, as long as the method out of the stack, there is no, the life cycle is relatively shorter

Object-oriented three major features: encapsulation, inheritance, polymorphism

Package Performance:

Method is one of the most basic packaging

Class is actually a package

Features of the package:

1. Improved reusability of the code

2. Hide the implementation details, but also provide access to the external means to facilitate the use of callers

3. Improved security

Examples of encapsulation realities:

A computer case, which has CPU, memory, hard disk and other components, if scattered outside, there will be many unsafe factors, so put them in the chassis, and provide a variety of sockets for people to use

Private keyword:

Define a class:

 Public class Person {    // People's name age, member variable     String name;     int Age ;     // speech function     Public void speak () {        System.out.println (age + "old" +name+ "speaking");}    }

Test:

 Public class persontest {    publicstaticvoid  main (string[] args) {        new person ();         = -200;         = "Zhang San";        P.speak ();    }}

After running:

Unreasonable, so we think, if you can't modify the age variable, this problem can be solved, and then have the private keyword

Members that are modified by private, can only be used in their own class

You can provide public access to private members: methods

 Public classPerson {//person's name age, member variableString name; Private intAge ; //define methods, assign values to age     Public voidSetage (inta) {if(A < 0 | | a > 100) { age= 20; } Else{ Age=A; }    }        //define method, get Age     Public intGetage () {returnAge ; }        //speech function     Public voidspeak () {System.out.println ( age+ "old" + name + "Speaking"); }}
 Public class persontest {    publicstaticvoid  main (string[] args) {        new person ();        P.setage (+);        System.out.println (P.getage ());         = "Zhang San";        P.speak ();    }}

Java Learning Note 8 (object-oriented one)

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.