JAVA basics/Lesson 22nd: classes and objects/What are references in JAVA? Inherit ?, Java inheritance

Source: Internet
Author: User

JAVA basics/Lesson 22nd: classes and objects/What are references in JAVA? Inherit ?, Java inheritance

1. The concept of reference. If a variable is of the class type rather than the basic type, this variable is also called a reference.

1. Reference and point:

new Hero();

Creates a Hero object.
But it only creates an object and cannot be accessed.
To access this object, a reference is used to represent this object.

Hero h = new Hero();

H. This variable is of the Hero type and is also called a reference.
= Indicates h. This reference represents the object created on the right.
"Representation" is also called "pointing" in object-oriented systems"

Public class Hero {String name; // float hp; // float armor; // armor int moveSpeed; // public static void main (String [] args) {// create an object new Hero (); // use a reference to point to this object Hero h = new Hero ();}}

2. Multiple references, one object:

There are multiple references, but only one object exists.
In this example, all references point to the same object.
The object is like "Real Estate", and the reference is like "real estate license"
There can be multiple copies of the real estate license, but the real "Real Estate" only has one

Public class Hero {String name; // float hp; // float armor; // armor int moveSpeed; // public static void main (String [] args) {// use a reference to point to this object Hero h1 = new Hero (); Hero h2 = h1; // h2 points to the object Hero h3 = h1 pointed to by h1; hero h4 = h1; Hero h5 = h4; // five references of h1, h2, h3, h4, and h5 all point to the same object }}

3. One reference with multiple objects:

Row 3: Reference garen to point to the newly created object (Object 1)
Row 3 points the same reference garen to the newly created object (Object 2)
At this time, object 1 has no reference pointing
In other words, the object becomes meaningless without any means to control and access it. 2. In LOL, a weapon is an item and has a name and a price. Therefore, when designing a weapon, you can let the weapon inherit the item and inherit the name and price attributes.1. Item categories have attributes name and price.
public class Item {    String name;    int price;}

2. Weapons: Weapon does not inherit Item writing
The name and price attributes are designed independently, and a damage attack force attribute is added at the same time.

Public class Weapon {String name; int price; int damage; // attack capability}

3. This time Weapon inherits Item
Although Weapon does not design the name and price, it inherits the Item class and has the attributes of name and price.

Public class Weapon extends Item {int damage; // attack force public static void main (String [] args) {Weapon infinityEdge = new Weapon (); infinityEdge. damage = 65; // The value of the damage attribute is the infinityEdge newly designed in the Weapon class. name = "Endless edge"; // The name attribute is inherited from the Item and does not need to be designed repeatedly. price = 3600 ;}}

package charactor; public class Hero {    public String name;    protected float hp;     public static void main(String[] args) {           Hero garen =  new Hero();           garen =  new Hero();    }}

 

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.