0
I have a code which is a little confusing.See it below...Can some one tell me how it executes step by step?
2 Antworten
+ 1
// Create TwoThree object storing it as a SubTwoThree
SubTwoThree t = new TwoThree();
// Call the void g(long x) {f(x);} of SubTwoThree with 20
// Call the void f(int x) {System.out.println("int in S: " + x);} of TwoThree as it overrides the SubTwoThree version with 20
// Output "int in S: 20"
t.g(20);
// Call the void g(long x) {f(x);} of SubTwoThree with 20L
// Since there isn't a void f(long x) function of SubTwoThree automatically convert to double which does exist
// Call the void f(double x) {System.out.println("double in T: " + x);} with 20.0
// Output "double in T: 20.0"
t.g(20L);
// Since there isn't a void g(float x) function of SubTwoThree automatically convert to double which does exist
// Call the void g(double x) {f(x);} of SubTwoThree with 3.5
// Call the void f(double x) {System.out.println("double in T: " + x);} with 3.5
// Output "double in T: 3.5"
t.g(3.5f);
// Call the void g(double x) {f(x);} of SubTwoThree with 3.5
// Call the void f(double x) {System.out.println("double in T: " + x);} with 3.5
// Output "double in T: 3.5"
t.g(3.5);
0
class TwoThree {
void g(int x) {
f(x);
}
void g(double x) {
f(x);
}
void g(long x) {
f(x);
}
void f(int x) {
System.out.println("int in T: " + x);
}
void f(double x) {
System.out.println("double in T: " + x);
}
}
public class SubTwoThree extends TwoThree {
void f(int x) {
System.out.println("int in S: " + x);
}
void f(long x) {
System.out.println("long in S: " + x);
}
public static void main(String[] args) {
TwoThree t = new SubTwoThree();
t.g(20);
t.g(20L);
t.g(3.5f);
t.g(3.5);
}
}