Java learning notes 8 (Object-Oriented 1) and java learning notes

Source: Internet
Author: User

Java learning notes 8 (Object-Oriented 1) and java learning notes

Process-oriented thinking: How can I solve this problem when I encounter a problem? Then solve the problem step by step.

Object-oriented Thinking: When you encounter one thing, think about who should do it. As for how to do it, it is not something I need to consider. Just do it well at last.

 

Practical example: we want to build a new computer

Process-oriented: We query the cost-effectiveness of various hardware parameters online, go to the mall to observe the inquiry, and then analyze the most appropriate, buy a variety of accessories and assemble them by yourself

Object-oriented: ask an expert who knows how to assemble a computer and tell him that I want a new computer.

 

Benefits of object-oriented:

1. More in line with people's thinking habits

2. Object-oriented is more like a conductor, while process-oriented is like executor.

3. Object-oriented thinking can simplify complex problems

4. All things in the world are objects

 

Example: describes a real car in a class

Define a car:

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

Running car:

Public class CarTest {public static void main (String [] args) {Car c = new Car (); c. color = "black"; c. count = 3; c. run ();}}

After running:

 

Object Memory analysis:

1. Run the main method in the stack.

2. Create a Car class, open a space in the heap memory, store the new object, the class member variables follow into the heap, and assign the default value (null, 0)

3. The memory address is passed to the referenced variable c, that is, c points to the object in the memory.

4. The color and count variables in the memory pointed to by c are modified.

5. The method run is called by Object c and executed in the stack.

6. run the method and release the stack memory.

7. The main method ends and the stack memory is released.

8. The c variable disappears, objects in the heap memory are cleared, and the memory is released.

 

 

Relationships between classes and objects:

Class: abstract description of a certain type of thing

Object: used to represent the individual of a thing in reality

Class is used to describe the common features of multiple objects, which is equivalent to a template.

An object is created through a class. A class can correspond to multiple objects.

 

The variables written in the class are member variables, and the variables written in the class method are local variables. Differences:

Scope

Member variable: The function scope is the entire class.

Local variables: Apply to methods or statements.

Default Value

Member variables: default values (null, 0, 0.0)

Local variable: no default value. It cannot be used if no value is assigned.

Memory location

Member variable: follows the object into the heap memory for storage.

Local variable: Follow your method into stack memory

Lifecycle

Member variable: follows the object and is stored in the heap memory. Wait for the JVM to clean up and the lifecycle is relatively longer.

Local variable: follows the method. As long as the method goes out of the stack, it does not exist and the lifecycle is relatively shorter.

 

 

Three features of object-oriented architecture: encapsulation, inheritance, and Polymorphism

Encapsulation:

The method is the most basic encapsulation body.

The class is actually an encapsulation body.

Encapsulation features:

1. Improved code reusability

2. The implementation details are hidden and accessible to the outside world to facilitate the use of callers.

3. Improved security

 

Examples of encapsulation:

A computer chassis contains CPU, memory, hard disk, and other components. If they are scattered outside, there will be many insecure factors. Therefore, they are placed in the chassis and various plug-ins are provided for people to use.

 

Private keywords:

Define a class:

Public class Person {// age of the Person's name, member variable String name; int age; // public void speak () {System. out. println (age + "years old" + name + "talking ");}}

Test:

Public class PersonTest {public static void main (String [] args) {Person p = new Person (); p. age =-200; p. name = "James"; p. speak ();}}

After running:

Unreasonable, so we thought that if we could not modify the age variable, this problem could be solved, so we had a private keyword.

Private-modified members can only be used in their own classes.

You can provide public access methods for private members:

Public class Person {// age of a Person's name, member variable String name; private int age; // definition method, assign public void setAge (int a) to age) {if (a <0 | a> 100) {age = 20 ;}else {age = a ;}// define a method to get age public int getAge () {return age;} // public void speak () {System. out. println (age + "years old" + name + "talking ");}}
Public class PersonTest {public static void main (String [] args) {Person p = new Person (); p. setAge (80); System. out. println (p. getAge (); p. name = "James"; p. speak ();}}

 

Related Article

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.