/**
* Created by Administrator on 2017/3/7.
*/
/**
* @author Zhenghong
* @date 2017-03-07-21:21
**/
public class TESTC {
Public static void main (String []args)
{
New Graph (5);
}
}
Class GRP
{
void Draw ()
{
System.out.println ("Draw ....");
}
GRP ()
{
Draw ();
}
}
Class Graph extends GRP
{
private int radius = 2;
Graph (int radius) {
This.radius = radius;
}
@Override
void Draw () {
System.out.println ("Draw.." +radius);
}
}
Result Draw ... 0
Initializes the graph, initializes the parent class, has an overloaded method in the parent constructor method, and then calls the child class's draw () method, and prints the result as draw: 0, because the subclass radius has not been initialized at this time.
Thinking in Java Page 299
The guidelines for writing constructors use the simplest possible method of getting objects into normal state, and avoid invoking other methods if you can.
The only method that can be safely called in the constructor is the final method in the base class. (also applies to private methods, which automatically belong to the final method)
Java constructor Internal Polymorphism method