2013年10月1日星期二

Parent class subclass object reference points to the problems

FatherClass f = new SonClass()


When the parent class reference f pointing to its subclass object, can not be accessed exclusively through f subclass members of the object .

If there are subclasses override the parent class method , then according to the multi-state mechanism, through f to access this method is that when the actual visit subclass overridden method .

problem is that if a subclass overrides a method to access the exclusive sub-class member variables , this time through the parent class reference f can also call the overridden method do ?
------ Solution ---------------------------------------- ----
when the parent class reference f pointing to its child class object, can not be accessed exclusively through f subclass members of the object .
why not ? LZ Have you ever thought , because f is FatherClass, so the compiler only knows f has FatherClass.class information , FatherClass.class information other than the compiler does not know, and the subclass members are SonClass.class years, also that in FatherClass.class outside , so f can not access the subclass members

If there are subclasses override the parent class method , then according to the multi-state mechanism, through f to access this method is that when the actual visit subclass overridden method .
Why can ? Said above , f can only access FatherClass.class information ( note here refers to compile the compiler only knows that f is FatherClass type , do not know what object f specific point , point to run before we know what object ) , while the sub-class overridden method in the parent class is also present, namely SonClass.class overridden method , FatherClass.class where there are ( if SonClass.class there but there is no way FatherClass.class , f can not be called directly ) , so f You can visit, but when called ( Note that this refers runtime ), f is the actual point SonClass object, so the call is SonClass objects.

problem is that if a subclass overrides a method to access the exclusive sub-class member variables , this time through the parent class reference f can also call the overridden method do ?
can , to distinguish between compile-time and run-time , compile the compiler checks the syntax and type of run is pseudo-code for the parser machine instruction is executed , the compiler compile checks the access scope of f , ie f does not exceed FatherClass.class access the information would not be wrong , run the parser will parse method code instruction because f pointing to subclass object , it will be resolved subclasses override method code instructions, and memory space subclass object that contains sub-class member variable space , so there was no sub- class member variable does not allocate memory problems, so you can call .

LZ should be clear , why would generate FatherClass.class and SonClass.class two files , since it generates two files , of course, is that the documents to be used , you can access the files. Compile compiler only knows that f is FatherClass type ( as f = anything, it is run , because they do not run = will not run ) , it will check whether the FatherClass.class range f , and run-time , f points is SonClass object, that is the object of the memory SonClass.class information, so the code is executed SonClass.class instructions.

------ Solution ------------------------------------ --------
actually quite good understanding of multi-state , you also mentioned the parent class subclass object reference points , f is just a reference , it points to what we actually access the object , f only we visited the object of a entry that is the parent class type , only the parent class methods exposed , so we can only run when accessed through f exposed parent class method, but we actually access the subclass object As for how to achieve this subclass and superclass method completely unrelated , the subclass can call any of your own methods and properties. Like a class implements an interface, the interface methods are not achieved , we can also use the interface reference points to its implementation class of the object.
------ Solution ---------------------------------------- ----
fact, a simple little thought , if the child class to override the parent class method , a subclass can not handle some of their attributes in content , that our so-called polymorphism did it still make sense ? And so we have inherited is not returning to rewrite it to set a rule that limits the overridden method should not be used subclass -specific properties and methods?
If this is the case , inheritance , overriding , dynamic binding , what does that mean ?
------ Solution ---------------------------------------- ----
first is possible because we are now basically uses a programming interface, plus implementation class , the general interface does not define a variable, the variable is defined in a subclass , but we are still able to interface calls methods to achieve call the child class.
why this is so , and as far as I understand, a reference address is actually a representation of the " string" , then the general approach by reference method we are to come through this address , the address where the corresponding object and then call the object's methods, so since you cite the actual address is the address of the child class object , then this time by calling the reference method does is to call the child class methods do, their own methods to call what can not , the last sentence is like to summarize , reference method invocation method belongs to whom in the end just look at the address indicated by the actual point referenced object These are just individuals for polymorphic understanding of the theory and personal feeling in the middle of the problem , but no problem for the use of the basic
------ For reference only --------------------- ------------------
seeking a sober teacher to teach
------ For reference only ------------ ---------------------------
can , what you can verify this problem yourself , nothing more practice ,
- ---- For reference only ---------------------------------------
can , to try it myself
------ For reference only --------------------------------- ------
can, write down the program under test
------ For reference only --------------------- ------------------
LZ are all said that they could not understand . Find answers Daniel
------ For reference only ---------------------------------- -----
call methods through the parent class reference , the actual implementation of the sub-class methods , see the parent class reference subclass exclusive members do not need to see , so there is no problem , you can do < br> ------ For reference only ---------------------------------------
  This reply was moderator deleted at 2012-06-13 16:45:28

------ For reference only ---------------------------------- -----
sub-class public override methods of the parent class , you can call , in fact, the internal implementation , specifically what he called variables , and even some private variables are not related . Only see public override method .
------ For reference only -------------------------------------- -

still Bokeo cow B ah , terrible ......
------ For reference only --------------------------- ------------
this possible.
------ For reference only -------------------------------------- -

explained very clearly , very grateful ~
------ For reference only ---------------------- -----------------

very good, thank you very much ~
------ For reference only -------- -------------------------------
parent class subclass object reference points when the saw is just one part of a small part of the parent class .
When I started on this issue for a long time you can also confuse your own stack and heap draw diagrams to analyze.
------ For reference only -------------------------------------- -
Yes, you may have a look I wrote this example:

