2013年11月18日星期一

A magical phenomenon , what is the intention of playing it ? Or fate dictates - subclass inherits the methods of the parent class (ps: really does get confused )


class Base
{
    private String showMe()
    {
        return "Base";
    }

    public void print()
    {
        System.out.print(showMe());
    }
}

public class Sub extends Base
{
    public String showMe()
    {
        return "Sub";
    }

    
//    public void print()
//    {
//        System.out.print(showMe());
//    }
    
    public static void main(String[] args)
    {
        Sub sub = new Sub();
        sub.print();
    }
}

uncomment the comment section , try the outcome ?
------ Solution ---------------------------------------- ----


according to the test results should be just a reference
itself does not follow exactly the same way as the parent class
or inherited method does not show is this

public void print()
    {
        super.print();
    }


but do not know if this is what I understand
specific java inheritance will have to see is how to achieve it ? wait downstairs Daniel answered
------ Solution ------------------------------ --------------

public static void main(String[] args)
{
    Sub sub = new Sub(); 
    sub.print(); 
    /*
     sub 自己没有print所以调用的是Base 的print,因为继承了Base。
     Base 的print 方法调用的showome() 是this.showme(),
     Base中的showme 是private 的,不能被重写,所以不会检查子类有没有重写直接调自身的showme()
     ------
     去sub的print注释后,调用shome 直接就是调用自己的print 压根就跟继承没多少关系
     因为你使用的是Sub sub = new Sub(); 非 Base sub = new Sub();
     -----
     试一下把Base 的showme() 改为public ,其它维持原状,相同的程序就会打印sub了。
     调用Base 的print --> print 调用this.showme() ,因为new 的是Sub,而且showme 是public是可被重写的
     就会检查Sub有没有重写showme ,发现重写了调用Sub的showme
     */
}

------ Solution -------------------- ------------------------
looked at , the landlord always wanted to do not understand is the method override , recommended a good understanding about it.

