पर ओवरलोडिंग विधि सरल प्रश्न, अजीब परिणाम। मैं दो वर्गों A
और B
है:विस्तारित वर्ग
public class A
{
protected int num;
public A(int n)
{
num = n;
}
public boolean f(A a)
{
return num == a.num * 2;
}
}
public class B extends A
{
public B(int n)
{
super(n);
}
public boolean f(B b)
{
return num == b.num;
}
}
क्यों y1.f(y2)
कॉल A
बजाय B
में में f()
विधि?
A y1 = new B(10);
B y2 = new B(10);
System.out.println(y1.f(y2));
यह रूप में B
A
की तुलना में अधिक विशिष्ट है B
में f()
फोन करने वाला नहीं है?
हालांकि @ जॉनस्केट का जवाब हमेशा निर्विवाद है .. बस इसे बाइट कोड के साथ भी समर्थन कर रहा है .. आप यह भी देख सकते हैं कि कौन सी विधि बीत रही है और आप इसे देख सकते हैं कि संकलित कोड '(y1.f (y2))' जब आप बाइट कोड '25: invokevirtual # 27 // विधि com/package1/Af: (Lcom/p ackage1/A;) Z' –