OOP of 02_JavaSE -- object-oriented (Object Memory diagram, anonymous object, this), 02_javaseoop --

Source: Internet
Author: User

OOP of 02_JavaSE -- object-oriented (Object Memory diagram, anonymous object, this), 02_javaseoop --

Object-oriented (2)

I. Status of objects in memory

1. the following code is used as an example:

1 class Demo {2 3 public static void main (String [] args) {4 // create object 5 Person p = new Person (); 6 7 // call the property and assign a value of 8 p. name = "Li Bai"; // the name of a person is assigned 9 p. age = 49; // age value of a person is 10 11 // call the learning method (call behavior) 12 p. study (); 13 14} 15 16} 17 18 // 19 class Person {20 // member variable 21 String name; // name 22 int age; // age 23 24 public void study {// The person is studying 25 systems. out. println ("+ age +" + name + "Learning"); 26} 27}

 

Specific execution method,

 

Ii. Anonymous objects

Next I will talk about it with a simple understanding, but I have not studied it in depth.

 1. What is an anonymous object?

  The so-called anonymous object is an object without a name. What is an object with a name? For example, Person p = new Person (). This is an object with a name. Its name is p. New Person () is an object without a name.

  2. What are the application scenarios of anonymous objects and how can they be used?

  Scenario:Anonymous objects are used only once. So what are the benefits of him? Save code.

Anonymous objects can also be passed as parameters. For example, setXXX (new Person) is a method ).

At the same time, it should be known that after a call, the anonymous object is garbage and will be recycled by the garbage collection mechanism.

  Usage:The above code is used as an example.

1 Person p = new Person (); 2 p. study (); 3 4 // call 5 new Person (). study () for an anonymous object ();

 

Iii. usage of this

 This is a keyword that represents the reference of the current object. That is, the current object.

Before officially speaking about this, let's take a look at the private keyword.

What is private? On the surface, it means private. For the sake of security, the attributes of the class cannot be modified at Will (for security reasons). Therefore, keywords such as private, protect, default, and public are available. Private is private. We cannot assign values directly by creating objects p and p. name. How can we assign values. At this time, we have to provide a method inside the class to assign values to it, and provide a get method, that is, the set/get method.

1 public class Person {2 // member variable 3 String name; // name 4 int age; // age 5 6 // set and get Method 7 public String getName () {8 return name; 9} 10 11 public void setName (String name) {12 this. name = name; 13} 14 15 public int getAge () {16 return age; 17} 18 19 public void setAge (int age) {20 this. age = age; 21} 22} 23 24 class Demo2 {25 public static void main (String [] args) {26 Person p = new Person (); 27 p. setAge (1); 28 p. setName ("Su Shi"); 29 30} 31}

 

  At this time, this indicates the reference of the current object. this. name is the name of the member variable of Person. It is used to distinguish between member variables and local variables.

 

 

If any error occurs, please correct it.

Email: it_chang@126.com

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.