发布时间 : 星期三 文章Java基础部分题(参考答案)更新完毕开始阅读e1b323d7240c844769eaeedf
}
class Teacher{ public Teacher(int courseNum){ this.courseNum=courseNum; } int courseNum; static double classXishu; double courseCompute(){ return courseNum*classXishu; }
}
11、设计一个描述二维平面上点的类Position,该类需要描述点的横坐标和纵坐标,并提供访问器方法(set方法和get方法)和计算两点间距离的方法。写一个程序测试这个类 class Position { private int x = 0; private int y = 0; public Position() { } public Position (int x,int y) { this.x = x; this.y = y; } public void setX(int x) { this.x = x; } public int getX() { return x; } public void setY(int y) { this.y = y; } public int getY() {
}
class Test { public static void main(String[] args) { Position p1 = new Position (); Position p2 = new Position(3,5); System.out.println(p1.distance(p2)); } }
继承部分: 一、 选择题
1. Java语言的类间的继承关系是( )。
A) 多重的 B) 单重的 C) 线程的 D) 不能继承 2. 以下关于Java语言继承的说法正确的是( )。
A)Java中的类可以有多个直接父类 B)抽象类不能有子类
C)Java中的接口支持多继承 D)最终类可以作为其它类的父类 3. 现有两个类A、B,以下描述中表示B继承自A的是()。 A) class A extends B B) class B implements A C) class A implements B D) class B extends A 4. 下列选项中,用于定义接口的关键字是( )。
A)interface B) implements C) abstract D) class 5. 下列选项中,用于实现接口的关键字是( B)。
A)interface B) implements C) abstract D) class 二、 填空题
1. 如果子类中的某个变量的变量名与它的父类中的某个变量完全一样,则称子类中的这个变量________了父类的同名变量。
2. 属性的隐藏是指子类重新定义从父类继承来的__________。
3. 如果子类中的某个方法的名字、返回值类型和________与它的父类中的某个方法完全一样,则称子类中的这个方法覆盖了父类的同名方法。
return y; }
public double distance(Positon p) { int d1 = x - p.getX(); int d2 = y - p.getY(); return Math.sqrt(d1*d1+d2*d2); }
4. Java 仅支持类间的____重继承。
5. 抽象方法只有方法头,没有________。
6. Java 语言的接口是特殊的类,其中包含______常量和_______方法。 三、 程序阅读题
1.现有类说明如下,请回答问题: public class A{
String str1=\
String str2=\ are you? \
public String toString( ){ return str1+str2; } }
public class B extends A{
String str1=\,Bill.\
public String toString( ){ return super.str1+str1; } }
问题:
1)类A和类B是什么关系?
2)类A和类B都定义了str1属性和方法toString( ), 这种现象分别称为什么?若a是类A的对象,则a.toString( )的返回值是什么? 4)若b是类B的对象,则b.toString( )的返回值是什么? 2.现有一个类定义如下,请回答问题: 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的值各是什么?
2)生成对象e1、e2后,e1.No值为多少?能否通过类名做前缀引用属性No?
3.阅读程序,回答问题。 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{
3) float a=3.0f; String d=\ int b=4; void show( ) {
System.out.println(\ super.show( );
System.out.println(\ } }
问题:1)这是哪一类java程序? 2)类A和类B是什么关系?
3)按程序输出的格式写出程序运行后的结果.
4.现有类说明如下,请回答问题: 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()的返回值是什么? 3)若b是类B的对象,则b.getA()的返回值是什么?
4)类A和类B都定义了x属性,这种现象称为什么?
5.有如下源程序,请回答问题: 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; System.out.println(a1.s); System.out.println(a2.s); b1=(B)a1; System.out.println(b1.s); System.out.println(b2.s); } }
问题: 该程序的四行输出各是什么? 6.现有类说明如下,请回答问题: 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+\ } }