- 提供一个接口 Measurable,它有一个方法 double getMeasure(),该方法以某种方式测量对象。让 Employee 类实现 Measurable 接口。提供一个方法 double average(Measurable[] objects),该方法计算测量的平均值。使用它来计算一组 employee 的平均工资。
参考答案:
Measurable.java 的代码如下:
package com.example;
/**
* Created by toby on 17-12-5.
*/
public interface Measurable {
double getMeasure();
}
LearnJava.java 的代码如下:
package com.example;
public class LearnJava {
static class Employee implements Measurable {
private double salary = 0;
Employee() {
this(0);
}
Employee(double salary) {
this.salary = salary;
}
@Override
public double getMeasure() {
return salary;
}
}
static double average(Measurable[] objects) {
double sum = 0;
if (objects.length > 0) {
for (Measurable m : objects) {
sum += m.getMeasure();
}
return sum / objects.length;
}
return 0;
}
public static void main(String[] args) {
Measurable[] objects = new Measurable[50];
for (int i=1; i<=50; ++i) {
Employee employee = new Employee(i);
objects[i-1] = employee;
}
double result = average(objects);
System.out.println(result);
}
}
上述代码的运行结果:
- 继续上一个练习并提供一个Measurable largest(Measurable[] objects) 方法。用该方法查找工资最高的员工的姓名。为什么需要强制转换?
参考答案:
package com.example;
public class LearnJava {
static class Employee implements Measurable {
private double salary = 0;
private String name = "";
Employee() {
this(0);
}
Employee(double salary) {
this(salary, "");
}
Employee(double salary, String name) {
this.salary = salary;
this.name = name;
}
@Override
public double getMeasure() {
return salary;
}
public String getName() {
return name;
}
}
static double average(Measurable[] objects) {
double sum = 0;
if (objects.length > 0) {
for (Measurable m : objects) {
sum += m.getMeasure();
}
return sum / objects.length;
}
return 0;
}
static Measurable largest(Measurable[] objects) {
Measurable maxMeasurable = null;
double max = 0;
if (objects.length > 0) {
for (Measurable m : objects) {
if (m.getMeasure() > max) {
max = m.getMeasure();
maxMeasurable = m;
}
}
}
return maxMeasurable;
}
public static void main(String[] args) {
Measurable[] objects = new Measurable[50];
for (int i=1; i<=50; ++i) {
Employee employee = new Employee(i, "test" + i);
objects[i-1] = employee;
}
Employee result = (Employee)largest(objects);
System.out.println(result.getName());
}
}
输出结果:
之所以需要转换类型,是因为我们需要调用子类的方法,而我们返回的是被继承的接口的类型,该接口类型没有相应的方法。
- String 的所有父类有哪些?Scanner 类的呢?ImageOutputStream 类的呢?注意,每个类型都有自己的父类。没有声明父类的类或者接口,其父类为 Object
参考答案:
String 的所有父类如下:
Scanner 类的所有父类如下:
其中 Closeable 继承自 AutoCloseable
ImageOutputStream 类的所有父类如下:
上图中 ImageInputStream 的所有父类如下:
其中 Closeable 继承自 AutoCloseable
- 实现 IntSequence 类的一个静态方法,该方法使用参数产生序列。例如,IntSequence.of(3, 1, 4, 1, 5, 9) 产生有六个值的序列。如果你返回匿名内部类的实例,则额外加分。
参考答案:
IntSequence 接口的实现如下:
package com.example;
import java.util.ArrayList;
/**
* Created by toby on 17-12-5.
*/
public interface IntSequence {
public static IntSequence of(int... ints) {
return new DigitSequence(ints);
}
static class DigitSequence implements IntSequence {
private ArrayList<Integer> integers = new ArrayList<>();
public DigitSequence(int[] ints) {
integers.clear();
integers = null;
integers = new ArrayList<>(ints.length);
for (int anInt : ints) {
integers.add(anInt);
}
}
public boolean hasNext() {
return integers.size() > 0;
}
public int next() {
int v = integers.get(0);
integers.remove(0);
return v;
}
}
}
测试使用的代码如下:
package com.example;
public class LearnJava {
public static void main(String[] args) {
IntSequence.DigitSequence digits = (IntSequence.DigitSequence)
IntSequence.of(3, 1, 4, 1, 5, 9);
while (digits.hasNext()) {
System.out.println(digits.next());
}
}
}
输出结果:
- 实现 IntSequence 类的静态 constant 方法,该方法产生无限常量序列。例如,IntSequence.constant(1) 产生值 1 1 1...永无休止。如果你使用 lambda 表达式,则额外加分。
IntSequence 类的代码如下:
package com.syberos.learnjava;
import java.util.ArrayList;
/**
* Created by toby on 17-12-5.
*/
public interface IntSequence {
public static IntSequence of(int... ints) {
return new DigitSequence(ints);
}
public static IntSequence constant(int i) {
return new InfiniteSequence(i);
}
static class DigitSequence implements IntSequence {
private ArrayList<Integer> integers = new ArrayList<>();
public DigitSequence(int[] ints) {
integers.clear();
integers = null;
integers = new ArrayList<>(ints.length);
for (int anInt : ints) {
integers.add(anInt);
}
}
public boolean hasNext() {
return integers.size() > 0;
}
public int next() {
int v = integers.get(0);
integers.remove(0);
return v;
}
}
static class InfiniteSequence implements IntSequence {
private int constant = 0;
public InfiniteSequence(int i) {
constant = i;
}
public boolean hasNext() {
return true;
}
public int next() {
return constant;
}
}
}
测试用的代码如下:
package com.syberos.learnjava;
public class LearnJava {
private static void printSequence(IntSequence.InfiniteSequence ints) {
Runnable runnable = () -> {
while (ints.hasNext()) {
System.out.println(ints.next());
}
};
new Thread(runnable).start();
}
public static void main(String[] args) {
IntSequence.InfiniteSequence ints = (IntSequence.InfiniteSequence)
IntSequence.constant(1);
printSequence(ints);
}
}