R4-1
在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数个数、类型或顺序各不相同,传回的值也可以不相同。这种面向对象程序的特性称为
重载|override
2 分
R4-2
请写出以下程序运行结果:
class Width { public static final int SINGLE = 1; } class Shape { public void draw(int Width) { System.out.println("Shape.draw()"+width); } } class Circle extends Shape { public void draw(Width n) { System.out.println("Circle.draw()"); } } public class Test { public static void test(Shape s) { s.draw(Width.Single); } public static void main(String[] args) { Shape c = new Circle(); test(c); } }
Shape.draw()1
2 分
R4-3
设计一个动物声音“模拟器”,希望模拟器可以模拟许多动物的叫声,要求如下:
(1)编写接口Animal
Animal接口有2个抽象方法.
(2)编写实现Animal接口的Dog类。
(3)编写主类SimulateTest,在主类SimulateTest的main方法中编写程序进行测试,输出以下结果:
我的名字叫Bob
汪汪
答案:
第1空:interface ||
第2空:cry() ||
第3空: getName() ||
第4空:implements ||
第5空:String ||
第6空:this.name ||
第7空:public ||
第8空:return ||
第9空:SimulateTest ||
第10空:static ||
第11空:main ||
第12空:dog ||
第13空:Bob ||
第14空:getName() ||
第15空:dog ||
R4-4
请写出以下程序运行结果:
public class Test { private int i = 0; Test increment() { i++; return this.clone(); } public Test clone() { Test t = new Test(); t.i = i; return t; } public Test() { System.out.print(i); } void print() { System.out.printf("i = %d", i); } public static void main(String[] args) { Test x = new Test(); for ( int i=0; i<10; i++ ) x = x.increment(); x.print(); } }
@@[00000000000i = 10](2)
2 分
R4-5
请写出以下程序运行结果:
package test; public class Test { public static void main(String[] args) { Test t = new Test(); t.show(new Car(){ public void run(){ System.out.println("test run"); } }); } public void show(Car c){ c.run(); c.show(); }} abstract class Car{ public void run(){ System.out.println("car run"); } public void show(){ System.out.println(getClass().getName()); }}
test run
2 分
test.Test$1
2 分
R4-6
请写出以下程序运行结果:
class Foo { protected class Bar { protected Bar() { System.out.println("Foo.Bar"); } } private Bar b; Foo() { System.out.println("Foo"); b = this.new Bar(); } } class FooToo extends Foo { protected class Bar { protected Bar() { System.out.println("FootToo.Bar"); } } public static void main(String[] args) { new FooToo(); } }
@@[Foo](2)
2 分
@@[Foo.Bar](2)
2 分
R4-7
若要使一个方法能不依赖与该类对象而存在,则需使用的关键字
static
2 分
R4-8
接口中的方法其默认属性为abstract
2 分
和public
R4-9
阅读下列程序:
class Animal{ Animal(){ System.out.println("我是动物"); } void shout(){ System.out.println("动物叫"); } } class Dog extends Animal{ void shout(){ super.shout(); System.out.println("汪汪"); } } class Cat extends Animal{ void shout(){ System.out.println("喵喵"; } } public class Test{ public static void main(String[]args){ Animal dog=newDog(); Animal cat=newCat(); dog.shout(); cat.shout(); } }
按程序执行顺序输出结果:
(1)@@[我是动物](2)
(2)@@[我是动物](2)
(3)@@[动物叫](2)
(4)@@[汪汪](2)
(5)@@[喵喵](2)
R4-10
若需要定义一个类属性或类方法,应使用
static
2 分
修饰符
R4-11
编写Acount类和CheckingAccount类类AcountTest,具体要求如下:
(1) Account类表示账户对象,包含以下成员
①属性:
balance:私有,double型,表示账户余额;
②方法:
Account(), 构造方法,balance初始化为0;
Account(double balance),构造方法,用参数设置账户余额;
void setBalance(double balance):修改账户金额
double getBalance():返回账户金额
boolean withDraw(double money):从账户提取特定数额,如果余额不足,返回false;否则,修改余额,返回true;
void deposit(double money):向账户存储特定数额。
(2)编写一个Account类的子类CheckingAccount,表示支票账户对象,包含以下成员
①属性:
overdraft:私有,double型,表示透支限定额;
②方法:
CheckingAccount(), 构造方法,balance和overdraft都初始化为0;
CheckingAccount(double balance,double overdraft),构造方法,用参数设置账户编号、余额和透支限定额;
boolean withDraw(double money):从账户提取特定数额,如果超出透支限定额,返回false;否则,修改余额,返回true。
根据以上要求,完善下面的代码。
第1空:private ,
第2空:balance=0; ,
第3空: this.balance=balance; ,
第4空:double ,
第5空:return false; ,
第6空: return true; ,
第7空: extends ,
第8空:double overdraft; ,
第9空:super() ,
第10空:overdraft=0; ,
第11空:super(balance); ,
第12空:this.overdraft=overdraft; ,
第13空:withDraw ,
第14空:money>getBalance()+overdraft ,
第15空:setBalance(getBalance()-money); ,
R4-12
说出下列A类中【代码1】~【代码3】的输出结果
class Fish { int weight=1; } class Lake{ Fish fish; void setFish(Fish s) { fish=s; } void foodFish(int m){ fish.weight=fish.weight+m; } } public class Main { public static void main(String args[]){ Fish redFish=new Fish(); System.out.println(redFish.weight);//【代码1】 Lake lake=new Lake(); lake.setFish(redFish); lake.foodFish(120); System.out.println(redFish.weight);//【代码2】 System.out.println(lake.fish.weight);//【代码3】 } }
【代码1】
1
2 分
【代码2】
121
2 分
【代码3】
121
2 分
R4-13
请写出以下程序运行结果:
class Letter { char c; } public class Main { static void f(Letter y) { y.c = 'z'; } public static void main(String[] args) { Letter x = new Letter(); x.c = 'a'; f(x); System.out.println(x.c); } }
z
2 分
R4-14
阅读以下程序,写出输出结果。
public class AppleMobilePhone extends MobilePhone{ public static void main(String[] args){ new AppleMobilePhone(); } public AppleMobilePhone(){ System.out.println("This is a Applemobilephone"); } } class MobilePhone extends Phone{ public MobilePhone(){ System.out.println("This is a mobilephone"); } } class Phone{ public Phone(){ System.out.println("This is a phone"); } }
程序运行后输出:
从下列选项中选,在空格处填上相应的序号数字(1、2或3)
(1)This is a phone
(2)This is a Apple mobile phone
(3)This is a mobile phone
第一行输出:
1
2 分
第二行输出:
3
2 分
第三行输出:
2
2 分
R4-15
For a class Class1 in Class1.java as below:
package Testpackage; public class Class1{ … … }
The main() is in MainPro.java as below:
import Testpackage.*; … …
The CLASSPATH is "c:\java\lib\classes.zip;.; ". The MainPro.java is in c:\Testdir. The current directory is c:\Testdir. Where should we put the Class1.class?
c:\Testdir\Testpackage
2 分
R4-16
分析程序的运行结果
public class Father{ String name,address,tel; int age; public Father(String name,int age){ this.name=name;this.age=age; } void out(){ System.out.println("姓名:"+name); System.out.println("年龄:"+age); } void outOther(){ System.out.println("家庭住址:"+address); System.out.println("电话:"+tel); } } class Son extends Father{ String school; public Son(String name,int age){ super(name,age); } void out(){ super.out(); super.outOther(); System.out.println ("学校:"+school); } public static void main(String args[]){ Son son=new Son("Tom",15); son.address="金水区"; son.school="九中"; son.tel="66123456"; son.out(); } }
可选项:
(1)家庭住址:金水区
(2)电话:66123456
(3)学校:九中
(4)姓名:Tom
(5) 年龄:15
在下列空行中根据上述可选项按运行顺序写下运行结果(在空格中填入序号1、2、3、4或5):
第1空:4 ,
第2空:5 ,
第3空:1 ,
第4空:2 ,
第5空:3 ,
R4-17
请写出以下程序运行结果:
class ValHold{ public int i = 10; } public class ObParm{ public static void main(String argv[]){ ObParm o = new ObParm(); o.amethod(); } public void amethod(){ int i = 99; ValHold v = new ValHold(); v.i=30; another(v,i); System.out.print( v.i ); } public void another(ValHold v, int i){ i=0; v.i = 20; ValHold vh = new ValHold(); v = vh; System.out.print(v.i); System.out.print(i); } }
答案:
第1空:10020
2 分
