请说出E类中【代码1】,【代码2】的输出结果。 interface A { double f(double x,double y); } class B implements A { public double f(double x,double y) { return x*y; } int g(int a,int b) { return a+b; } } public class E { public static void main(String args[]) { A a = new B(); System.out.println(a.f(3,5)); //【代码1】 B b = (B)a; System.out.println(b.g(3,5)); //【代码2】 } }
举一反三
- 请说出E类中[代码1]和[代码2]的输出结果。 class A{ double f(int x, double y){ return x+y; } int f(int x,int y) { return x*y; } } public class E{ public static void main(String args[ ] ){ A a=new A(); System.out.println(a.f(10,10)) ; // [代码1] System.out.println(a.f (10,10.0)) ; // [代码2] } }
- 不能与该方法构成重载的方法是() public double fun(int x,double y){ return x + y; } A: public void fun(int x){ System.out.println("x * x="+(x*x)); } B: public void fun(int a, double b){ System.out.println("a +b ="+(a+b)); } C: public int fun(int x, int y,int z){ renturn (x+y+z); } D: public double fun(double x, int y){ return x * y ; }
- public class test1 { public static int add(int a, int b) { return a+b; } public static double add(double a, double b) { return a+b; } ________________ ___________________ { ________________ ___________________ } public static void main(String[] args) { System.out.println("调用add方法:"+add(2.1,3.4,4.5)); } }
- 执行下列程序,输出结果为()。 public class B{ public static void main(String[] args) { int x = 5; double y = 10.5f; float z = (float)(x*y); System.out.println(z); } }
- 以下代码运行后输出的内容( )public class Main { public static double div(int a, int b) { return (double)a/b; } public static int div(int a, int b) { return a/b; } public static void main(String[] args) { System.out.println(div(5, 2)); } } A: 2.5 B: 2 C: 输出内容不确定 D: 程序错误,无法运行