subclass appear exactly the same with the parent class method , there will be an overwrite operation , also known as rewritten or replication.
subclass private method can not be overridden.
overridden method in a subclass , the continued use of overridden methods through super. function name obtained .
------ Solution ---------------------------------------- ----
individuals a guess: the inheritance of the parent in the child 's class at compile java into . java byte code at compile time when the method
will first determine the methods inside the variable modifiers if private ( example, here 's private String showMe () returns a string as can be seen as the variable bar ) directly in the parent class compiled
If the method is not inside the private variable modifier is placed subclass compiled
did not look at the bottom of the compilation process so please correct me guess the next
------ For reference only ------------ ---------------------------
seeking great God, and explain why the outcome will be ?
------ For reference Why only ---------------------------------------
shown in subclasses parent class inherited methods and do not show such a result the difference ?
------ For reference only -------------------------------------- -
is not a rewrite of a rewrite do not
------ For reference only --------------------- ------------------

which overrides which did not rewrite ?
------ For reference only -------------------------------------- -
annotated output is not overridden base
uncomment rewritten output is sub

------ For reference only ---------------------------------- -----
Why would rewrite the first print () can be inherited , and secondly, I did not change from the parent class print () in any way !
Is it just show up on behalf of the rewrite yet ?
------ For reference only -------------------------------------- -
1. uncommented , output : Base.
2. release notes, output : Sub.
showMe parent class method , private properties means can not be inherited . Therefore, the main function , the subclass method showMe quoted are subclasses own . But the print is the public, so subclasses can inherit the parent class .
So, no comment, subclass references print method of the parent class , parent class print method is called the Father of his showMe, namely printed base.
release notes after the subclass will print method calls itself showMe methods, namely printed sub. .
I have not tried , but the reasoning that, if really wrong , the landlord when I have not. . .
------ For reference only -------------------------------------- -
layer Lord " so, no annotations . . ." here should be " annotated when " it should be wrong !
I create and debug it, your reasoning is correct, it is indeed run in this way , but still to explain :
Why I inherited method appears intact will impact on the results ?
------ For reference only -------------------------------------- -
In fact, I was wrong, and my colleague is right , private it is not inherited , but can not be accessed by subclasses . . My question. .
------ For reference only -------------------------------------- -
then what ? Why would a method inherited intact will show an impact on the results ?
------ For reference only -------------------------------------- -
again, if private be inherited , but inherited not be used, then why should inherit ?
------ For reference only -------------------------------------- -
first private can not be inherited

annotation
because subclass inherits the parent class print method
So sub.print ();
At this point in a subclass called like this

 public void print()
    {
        super.print();
    }

actually call the parent class print method
output is the base

After removing comments

subclasses override the parent class print method
sub.print (); calling a subclass own methods
output is base


In fact, this is similar to

Class A {
  public static void main(String[] args) {
       A a = new A();
       System.out.println(a.toString()); 
  }
}

This output is the address of a

When A after rewriting the toString method

Class A {
  private String toString() {
   return "a";
  }

  public static void main(String[] args) {
       A a = new A();
       System.out.println(a.toString()); 
  }
}


the output is a

rewriting is a subclass method name , return type, keep the parent class parent class unanimously agreed to keep the scope or extensive than the parent class

So the landlord when sub subclass uncomment clearly overrides the parent class method
------ For reference only ------ ---------------------------------

1, first of all there is no change in the parent class print ( ) method content, just show up , this is a rewrite ?
2, followed by the no will print () is displayed, which is commented out print () , a sub-class is definitely inherited the print () method , then what is this so-called inherited a reference to the parent class , or itself have real code to write a print () method in the interface just did not show it ?
------ For reference only -------------------------------------- -

  
according to the test results should be just a reference   
itself does not follow exactly the same way as the parent class   
or inherited method does not show is this   
  

public void print()
    {
        super.print();
    }
  
  
but do not know if this is what I understand   
specific java inheritance will have to see is how to achieve it ? Daniel waiting downstairs to answer   constructor inheritance is !
------ For reference only -------------------------------------- -


constructor , then
is new when the first call to a subclass of the parent class constructor and then call your own constructor do ?

seemingly not the same , right ?
------ For reference only ---------------------------- -----------

  
constructor , then   
is new when the first call to a subclass of the parent class constructor and then call your own constructor do ?   
  
seemingly not the same , right ?   I said in another post inside the order, you can look at :
http://bbs.csdn.net/topics/390635323?page=1 # post-396093690
I mean, the default constructor , if not stated subclass out whether the call should also be a reference to the parent class !
------ For reference only -------------------------------------- -
seemingly upstairs explain clearly points than I

learn
------ For reference only -------------------------------- -------
I understand your idea, but there is another question is: Why is the print () in the subclass does not move completely written out , it becomes a sub-class custom method of rather than inherited from the parent class method ?
------ For reference only -------------------------------------- -
to sub the print notes , call shome directly is to call your own print are they just do not have much relationship inheritance
because you are using a Sub sub = new Sub (); non- Base sub = new Sub ();

Sub sub = new Sub (); / / This is the parent class does not exist subclass relationship , declare that Sub, Sub is instantiated
sub.print (); / / First check that they have no print, if there is a direct call your own print. Before the print will call the parent class because he did not Sub print, so go see if there is print the parent class

------ For reference only ---------------------------------- -----
inheritance should only be a reference to the parent class method
I drew a map subclass relationship with the parent class should be like

------ For reference only ----- ----------------------------------
when you call sub.print () when subclass does not have this way it will go to the parent class
like to call the parent class upstairs showMe () when it is not inherited because showMem direct output base
------ For reference only ---- -----------------------------------
I know what you mean , however. . . My question is : Why is displayed print () becomes a subclass defines its own way , while not displayed or inherited method , just a " show " will be able to produce such a result ?
If print () is a sub- class of its own display method defined , then the subclass inherited from the parent class print () method which went ?
------ For reference only -------------------------------------- -
that the print () displays whether the parent class method overrides ?
------ For reference only -------------------------------------- -
that the print () displays whether the parent class method overrides ?  

is a subclass override print () method after subclass will have a print ()
When calling sub.print () can be found when subclass print () method on the direct implementation of the
then call the subclass itself showMe () on the output sub a
------ For reference only ------------------- --------------------
subclasses override print () can not be said to be displayed
because the method does not hide but this method is not a subclass
------ For reference only ----------------- ----------------------
that the print () displays whether the parent class method overrides ?          
  
is a subclass override print () method after subclass will have a print ()   
When calling sub.print () can be found when subclass print () method on the direct implementation of the   
then call the subclass itself showMe () on the output of the sub   That is to say , after no longer override the parent class print () method with references ?
------ For reference only -------------------------------------- -
had cut off after a reference to the parent class , the so-called burning bridges ?
------ For reference only -------------------------------------- -
that the print () displays whether the parent class method overrides ?                
    
is a subclass override print () method after subclass will have a print ()     
When calling sub.print () can be found when subclass print () method on the direct implementation of the     
then call the subclass itself showMe () on the output of the sub        That is to say , after no longer override the parent class print () method with references ?  

rewrite adopted after sub.print () is not visit parent class method
only through super.print () to access

------ For reference only ---------------------------------- -----
that the print () displays whether the parent class method overrides ?                      
      
is a subclass override print () method after subclass will have a print ()       
When calling sub.print () can be found when subclass print () method on the direct implementation of the       
then call the subclass itself showMe () on the output of the sub            That is to say , after no longer override the parent class print () method with references ?          
  
rewrite adopted after sub.print () is not visit parent class method   
only through super.print () to access   
  you change the code :
Sub sub = new Sub ();
read:
Base sub = new Sub ();
will find the same result !
------ For reference only -------------------------------------- -
had cut off after a reference to the parent class , the so-called burning bridges ?  

This can not be called " quote " I do not know
I did not feel before rewriting print () is part of the parent class
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class
after she had rewritten print () do not need to go looking for the parent class

------ For reference only ---------------------------------- -----
had cut off after a reference to the parent class , the so-called burning bridges ?          
  
This can not be called " quote " I do not know   
I did not feel before rewriting print () is part of the parent class   
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class   
after she had rewritten print () do not need to go looking for the parent class   
  There are no other thoughts can only agree with this knowledge !
------ For reference only -------------------------------------- -
had cut off after a reference to the parent class , the so-called burning bridges ?                
    
This can not be called " quote " I do not know     
I did not feel before rewriting print () is part of the parent class     
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class     
after she had rewritten print () do not need to go looking for the parent class     
       There are no other thoughts can only agree with this knowledge !  

so you have other large cattle to doubts ah in the end is not the case

for the hair did Daniel do ?
------ For reference only -------------------------------------- -
had cut off after a reference to the parent class , the so-called burning bridges ?                      
      
This can not be called " quote " I do not know       
I did not feel before rewriting print () is part of the parent class       
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class       
after she had rewritten print () do not need to go looking for the parent class       
           There are no other thoughts can only agree with this knowledge !          
  
so you have other large cattle to doubts ah in the end is not the case   
  
for the hair did Daniel do ?   I have discussed this problem before , because the layers of references cause java performance is low. But thank you very much , I'm sure you will as a good woman to become a Piece of men !
------ For reference only ---------------------------------------
been cut off after the parent references to a class , the so-called burning bridges ?                            
        
This can not be called " quote " I do not know         
I did not feel before rewriting print () is part of the parent class         
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class         
after she had rewritten print () do not need to go looking for the parent class         
               There are no other thoughts can only agree with this knowledge !                
    
so you have other large cattle to doubts ah in the end is not the case     
    
for the hair did Daniel do ?        I have discussed this problem before , because the layers of references cause java performance is low. But thank you very much , I'm sure you will as a good woman to become a Piece of men !     

I want to become a pirate queen of men !

------ For reference only ---------------------------------------
been cut off after the parent references to a class , the so-called burning bridges ?                                  
          
This can not be called " quote " I do not know           
I did not feel before rewriting print () is part of the parent class           
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class           
after she had rewritten print () do not need to go looking for the parent class           
                   There are no other thoughts can only agree with this knowledge !                      
      
so you have other large cattle to doubts ah in the end is not the case       
      
for the hair did Daniel do ?            I have discussed this problem before , because the layers of references cause java performance is low. But thank you very much , I'm sure you will as a good woman to become a Piece of men !               
  
I want to become a pirate queen of men !   
     Haha, afternoon continue to struggle , free Results posted !
------ For reference only -------------------------------------- -
had cut off after a reference to the parent class , the so-called burning bridges ?                                        
            
This can not be called " quote " I do not know             
I did not feel before rewriting print () is part of the parent class             
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class             
after she had rewritten print () do not need to go looking for the parent class             
                       There are no other thoughts can only agree with this knowledge !                            
        
so you have other large cattle to doubts ah in the end is not the case         
        
for the hair did Daniel do ?                I have discussed this problem before , because the layers of references cause java performance is low. But thank you very much , I'm sure you will as a good woman to become a Piece of men !                       
    
I want to become a pirate queen of men !     
               Haha, afternoon continue to struggle , free Results posted !  

Come Results posted . .
otherwise be withheld wages . . .
------ For reference only ---------------------------------------
been cut off after the parent references to a class , the so-called burning bridges ?                                              
              
This can not be called " quote " I do not know               
I did not feel before rewriting print () is part of the parent class               
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class               
after she had rewritten print () do not need to go looking for the parent class               
                           There are no other thoughts can only agree with this knowledge !                                  
          
so you have other large cattle to doubts ah in the end is not the case           
          
for the hair did Daniel do ?                    I have discussed this problem before , because the layers of references cause java performance is low. But thank you very much , I'm sure you will as a good woman to become a Piece of men !                               
      
I want to become a pirate queen of men !       
                       Haha, afternoon continue to struggle , free Results posted !          
  
Come Results posted . .   
otherwise be withheld wages . . .      see if there are more cattle to answer , Oh, leave the first waiting !
------ For reference only -------------------------------------- -
had cut off after a reference to the parent class , the so-called burning bridges ?                                                    
                
This can not be called " quote " I do not know                 
I did not feel before rewriting print () is part of the parent class                 
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class                 
after she had rewritten print () do not need to go looking for the parent class                 
                               There are no other thoughts can only agree with this knowledge !                                        
            
so you have other large cattle to doubts ah in the end is not the case             
            
for the hair did Daniel do ?                        I have discussed this problem before , because the layers of references cause java performance is low. But thank you very much , I'm sure you will as a good woman to become a Piece of men !                                       
        
I want to become a pirate queen of men !         
                               Haha, afternoon continue to struggle , free Results posted !                
    
Come Results posted . .     
otherwise be withheld wages . . .             see if there are more cattle to answer , Oh, leave the first waiting !  

large cattle Remind me down ha
------ For reference only ----------------------- ----------------
had cut off after a reference to the parent class , the so-called burning bridges ?                                                          
                  
This can not be called " quote " I do not know                   
I did not feel before rewriting print () is part of the parent class                   
subclass inherits the parent class is because the relationship between the call sub.print () is not found in the subclass print () to go to the parent class                   
after she had rewritten print () do not need to go looking for the parent class                   
                                   There are no other thoughts can only agree with this knowledge !                                              
              
so you have other large cattle to doubts ah in the end is not the case               
              
for the hair did Daniel do ?                            I have discussed this problem before , because the layers of references cause java performance is low. But thank you very much , I'm sure you will as a good woman to become a Piece of men !                                               
          
I want to become a pirate queen of men !           
                                       Haha, afternoon continue to struggle , free Results posted !                      
      
Come Results posted . .       
otherwise be withheld wages . . .                   see if there are more cattle to answer , Oh, leave the first waiting !          
  
large cattle Remind me down ha  
------ For reference only ---------------------------------------
see half feel upstairs interpretation has been largely very clear, I do not know what the landlord still drill horn ?
------ For reference only ---------------------------------------
in the tangle : Why display print ( ) method will produce different results , just show - the result is : shows subclass own custom print (), did not show when the parent object is a subclass of class print () method references , when full write a subclass of the parent class print () method, is to rewrite the method of the parent class , and there is no morality in reference to abandon the original print () method ! What do you think ?
------ For reference only -------------------------------------- -
into Base sub = new Sub (); After the result is the same

I think it is not engage in anti- ?

fact
call sub.print () when you first go to the parent class print () to see whether this approach is found after being rewritten
If being rewritten to call the child class method

seem to understand this , then you can pass the
------ For reference only ---------------------- -----------------
changed after the result is still the same ! As for the calling process I do not know, now head to a regiment paste , Oh !
------ For reference only -------------------------------------- -
now understood that the above burning bridges , you think not ?
------ For reference only -------------------------------------- -
changed after the result is still the same ! As for the calling process I do not know, now head to a regiment paste , Oh !  
is a bit messed up temporarily holds the previous view. .
------ For reference only -------------------------------------- -
changed after the result is still the same ! As for the calling process I do not know, now head to a regiment paste , Oh !          
is a bit messed up temporarily holds the previous view. .   eat well, grow up , ha ha !
------ For reference only -------------------------------------- -
changed after the result is still the same ! As for the calling process I do not know, now head to a regiment paste , Oh !                
is a bit messed up temporarily holds the previous view. .        eat well, grow up , ha ha !  

------ For reference only ---------------------------------------
now understood that the above burning bridges you think not ?   burning bridges so vivid metaphor , ah, but does not seem to comply with it. To be understood as , your son I have, you do not have the
------ For reference only ---------------------------------------
definitely sub the
------ For reference only ---------------------------------------

     public void print() {
     // TODO Auto-generated method stub
     super.print();
    }
//    public void print()
//    {
//        System.out.print(showMe());
//    }


Bibi does not automatically generate one on the line
- ----- For reference only ---------------------------------------
now that you understand the above burning bridges , you think not ?        burning bridges so vivid metaphor , ah, but does not seem to comply with it. To be understood as , your son I have, you do not have the     
------ For reference only ---------------------------------------
has more than done, generally is started discuss the results !
------ For reference only -------------------------------------- -
there are so complicated? Displayed on the rewrite , covering , called overloading .
------ For reference only -------------------------------------- -
rewrite is not overloaded !
------ For reference only ---------------------------------------
rewrite is not overloaded !      I made a mistake . It should be called the coverage
------ For reference only ---------------------------------- -----
rewrite is not overloaded !             I made a mistake . It should be called the covering  
------ For reference Why do I only ---------------------------------------
inherited methods intact will show an impact on the results ?

rewriting without rewriting can not see out of it ?

Do you think the compiler a word a word there are two methods to verify inconsistencies ?
------ For reference only -------------------------------------- -
you mean is look at the method name , the parameters of these can be, do not look at the method body completely content ?
------ For reference only -------------------------------------- -
these are the basic features of inheritance java dynamic binding pleomorphic landlord to engage the book look it
------ For reference only - -------------------------------------
18th floor, feeling the explanation already in place. . . Louzhu one hundred thousand why is indeed puzzling . .

没有评论:

发表评论