JAVA basics/Lesson 24th: What does THIS mean in classes and objects/JAVA? Passing parameter xiangjie and java xiangjie

Source: Internet
Author: User
Tags print object

JAVA basics/Lesson 24th: What does THIS mean in classes and objects/JAVA? Passing parameter xiangjie and java xiangjie

1. the keyword "this" is equivalent to "I" in Mandarin, for example:

When James says "I have eaten", "I" represents James
When Xiaohong says "I have eaten", "I" represents Xiaohong.
"I" represents the current character.

1. this indicates the current object:

Public class Hero {String name; // float hp; // float armor; // armor int moveSpeed; // moving speed // print the virtual address in the memory public void showAddressInMemory () {System. out. println ("print the virtual address this saw:" + this);} public static void main (String [] args) {Hero garen = new Hero (); garen. name = "galun"; // print the object directly. The virtual address of the object in the memory is displayed. // format: Hero @ c17164 c17164: virtual address, the obtained address is not necessarily the same as that of System. out. println ("the virtual address seen by the Print Object:" + garen); // call showAddressInMemory to print this of the object and display the same virtual address garen. showAddressInMemory (); Hero teemo = new Hero (); teemo. name = "Timo"; System. out. println ("the virtual address seen by the Print Object:" + teemo); teemo. showAddressInMemory ();}}

2. Access the attributes of an object using the this keyword:

Public class Hero {String name; // float hp; // float armor; // armor int moveSpeed; // moving speed // The same parameter name and attribute name // In the method body, only the parameter name public void setName1 (String name) {name = name;} can be accessed ;} // to avoid problems in setName1, the parameter name must use other variable names public void setName2 (String heroName) {name = heroName ;} // use this to access the public void setName3 (String name) {// name indicates the parameter name // this. name indicates the property name this. name = name;} public static void main (String [] args) {Hero h = new Hero (); h. setName1 ("teemo"); System. out. println (h. name); h. setName2 ("garen"); System. out. println (h. name); h. setName3 ("Dead song"); System. out. println (h. name );}}

3. Use this to call other constructor methods:

To call another constructor in one constructor, you can use this ()

Public class Hero {String name; // float hp; // float armor; // armor int moveSpeed; // moving speed // The construction method with one parameter public Hero (String name) {System. out. println ("constructor of a parameter"); this. name = name;} // constructor with two parameters: public Hero (String name, float hp) {this (name); System. out. println ("constructor of Two Parameters"); this. hp = hp;} public static void main (String [] args) {Hero teemo = new Hero ("Timo", 383); System. out. println (teemo. name );}}

2. There are two types of variables: basic type and class type. parameters are also variables. Therefore, passing parameters can be divided into basic type parameters and class type parameters:

1. input parameters of the basic type:

The basic type parameter outside the method cannot be modified within the Method

Public class Hero {String name; // float hp; // float armor; // armor int moveSpeed; // movement speed public Hero () {}// blood return public void huixue (int xp) {hp = hp + xp; // after blood return, the blood bottle = 0 xp = 0;} public Hero (String name, float hp) {this. name = name; this. hp = hp;} public static void main (String [] args) {Hero teemo = new Hero ("Timo", 383); // blood bottle, the value is 100 int xueping = 100; // Timo uses this blood bottle to restore teemo. huixue (xueping); System. out. println (xueping );}}

2. Reference and =

If a variable is of the basic type, such as int hp = 50, we directly take over the variable named hp, and = indicates the meaning of the value assignment.
If a variable is of the class type, for example, Hero h = new Hero (); we call h a reference.

= No longer the meaning of the value assignment, = indicates the meaning of the point:

For example, Hero h = new Hero (); References h and points to a Hero object.

3. Passing parameters of the class type:

The class type is also called reference.
The references of row 24th are different from those of row 17th hero.
By calling garen. attack (teemo, 100); both references point to the same object
Therefore, the hpvalue of this object is changed in Row 3 hero. hp = hero. hp-damage;
Therefore, in Row 3, The hpvalue of the object is the changed value.

Public class Hero {String name; // float hp; // float armor; // armor int moveSpeed; // public Hero (String name, float hp) {this. name = name; this. hp = hp;
} // Attack a Hero and let him lose the blood of the damage point public void attack (hero, int damage) {Hero. hp = hero. hp-damage;} public static void main (String [] args) {Hero teemo = new Hero ("Timo", 383); Hero garen = new Hero ("galun ", 616); garen. attack (teemo, 100); System. out. println (teemo. hp );}}

 

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.