class Animal {
  public void eat(){
    System.out.println("Animal Eating...");
  }
  public void sleep() {
    System.out.println("Animal Sleeping...");
  }
}

public class Lion {
  public void eat() {
    System.out.println("Lion eating...");
  }

  public void sleep() {
    System.out.println("Lion sleeping...");
  }

  public static void main(String[] args) {
    Animal animal = new Lion();
    animal.eat();
    animal.sleep();
  }
}


Animal animal = new Lion ();
equivalent to creating a Lion object, and then the handle on animal operations are the equivalent of a lion, running will get Lion eating ...
lion sleeping ...
------ For reference only ---------------------------- -----------
public class FatherClass {

public void test () {

}
}

public class SonClass extends FatherClass {

private String mes = "SonClass mes ...";

@ Override
public void test () {
System.out.println (mes);
super.test ();
}

}

public class App {
public static void main (String [] args) {
FatherClass f = new SonClass ();
f.test ();
}
}

can output a "SonClass mes ..."
------ For reference only ------------------------ ---------------
compilation look left , look to the right to run ,
------ For reference only ------------ ---------------------------
too brilliant , words to describe polymorphism.

------ For reference only ---------------------------------- -----
Yes, I agree to the 7th floor saying
------ For reference only ---------------------- -----------------
looked to answer the seventh floor , I found my understanding of the compiler for JAVA is too superficial ==
- ---- For reference only ---------------------------------------
< br />
studied .
------ For reference only -------------------------------------- -
+1




------ For reference only ---------------------------------- -----
this paste is very good, 7th and 22nd floor has spoken well

------ For reference only ---------------------------------- -----

Thank you ~
------ For reference only ------------------------ ---------------
this Post great. . . Had the top, thanks to 7th floor 22 floor. . . Let this only fatherclass f = new fatherclass () is enlightened. . .
------ For reference only -------------------------------------- -
benefit
------ For reference only ------------------------------- --------


Daniel speak is not the same
------ For reference only ---------------------------------------


no inheritance
------ For reference only -------------------------------- -------
watched the 7th floor of interpretation , and instantly want to own interpretation of the
------ For reference only --------------- ------------------------
benefited ah. Powerful. Haha. Feeling a sudden epiphany a lot
------ For reference only ------------------------------ ---------
even subclasses override the parent class method , it is still called the father of it . .
------ For reference only -------------------------------------- -
class A {
public void print(){
System.out.println("A");
}
}
class B extends A {
private int a = 0;
public void print(){
System.out.println(a);
}
}
public class _Test {
public static void main(String[] args){
new B().print();
}
}

polymorphic three conditions: inheritance , overriding the parent class reference to a sub class object.
seventh floor said, well , I summed up below:
There are two polymorphic First : compile-time polymorphism, compile-time polymorphism general performance of method overloading , a class of the same name exists in multiple ways , but the method parameters without ( method data type, etc. ) , at compile time , according to the method of the passed arguments to be reloaded .
The second is : run-time polymorphism , also known as multi-state binding. Subclasses override superclass method after . Handle is passed to the parent class reference class object , and then to call the parent class object subclasses override methods . The final result of the implementation and which kind of object is passed to the parent class , which is called class methods .

------ For reference only ---------------------------------- -----
modify the code under test class is : A a = new B (); a.print ();
------ For reference only ---------------------------------------
this post is good, seventh Daniel
------ For reference only -------------------------------------- -

+ + + + + + +
learned
------ For reference only -------------------------------- -------
35 F remark well .
executed last result is what kind of object is passed to the parent class , which is called class methods .

But that is dynamic binding. Not polymorphic bind it.
------ For reference only -------------------------------------- -
reading the reply , nothing added
------ For reference only --------------------------- ------------


possible
------ For reference only -------------------------------- -------
  This reply was moderator deleted at 2012-11-02 09:12:51

------ For reference only ---------------------------------- -----
light rote rules useless, have to think about why, or run and compile issue.
------ For reference only -------------------------------------- -
Yes, you FatherClass f = new SonClass ()


Although ostensibly FatherClass, in fact, the essence of SonClass
jvm will they know , although you can not f.sonMethod (), this will not compile.
But f.fatherMethod () is actually called sonClass.sonMthod. So it does not matter , in essence, is the son
------ For reference only -------------------------------- -------
big god is answering questions of high efficiency ah
------ For reference only ---------------------------------------
  The reply deleted by an administrator at 2013-05-22 09:13:06

------ For reference only ---------------------------------- -----
package com.zhou;

public class DuoTai {
public static void main (String [] args) {
A a = new B ();
/ / a.tell ();
/ /
/ / A a = new A ();
B b = (B) a;
/ / a.tell ();
b.run ();
}

}
class A {
public void tell () {
System.out.println (" I am the parent class ! " ) ;
}
}

class B extends A {
String s = " I am a sub-class ! ' ;
public void tell () {
System.out.println (s);
}

public void run () {
System.out.println (" Run ! " ) ;
}
}
------ For reference only --------------------------------- ------

Why would generate FatherClass.class and SonClass.class two files , seemingly exist as long as there is this class will generate bytecode
------ For reference only ---------------------------------------

   Why would generate FatherClass.class and SonClass.class two files , seemingly exist as long as there is this class will generate bytecode      misunderstand what you mean , so the parent class reference when compiling the effective detection range , the runtime pointing to subclass object , two conditions determine it can not access the parent class in addition to the existing sub- class method.

没有评论:

发表评论