Java value class for the doubts

Source: Internet
Author: User

The so-called value class is the class whose instance represents a value.

Look at the two value classes below, one representing a point on a plane and another representing a point with color.

<span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class ColorPoint extends Point{private final String color;public colorpoint (int x,int y,Str ing color) {super (x, y);//(2) Chain to point constructor This.color = color;//initialize blank Final-too late}public String Makename () {return super.makename () + ":" + color;//(4) Execute before subclass constructor body}public static void main (St Ring[] args) {System.out.println (new ColorPoint (4,2, "purple"));//(1) Incoke subclass Constructor}}class Point{private Final int x,y;private final String name;public point (int x,int y) {this.x = X;this.y = Y;name = Makename ();//(3) Invoke SUBC Lass method}protected String Makename () {return "[" +x+ "," +y+ "]";} Public String toString () {return name;}} </span>

What is the final output?

[4,2]:null

It can be understood through the execution route indicated above

Be aware that:

1. Polymorphism

If a subclass overrides a method in the parent class, the subclass method is called when called to the function, even if it is called in the parent class instance.

2. Fields

When an instance field is assigned a value, there is a possibility to take its value.

Tips:

The constructor does not call the Quilt class override method.

Extended:

<span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class ColorPoint extends Point{private String color;public colorpoint (int x,int y,String CO LOR) {super (x, y); this.color = color;} Public String Makename () {return super.makename () + ":" + Color;} public static void Main (string[] args) {System.out.println (new ColorPoint (4,2, "purple"));}} Class Point{private int x,y;private String name;public point (int x,int y) {this.x = X;this.y = Y;name = This.makename (); if ( This instanceof ColorPoint) {System.out.println ("ColorPoint");} ElseSystem.out.println ("point");} Protected String Makename () {return "[" +x+ "," +y+ "]";} Public String toString () {return name;}} </span>

ColorPoint

[4,2]:null

Java value class for the doubts

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.