(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);
}
}
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);
}
}
举一反三
- (7-12)请阅读程序,写出程序运行结果。 class A{ static String name="tom"; static int getX() { return 2; } int getY() { return 3; } } class B extends A{ static String name="Tuny"; static int getX() { return 4; } int getY() { return 5; } } class C extends A{ static String name="Tuny"; static int getX() { return 4; } int getY() { return 5; } } interface D{ int getY(); } class E implements D{ public int getY() { return 6; } } public class Demo{ public static void main(String[] args) { A a=new A(); A b=new B(); A c=new C(); D d=new E(); int sum=a.name.length()+b.getX()+c.getY()+d.getY(); System.out.println(sum); } }
- 以下的类(接口)定义中正确的是( ) A: public class A{ private int x; public getX(){ return x; }} B: public abstract class A{ private int x; public abstract int getX(); public int aMethod(){ return 0; }} C: public class A{ private int x; public abstract int getX();} D: public interface interfaceA{ private int x; public int getX(){ return x; }}
- 有如下程序: #include<iostream> using namespace std; class XX{ int x; public: XX(int XX=0):x(xx){} int getX(){return x;} }; class YY:public XX{ int y; public: YY(int xx,int yy):XX(xx),y(yy){} int getV(){return getX()+y;} }; int main(){ YY c(3,4); cout<<c.getV()+c.getX()<<endl; return 0; } 运行这个程序的输出结果是______。 A: 3 B: 4 C: 7 D: 10
- (7-1)以下程序运行结果是()。 class FatherX{ public Integer getX() { return new Integer(10); } } class Son extends FatherX{ public Double getX() { return new Double(20); } } public class Demo12 { public static void main(String[] args) { FatherX f=new FatherX(); Son s=new Son(); System.out.println(f.getX()+s.getX()); } }
- 写出下面程序运行结果。 public class Demo { public static void main(String args[]) { int x = 9, y = 11, z = 8; int t, w; t = x > y ? x : y + x; w = t > z ? t : z; System.out.println(w); } }