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]
}
}
举一反三
- 请说出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】 } }
- 请写出下列程序的输出结果。class A{public int f(int x) {return x+1;}}class B extends A{public int f(int x){return x*x;}}public class E{public static void main(String args[]){A a=new B();int m=a.f(10);System. out. println(m) ;}}
- 说出下列B类中【代码1】,【代码2】的输出结果 class A { public int getNumber(int a) { return a+1; } } class B extends A { public int getNumber (int a) { return a+100; } public static void main (String args[]) { A a =new A(); System.out.println(a.getNumber(10)); //【代码1】 a = new B(); System.out.println(a.getNumber(10)); //【代码2】 } }
- 下列 A 类中【代码 1】~【代码 4】哪个是错误的? class Tom { private int x = 120; protected int y = 20; int z = 11; private void f() { x = 200; System.out.println(x); } void g() { x = 200; System.out.println(x); } } public class A { public static void main(String args[]) { Tom tom = new Tom(); tom.x = 22; //【代码 1】 tom.y = 33; //【代码 2】 tom.z = 55; //【代码 3】 tom.g(); //【代码 4】 } }
- (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); } }
内容
- 0
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方法。( )
- 1
不能与该方法构成重载的方法是() 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 ; }
- 2
以下代码的输出结果为( ) public class Pass{ static int j = 20; public void amethod(int x){ x = x*2; j = j*2; } public static void main(String args[]){ int i = 10; Pass p = new Pass(); p.amethod(i); System.out.println(i+" and "+j); } }
- 3
执行下列程序,输出结果为()。 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); } }
- 4
智慧职教: 以下给出代码运行后的结果是? public class Example { public static void main(String[] args) { int x=1; int y=~x+1; System.out.println(x+" "+y); } }