(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); } }
举一反三
- (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); } }
- (6-3)阅读程序,写出程序运行结果。 class Book{ private static int counter=0; private int id=1; private String name; public Book(String name) { this.name = name; counter++; this.id=this.id+8; } public static int getCounter() { return counter; } public int getID() { return this.id; } } public class BookDemo{ public static void main(String[] args) { Book b1=new Book("红楼梦"); Book b2=new Book("西游记"); Book b3=new Book("儒林外史"); System.out.println(b3.getCounter()*Book.getCounter()*b3.getID()); } }
- 写出程序的运行结果。 public class Demo{ public static void main(String args[]) { int b = get(); System.out.println(b); } public static int get() { try { return 1; } finally { return 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) ;}}
- (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()); } }