举一反三
- (7-1)请阅读程序,并写出程序运行结果。 class X{ int getX() { return 5; } } class Y extends X{ int getX() { return 6; } } class T extends X{ int getX() { return 7; } } public class Demo10 { public static void main(String[] args) { X x=new X(); Y y=new Y(); X t=new T(); int sum=x.getX()+y.getX()+t.getX(); System.out.println(sum); } }
- 请说出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 class TestDemo{int m=5;public void some(int x){m=x;}public static void main(String args[]) {new Demo().some(7);}}class Demo extends TestDemo{int m=8;public void some(int x) {super.some(x);System.out.println(m);}} A: 5 B: 8 C: 7 D: 编译错误
- 阅读下列程序,请写出该程序的输出结果。 class B{ int b; B(int x){b=x;System.out.println("b="+b); } } class A extends B{ int a; A(int x,int y){ super(x); a=y; System.out.println("b="+b+",a="+a); } } public class a32{ public static void main(String[]args){ A obj=new A(1,2); } }
- class Person {private int a;public int change(int m){ return m; }}public class Teacher extends Person {public int b;public static void main(String arg[]){Person p = new Person();Teacher t = new Teacher();int i;// point x}}在 // point x安排哪个语句合法?
内容
- 0
请说出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】 } }
- 1
class Demo{ public static void main(String[] args){ int x = 0; try{ x = div(1,2); }catch(Exception e){ System.out.println(e); } System.out.println(x) ; } public static int div(int a,int b){ return a / b ; } }
- 2
class A { public int f(int x,int y) { return x+y; } } class B extends A { public int f(byte x,int y) { return x*y; } } 子类B的对象只能调用子类中的f方法。( )
- 3
下列代码的输出结果是( )interface Com{ int M=100; int on();}class A implements Com{ public int on(){ return Com.M; }}public class E{ public static void main(String args[]){ Com com=new A(); int m=com.on(); System.out.println(m); }}
- 4
以下程序运行结果是 public class Test { public static void main(String[] args) { int a=1,b[]={2}; add(a); add(b); System.out.println(a+","+b[0]); } static int add(int x){ x++; return x; } static void add(int[] x){ x[0]++; } }