1.题目
class A {
float computer(float x,float y) {
return x+y;
}
public int g(int x,int y) {
return x+y;
}
}
class B extends A {
/***************************************************/
float computer (double x,float y) {
return x-y;
}
}
public class E_9{
public static void main(String args[]) {
B b=new B();
/***************************************************/
double result=b.computer(8,9.0); //b调用重写的方法
System.out.println(result);
int m=b.g(12,8); //b调用继承的方法
System.out.println(m);
}
}
1.答案
float computer (double x,float y) 改成 float computer (floatx,float y)
//在 A 类中,computer 方法的签名是 float computer(float x, float y),它接受两个 float 类型的参数。在 B 类中,computer 方法的签名被重写为 float computer(double x, float y),它接受一个 double 类型的第一个参数和一个 float 类型的第二个参数。
double result=b.computer(8,9.0); 改成 double result=b.computer(8,9); 或 double result=b.computer(8,9.0L);
//int和long 类型的参数可以隐式转换为 float 类型.
2.题目
class Sum {
int n;
float f() {
float sum=0;
for(int i=1;i<=n;i++)
sum=sum+i;
/***********************************/
return i;
}
}
class Average extends Sum {
int n=5;
float f() {
float c;
/***********************************/
c=Sum.f();
return c/n;
}
}
public class Example5_7 {
public static void main(String args[]) {
Average aver=new Average();
aver.n=100;
float resultOne=aver.f();
System.out.println("resultOne="+resultOne);
}
}
2.答案
return i; 改成 return sum;
c=Sum.f(); 改成 c=super.f();
3.题目
interface Computable {
int MAX = 46;
int f(int x);
}
/***********************************/
class China , Computable { //China类实现Computable接口
int number;
/************************************/
int f(int x) {
int sum=0;
for(int i=1;i<=x;i++) {
sum=sum+i;
}
return sum;
}
}
public class E_16 {
public static void main(String args[]) {
China zhang;
Japan henlu;
zhang=new China();
henlu=new Japan();
zhang.number=32+Computable.MAX;
henlu.number=14+Computable.MAX;
System.out.println("zhang的学号"+zhang.number+",zhang求和结果"+zhang.f(100));
System.out.println("henlu的学号"+henlu.number+",henlu求和结果"+henlu.f(100));
}
}
3.答案
class China , Computable 改成 class China implements Computable
public int f(int x)
4.题目
interface MoneyFare {
void charge();
}
interface ControlTemperature {
void controlAirTemperature();
}
class Bus implements MoneyFare {
void brake() {
System.out.println("公共汽车使用毂式刹车技术");
}
public void charge() {
System.out.println("公共汽车:一元/张,不计算公里数");
}
}
/************************************************/
class Taxi implements MoneyFare, implements ControlTemperature {
void brake() {
System.out.println("出租车使用盘式刹车技术");
}
public void charge() {
System.out.println("出租车:2元/公里,起价3公里");
}
/***********************************************/
void controlAirTemperature() {
System.out.println("出租车安装了Hair空调");
}
}
public class E_17 {
public static void main(String args[]) {
Bus bus101 = new Bus();
Taxi buleTaxi = new Taxi();
Cinema redStarCinema = new Cinema();
bus101.brake();
bus101.charge();
buleTaxi.brake();
buleTaxi.charge();
buleTaxi.controlAirTemperature();
redStarCinema.charge();
redStarCinema.controlAirTemperature();
}
}
4.答案
class Taxi implements MoneyFare, implements ControlTemperature 改成 class Taxi implements MoneyFare,ControlTemperature
void controlAirTemperature() 改成 public void controlAirTemperature()
5.题目
interface ShowMessage {
void 显示商标(String s);
}
class TV implements ShowMessage {
/**************************************/
public String 显示商标(String s) {
System.out.println(s);
}
}
public class E_18 {
public static void main(String args[]) {
ShowMessage sm;
/*******************************************/
sm=new ShowMessage();
sm.显示商标("长城牌电视机");
}
}
5.答案
public String 显示商标(String s) 改成 public void 显示商标(String s)
sm=new ShowMessage(); 改成 sm=new TV();
6.题目
interface A
{
String INFO = "Computer" ;
void say() ;
}
/**************************************/
class X extends A {
public void say()
{System.out.println("信息是:"+INFO) ;}
}
public class TestInterface1
{
public static void main(String args[])
{
X x = new X() ;
x.say() ;
/*******************************************************/
System.out.println(“接口中Info:"+INFO);
}
6.答案
class X extends A 改成 class X implements A
System.out.println(“接口中Info:“+INFO); 改成 System.out.println(“接口中Info:”+A.INFO);
7.题目
abstract class A {
abstract int add(int x,int y);
int sub(int x,int y) {
return x-y;
}
}
class B extends A {
/***********************************/
int add(float x,int y) {
return x+y;
}
}
public class E_20 {
public static void main(String args[]) {
B b=new B();
int sum=b.add(30,20);
int sub=b.sub(30,20);
System.out.println("sum="+sum);
System.out.println("sum="+sub);
A a;
/***************************************/
b= a;
sum=a.add(30,20);
sub=a.sub(30,20);
System.out.println("sum="+sum);
System.out.println("sum="+sub);
}
}
7.答案
int add(float x,int y) 改成 int add(int x,int y)
b= a; 改成 a=b;
8.题目
import java.io.*;
public class Example10_1 {
public static void main(String args[]) {
File f = new File("","Example10_1.java");
System.out.println(f.getName()+"是可读的吗:"+f.canRead());
System.out.println(f.getName()+"的长度:"+f.length());
System.out.println(f.getName()+"的绝对路径:"+f.getAbsolutePath());
File file = new File("new.txt");
System.out.println("在当前目录下创建新文件"+file.getName());
/****************************************************/
if(!file) {
try {
/**************************************/
file.create();
System.out.println("创建成功");
}
catch(IOException exp){}
}
}
}
8.答案
if(!file) 改成 if(!file.exists())
file.create(); 改成 file.createNemFile();