2013年9月15日星期日

java subclass can call the parent class method ?

Today to see the effective java Article 14 , which calls the parent class subclass method previously learned C + +. For this example has been not understand , experts hope to explain . . .




public class SuperTest {
public static void main(String[] args) {
Child c = new Child();
c.addAll();
}
}

class Parent{
public void add(){
System.out.println("A add...");
}

public void addAll(){
System.out.println("A add ALL...");
this.add();
}
}

class Child extends Parent{

@Override
public void add() {
// TODO Auto-generated method stub
System.out.println("B add...");
super.add();
}

@Override
public void addAll() {
// TODO Auto-generated method stub
System.out.println("B add ALL...");
super.addAll();
}

}





final output result is
B add ALL ...
A add ALL ...
B add ...
A add ...


According to my understanding , super.addAll () method call to Parent in addAll (), and Parent in addAll () call to add (). Here's add the final call is Child 's add (), since it is super.addAll (), why even call the last subclass add () method . . .

really confused , forget expert answers down . . Thank
------ Solution ------------------------------------- -------
this.add ();
where this refers to the current object, that is Child c = new Child (); in c.
This should get the point !
------ Solution ---------------------------------------- ----

public class SuperTest {
    public static void main(String[] args) {
        Child c = new Child();
        c.addAll();
    }
}
 
class Parent{
    public void add(){
        System.out.println("A add...");
    }
     
    public void addAll(){
        System.out.println("A add ALL...");
        //加上下面这句话 你的世界就变的清晰了  肯定就明白了
        System.out.println(this.getClass().getName()); // 打印出来Child 你难道还会不明白么?
        this.add();
    }
}
 
class Child extends Parent{
 
    @Override
    public void add() {
        // TODO Auto-generated method stub
        System.out.println("B add...");
        super.add();
    }
 
    @Override
    public void addAll() {
        // TODO Auto-generated method stub
        System.out.println("B add ALL...");
        super.addAll();
    }
     
}


------ For reference only ---------------------------------- -----
add method is overridden
------ For reference only ------------------------- --------------
when the variable compile-time and run-time class item type is not the same access it through the variable referenced object's instance variables , the instance variable's value by the declare the variable type determines ; but through the variable reference object calling it an instance method , the method of the actual behavior of the object referenced by him to decide.
------ For reference only -------------------------------------- -
this, used to represent an object. While only Child is instantiated , so this is the Child.
Another:
When instantiating Child java time, will look for Parent class files , and Child , after the merger be loaded into the virtual machine.
At this time, the class will be retained son relationship , but the virtual machine think this merger is a complete class file Child.
------ For reference only -------------------------------------- -
My understanding is : c.addAll () -> System.out.println ("B add ALL ..."); super.addAll (); ->
System.out.println ("A add ALL ..."); this.add (); case this reference is instantiated c, so this.add () is equivalent to c.add () -> System.out . println ("B add ..."); super.add (); -> System.out.println ("A add ...");
sequentially output to the console

------ For reference only ------------------ ---------------------
super.addAll () method inside this.add () is to call the current object add () method , the current object is a child.

subclass call the parent class method sometimes really confusing , especially in the base class and subclass constructor method of retrofitting it is not the default .
Memory landlord to blog column facie parent class method calls the subclass to explain it.
------ For reference only -------------------------------------- -

public void addAll() {
        // TODO Auto-generated method stub
        System.out.println("B add ALL...");
        super.addAll();
}
相当于
public void addAll() {
        // TODO Auto-generated method stub
        System.out.println("B add ALL...");
        System.out.println("A add ALL...");        
        this.add();
}

this refers to the current sub-class object , there is no change
------ For reference only ----------------------- ----------------
this is who ? who called the current method , this is it
Child c = new Child ();
c.addAll ();

1, new a child
2, call the child in the addAll ()
3, syso "B add ALL ..."
4, skip to the parent class addAll ()
5, syso "A add ALL ..."
6, skip subclasses add () / / because the child class to call the parent class so this represents a subclass
7, syso "B add ..."
8, jump to the parent class add ()
9, syso "A add ..."
End
------ For reference only --------------------------------- ------
this represents the current object , super on behalf of the parent class
------ For reference only ------------------ ---------------------
java in this represents the current object , super on behalf of the parent class
------ For reference only ---------------------------------------
just a reference ,
------ For reference only ---------------------------------------
methods are covered ,
------ For reference only ------------------------------ ---------
not use this use the super
------ For reference only ------------------- --------------------
learned never learned C + + virtual keyword ? Same reason
------ For reference only ------------------------------------ ---
Yes, of course .

reference design pattern template method
------ For reference only -------------------------- -------------


What if I just let super.addAll () method call their own add method it ? By such analysis , it is the parent class method addAll add method is not this.add but super.add it. . . but the parent class does not super.add () Is there any other way to do ?
------ For reference only -------------------------------------- -

  
What if I just let super.addAll () method call their own add method it ? By such analysis , it is the parent class method addAll add method is not this.add but super.add it. . . but the parent class does not super.add () Is there any other way to do ?  

You seem to be around the inside , you subclass overrides the superclass method , create a sub- class of objects , a subclass can call their own methods and non private parent class method rather now you want to call the parent class parent class method can only create the parent object of the class to call

没有评论:

发表评论