2013年10月5日星期六

Neutron java class inherits the parent class constructor do ?

Look java2 practical tutorial on writing a subclass can not inherit the parent class constructor
Baidu, was said to inherit , some people say no inheritance
himself wrote some tried

class A {
A() {
System.out.println("调用了");
}
}

class B extends A {
    B(){
        System.out.println("no");
    }
}

public class E {
  public static void main(String[] args) {
  B b = new B(); //OK
  }
}

run:
call
no
is this call to you, in the end how is it ? Can you specifically talk about , thank you
------ Solution -------------------------------- ------------
inheritance , the subclass is instantiated to generate objects, put this class is loaded into the JVM, will the President into instances of classes , but the new keyword has a special nature that will first call the class constructor with no arguments , if this class inherits the parent class, sub-class is instantiated , it will first call the superclass constructor with no arguments , so here B b = new B ( ) in the process, will first call the a class constructor with no arguments
------ Solution ------------------------ --------------------
first of all, one that is not inherited , only overwrite or overloading, classes and inheritance between classes can be called .
Secondly, you appear in effect, is like an initialization order problems , sub-class in the initialization time will first call the parent class constructor , so you played that sentence .
Finally , the subclass can use the super () call the parent constructor shown , there is no need to rewrite .
------ Solution ---------------------------------------- ----
sub-class does not inherit the parent class constructor method, but the object is instantiated subclass : If the child does not show the class constructor calls the superclass method , it will automatically call the super class default constructor makers. If no superclass constructor with no arguments , and the subclass constructor calls super class was not any show other constructors , the Java compiler will report an error (java core technologies P146).
------ For reference only -------------------------------------- -
this is called ah
------ For reference only ----------------------------- ----------
not inherit the parent class constructor ; however able to use super () call
------ For reference only --------- ------------------------------
is like that.

------ For reference only ---------------------------------- -----
book says : sub-class can not be inherited from the parent class constructor , so if you want to use the subclass constructor of the parent class , subclass constructor must be used in the method , and use the keyword super representation , and super subclass constructor must be the first statement
------ For reference only ----------------------- ----------------
inherited just like our reality almost father-son relationship , to have a child object then you have to first have a father ( will call the parent class constructor ) , it will run a parent constructor. Another if you want to call the parent class constructor with parameters , that it is through the super keyword to call ( 6th floor said the way ) . Constructors are not inherited , think if they can not inherit that children can construct father. ( Do not they understand that the fine thank batch )
------ For reference only --------------------------- ------------
god Ma ah ! First , the landlord 's understanding is problematic .
here that can not inherit is to say, the constructor can not be overridden in a subclass .

here that inherits the parent class constructor is said subclass constructor method calls the parent class constructor.
------ For reference only -------------------------------------- -
learn, popular use . . . .
------ For reference only ---------------------------------------
answer is: call
invocation: if the child class does not override the parent class constructor , the default call the parent class 's default constructor ( ie the parent class constructor with no arguments ) , which means that if the parent class constructor has parameters will wrong.
You can try this: ( error code )
class A {
A (String a) {
System.out.println (a);
}
private String a = " call " ;
}
class B extends A {
B () {
System.out.prntln ("no");
}
}
you must do :
class A {
A (String a) {
System.out.println (a);
}
private String a = " call " ;
}
class B extends A {
B (String a) {
super (a);
System.out.prntln ("no");
}
private Stirng a = " call " ;
}
------ For reference only --------------------------------- ------
see upstairs above described point I am not critical pressure ah
LZ reason you feel subclass inherits the parent class constructor because subclasses there are hidden super (), which is present in the system , but can be omitted , because it calls the parent class constructor so it shows the main parent class constructor.
If the parent class does not have no-argument constructor , write super () definitely wrong ah , to write super ( parameters ) , to clear something up . Understand what

没有评论:

发表评论