26
10. 现有一个类定义如下,请回答问题:
class Employee {
String name; int age; double wage; static int No=0;
Employee(String a1,int a2,double a3) {
name=a1; age=a2; wage=a3; No++; } }
在使用该类时,已使用下面语句生成了该类的对象:
Employee e1,e2;
e1=new Employee(\王劲\
e2=new Employee(\张山\
问题:
(1)e2.name,e2.age,e2.wage的值各是什么? (张山;30;3800.0)
(2) 生成对象e1、e2后,e1.No值为多少?能否通过类名做前缀引用属性No? (2; 能) 11. 阅读程序,回答问题。
public class InheritTest1 {
public static void main (String[] args) { A aa; B bb;
aa=new A( ); bb=new B( ); aa.show( ); bb.show(); } } class A {
int a=1; double d=2.0; void show( )
{ System.out.println(\}
class B extends A {
float a=3.0f;
String d=\ int b=4;
26
27
void show( ) {
System.out.println(\ super.show( );
System.out.println(\ } }
问题:(1)这是哪一类java程序?(java应用程序)
(2)类A和类B是什么关系?(类B是类A的子类)
(3)按程序输出的格式写出程序运行后的结果. (程序运行结果如下: Class A: a=1 d=2.0 Class A: a=1 d=2.0 Class A: a=1 d=2.0
Class B: a=3.0 d=Java program. b=4 ) 12. 现有类说明如下,请回答问题:
class A {
int x=10;
int getA(){return x;} }
class B extends A {
int x=100;
int getB(){return x;} }
问题:(1)类B是否能继承类A的属性x?(能)
(2)若b是类B的对象,则b.getB()的返回值是什么?(100) (3)若b是类B的对象,则b.getA()的返回值是什么?(10)
(4)类A和类B都定义了x属性,这种现象称为什么?(属性的隐藏) 13. 有如下源程序,请回答问题:
class A
{ String s=\ class B extends A
{ String s=\ public class TypeConvert { public static void main(String args[]) { B b1,b2=new B(); A a1,a2; a1=(A)b2;
a2=b2;
27
28
}
}
System.out.println(a1.s); System.out.println(a2.s); b1=(B)a1;
System.out.println(b1.s); System.out.println(b2.s);
问题:该程序的四行输出各是什么?
(class A class A class B class B)
14. 现有类说明如下,请回答问题:
public class A {
int x=888;
String str=\ public String toString() { return str+x; } }
public class B extends A {
String x=\
public String toString()
{ return str+x+\}
问题:1)类A和类B是什么关系?(类B是类A的子类)
2)类A和类B都定义了x属性和方法toString(),这种现象分别称为什么?(属性的隐藏和方法的覆盖)
3)若a是类A的对象,则a.toString( )的返回值是什么?(I like: 888)
4)若b是类B的对象,则b.toString( )的返回值是什么? (I like: java and 888) 15. 运行类C的输出结果是什么?
class A { } }
class B extends A {
public B() { }
28
public A() {
System.out.println(“The default constructor of A is invoked”);
29
}
public class C { public static void main(String[] args) { B b = new B();
}
}
16. 写出下面程序的运行结果() (1)
import java.io.*; public class abc
{ public static void main(String args[ ]) { AB s = new AB(\ System.out.println(s.toString( )); } }
class AB { String s1; String s2;
AB( String str1 , String str2 ) { s1 = str1; s2 = str2; } public String toString( ) { return s1+s2;} }
Hello! I love JAVA. (2)
import java.io.* ; public class abc {
public static void main(String args[ ]) { int i , s = 0 ;
int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 }; for ( i = 0 ; i < a.length ; i ++ ) if ( a[i]%3 = = 0 ) s += a[i] ; System.out.println(\ } } s = 180 (3)
import java.io.* ;
public class abc {
29
30
public static void main(String args[ ]) )
{ System.out.println(\ }
class SubClass extends SuperClass { int c;
SubClass(int aa,int bb,int cc) { super(aa,bb); c=cc; } }
class SubSubClass extends SubClass { int a;
SubSubClass(int aa,int bb,int cc) { super(aa,bb,cc); a=aa+bb+cc; }
void show()
{ System.out.println(\ } a=60 b=20
c=30 (4)
// FileName: MyException.java
class MyException extends Exception { static int intNumber=0; MyException() { intNumber++; } String Say()
{ return \}
class demo
{ public static void main(String args[]) {
int i;
for(i=0;i<3;i++) { try
{ System.out.println(\ throw new MyException(); }catch(MyException e)
{ System.out.println(e.Say()); } } }}
(5)
30