void a () {
System.out.println ("parent'a ()");
}
}
class AA extends A {
void a () {
System.out.println ("child'a ()");
}
}
public static void main (String [] agrs) {
AA aa = new AA ();
aa.a ();
A a = (A) aa;
a.a ();
}
Both print out the results are the same, how to call a method in A , A and AA without changing the way the case .
------ Solution ---------------------------------------- ----
A a = (A) aa;
you this wording is actually polymorphic writing, running, or AA, because you override the parent class method , want to call the parent class method, you just go directly to the new parent class A a = new A ();
------ Solution -------------------------------- ------------
AA aa = new AA ();
Since you construct the AA, A then stop thinking about it.
------ Solution ---------------------------------------- ----
class AA extends A{
void a(){
System.out.println("child'a()");
}
void sa(){
new A().a();
}
}
a.sa ();
------ Solution ---------------------------- ----------------
want to call that method a, there must be an instance of that object . A a = (A) aa; wrote , that the real object or AA, so it will call the AA with that method .
------ Solution ---------------------------------------- ----
you upcast when, in fact invoked method is overridden by subclasses way , that is, polymorphism, you can new subclass object , if the super. methods ( ) , it can Kazakhstan .
------ Solution ---------------------------------------- ----
reflection ?
------ Solution ---------------------------------------- ----
subclass call the parent class is overridden method, using super. overridden method ( ) can be !
------ Solution ---------------------------------------- ----
supports 13 floor.
------ Solution ---------------------------------------- ----
right, with a super positive solution
------ For reference only ------------------------ ---------------
in fact, I just want to know how to access through the only class has been overridden method !
His type is changed or why the point is that AA does not point A.
------ For reference only -------------------------------------- -
that the wording and
A a = new AA (); wrote no difference , polymorphism
------ For reference only -------------- -------------------------
static method
------ For reference only ---------------------------------- -----
how reflection , just read a little , how to get A reflection of this class too !
------ For reference only -------------------------------------- -
this is a subclass call the parent class , AA a = new A (), then call no problem
------ For reference only --------- ------------------------------
will complain , and sub-class can not be explicitly converted to the parent class
------ For reference only --------------------- ------------------
13 F, positive solution , this is a java and c + + is a very important distinction , in c + + , you can move upward through such type of operation to achieve the call the parent class act , and in Java, no matter what its type conversion , and its type is actually the same.
cite a simple example , C + + in
class A {
public:
void print(){
cout<<"Class A"<<endl;
}
};
class B: public A{
public:
void print(){
cout<<"Class B"<<endl;
}
};
int main(){
B b;
A a = (A)b;
a.print();
A *aa = new B();
aa->print();
cout<<typeid(a).name()<<endl;
system("PAUSE");
return 0;
}
the output is
Class A
Class A
1A
DESCRIPTION type promotion has become the object type b A type. Therefore, the behavior of call A , the A Perceptual behavior.
But this is indeed the contrary in java , see the following code:
class Base{
public String getFields(){
return "Base";
}
}
class Agg extends Base{
public String getFields(){
return "AGG";
}
public String getSuperFiedls(){
return super.getFields();
}
}
execute this code
Agg agg = new Agg ();
Base base = new Agg ();
if (base instanceof Agg)
System.out.println ("Class Agg");
else
System.out.println ("Class Base");
System.out.println (base.getFields ());
System.out.println (agg.getSuperFiedls ());
Results
Class Agg
AGG
Base
this can be , regardless of how the Java objects for type promotion , retains its original type.
Today is also encountered this problem well summed up what I hope this can help people to see .
------ For reference only -------------------------------------- -
class AA extends A {
void a () {
super.a ();
System.out.println ("child'a ()");
}
}
没有评论:
发表评论