public class Employee{ private String name; public Employee(String name){ this.name = name; } public void display(){ System.out.print(name); } } public class Manager extends Employee{ private String department; public Manager(String name,String department){ super(name); this.department = department; } public void display(){ System.out.println( super.display()+”,”+department); } } 执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
第1题:
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
[说明]
在一些大型系统中,大多数的功能在初始化时要花费很多时间,如果在启动的时候,所有功能(连不用的功能)都要全面初始化的话,会连带影响到应用软件要花很多时间才能启动。因此常将程序设计成到了实际要使用某种功能的阶段才初始化该功能。
以下示例展示了Proxy(代理)模式,PrinterProxy类执行一些比较“轻”的方法——设置名称和取得名称,需要真正执行“重”的方法——真正打印——时才初始Print类。图6-1显示了各个类间的关系。
[图6-1]
[Java代码]
//Printable.Java
publiC (1) Printable{
public abstract void setPrinterName(String name);
public abstract String getprinterName();
public abstract void print(String string);
}
//Printer.Java
public class Printer implements Printable{
private String name;
public Printer(){
System.out.println("正在产生Printer的对象实例");
}
public Printer(String name){
this.name=name;
heavyJob("正在产生Printer的对象实例("+name+")");
public void setPrinterName(String name){
this.name=name;
public String getPrinterName(){
return name;
public void print(String string){
System.out.println("===" +name+" ====");
System.out.println(string);
}
}
//PrinterProxy.Java
public class PrinterProxy (2) Printable{
private String name;
private Printer real;
public PrinterProxy(){}
public PrinterProxy(String name){
this.name=name;
}
public gynchronized void setPrinterName(String name){
if( (3) ){
real.setPrinterName(name);
}
this.name=name;
}
public String getprinterName(){
return name;
}
public void print(String string){
(4);
real.print(string);
}
private synchronized void realize(){//产生真正的Printer对象
if(real==null){
real=(5);
}
}
}
(1)
第2题:
public class Pet{ private String name; public Pet(){ System.out.print(1); } public Pet(String name){ System.out.print(2); } } public class Dog extends Pet{ public Dog(String name){ System.out.print(3); } } 执行new Dog(“棕熊”);后程序输出是哪项?()
第3题:
阅读以下说明和Java代码,将应填入(n)处的字句写在对应栏内。
[说明]
本程序的功能是给公司的员工Tom,Jack,Green增加薪水。三人的职位分别是programmer, Manager,CEO。
程序由6个类组成:WorkerTest是主类,programmer,Manager,CEO三个类,薪水增加的规则是 programmer的涨幅是5%;Manager的是10%;CEO也是Manager,但是它除了有Manager的涨幅,还有1000元的bonus。接口SalaryRaise提供了一个增加薪水的方法raise()。
[java程序]
public class WorkerTest {
public WorkerTest( ) {}
public static void main( String[] args) {
Programmer programmer = new Programmer( "Tom" ,3000);
Manager manager = new Manager( "Jack" ,4000);
CEO ceo = new CEO( "Green" ,4000);
Worker [] worker = new Worker[3];
programmer, raise( );
manager, raise( );
ceo. raise( );
worker[0] = programmer;
worker [1] = manager;
worker[2] = ceo;
for ( int i = 0 ;i < worker, length; i + + ) {
System. out. prinfln (" Name:" + worker [i]. getName ( ) +" \ tSalary:" + worker [i]. getSalary ());
public interface SalaryRaise { void raise( ); }
public class Worker {
public String name;
public double (1);
public Worker( ) {}
public String getName( ) {return name;}
public void setName( String name) {this. name = name;}
public double getSalary( ) {return salary;}
public void setSalary(double salary) { this. salary = salary; }
}
public class Programmer extends Worker implements (2) {
public Programmer( ) {}
public void raise( ) {
double pets=0.05;
double sala = this. getSalary( ) * (1 + pers);
this. setSalary (sala);
public Programmer( Siring name, double salary) t
this. name = name;
this. salary = salary;
public class Manager extends (3) implements SalaryRaise {
public Manager( ) { }
public Manager(String name, double salary) {
this. name = name;
this. salary = salary;
}
public void raise( ) {
double pets = 0.1;
double sala = this. getSalary() * (1 + pers);
this. setSalary(sala);
}
}
public class CEO extends Manager implements SalaryRaise {
public CEO() {}
public CEO( String name,double salary) {
this. name = name;
this. salary = salary;
}
public void raise( ) {
double bonus = 1000;
(4);
double sala = this. getSalary( );
(5);
this. setSalary(sala);
}
}
第4题:
public class Pet{ private String name; public Pet(){ System.out.print(1); } public Pet(String name){ System.out.print(2); } } public class Dog extends Pet{ public Dog(){ System.out.print(4); } public Dog(String name){ super(name); System.out.print(3); } } 执行new Dog(“棕熊”);后程序输出是哪项?()
第5题:
public class Pet{ private String name; public Pet(String name){ this.name = name; } public void speak(){ System.out.print(name); } } public class Dog extends Pet{ public Dog(String name){ super(name); } public void speak(){ super.speak(); System.out.print(“ Dog ”); } } 执行代码 Pet pet = new Dog(“京巴”); pet.speak(); 后输出的内容是哪项?()
第6题:
执行以下代码后,下面哪些描述是正确的() public class Student{ private String name = “Jema”; public void setName(String name){ this.name = name; } public String getName(){ return this.name; } public static void main(String[] args){ Student s; System.out.println(s.getName()); } }
第7题:
public class Employee{ private String name; public Employee(String name){ this.name = name; } public String getName(){ return name; } } public class Manager extends Employee{ private String department; public Manager(String name,String department){ this.department = department; super(name); (应于上一行掉位置) System.out.println(getName()); } } Super的位置是否在方法的首行 执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
第8题:
interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable {
Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
this.name = name;
}
public void play() {
ball = new Ball("Football");
System.out.println(ball.getName());
}
}
这个错误不容易发现。
错。"interface Rollable extends Playable, Bounceable"没有问题。interface 可继承多个
interfaces,所以这里没错。问题出在interface Rollable 里的"Ball ball = new Ball("PingPang");"。
任何在interface 里声明的interface variable (接口变量,也可称成员变量),默认为public static
final。也就是说"Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new
Ball("PingPang");"。在Ball 类的Play()方法中,"ball = new Ball("Football");"改变了ball 的
reference,而这里的ball 来自Rollable interface,Rollable interface 里的ball 是public static final
的,final 的object 是不能被改变reference 的。因此编译器将在"ball = new Ball("Football");"
这里显示有错。
第9题:
public class Employee{ private String name; public Employee(String name){ this.name = name; } public void display(){ System.out.print(name); } } public class Manager extends Employee{ private String department; public Manager(String name,String department){ super(name); this.department = department; } public void display(){ System.out.println(super.display()+”,”+department); } } 执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()
第10题:
public class Employee{ private String name; public Employee(String name){ this.name = name; } public String getName(){ return name; } } public class Manager extends Employee{ private String department; public Manager(String name,String department){ this.department = department; super(name); System.out.println(getName()); } } 执行语句new Manager(“smith”,”SALES”)后程序的输出是哪项?()