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