我正在编写关于Java中的接口,泛型和抽象类的练习表。无论我怎样编码它,练习1都不会起作用。问题在代码中得到了评论。任何帮助将不胜感激,我不确定错误是在练习一个代码或在时间和点类中的接口的实现。
/*Exercise 2
(a) Write an interface Printable which has a single method put whose intention is to display a string representation of any object whose class implements Printable. Check it by compiling it. */
interface Printable {
public void put(Object o);
}
/*(b) Enhance classes Point and Time from Chapter 2 of the notes to implement Printable. Check them by compiling them. */
class Point implements Printable {
static Scanner sc = new Scanner(System.in);
private double x, y; // coordinates
Point(double x, double y){ // all-args constructor
this.x = x; this.y = y;
}
Point(){}; // no-args constructor (defaults apply)
void get() {
x = sc.nextDouble();
y = sc.nextDouble();
}
public String toString() {
return "(" + x + "," + y + ")";
}
double distance(Point r) { // distance from r
double xdist = x-r.x; double ydist = y-r.y;
return(Math.sqrt(xdist*xdist+ydist*ydist));
}
public void put(Object o) {
if(o==null) return;
Point p = (Point)o;
System.out.println(x + ":" + y);
}
}
class Time implements Order, Printable {
private int hours; // 0..23
private int mins; // 0..59
Time() { }
Time (int hours, int mins) {
this.hours = hours;
this.mins = mins;
}
public boolean lte(Object other) { // Note public
if (other==null) return false;
Time t = (Time) other;
return hours*60+mins<=t.hours*60+t.mins;
}
public void put(Object o) {
if(o==null) return;
Time t = (Time)o;
System.out.printf("%02d:%02d\n", t.hours, t.mins);
}
}
/*(c) Write a static method print which takes an array of objects whose class implements Printable, and prints each element in the array, one element per line. Check it by placing it in an otherwise empty class and compiling it. */
//this is the bit that is giving me grief, I've tried :
public class Exercise1 {
static void print(Printable[] a) {
for(int i = 0; i < a.length ; i++) {
a[i].put(); // put(java.lang.Object) in Printable cannot be applied to () //a[i].put();
}
}
public static void main(String[] args) {
Time[] t = new Time[10];
for(int i = 0; i < t.length; i++) {
t[i] = new Time();
}
print(t);
}
}
public interface Order {
boolean lte (Object obj); // Object for max generality
// is this object less than or equal to obj?
}
答案 0 :(得分:1)
您想要打印this
,而不是打印任意对象o
。
答案 1 :(得分:1)
我认为问题是Printable接口。它与正确的问题不符。
显示其类实现Printable
的任何对象的字符串表示形式
这并不意味着put()
方法应该具有Object
类型的参数。
此处的对象引用this
,即类实现Printable
的对象。方法put()
应打印出this
的字符串表示形式。因此,在最简单的情况下,您可以在toString()
的帮助下实现它。
interface Printable {
/**
* Display a string representation of this
*/
void put();
}
class Point implements Printable {
// ...
/**
* returns a string representation of this
*/
public String toString() {
return "(" + x + "," + y + ")";
}
public void put() {
// this is the same as System.out.println(this.toString());
System.out.println(this);
}
}
然后,您可以为数组中的每个put()
调用Printable
。
答案 2 :(得分:0)
你可能希望你的Printable方法是print(Object object),而不是put。
答案 3 :(得分:0)
您要做的是使界面Printable
看起来像这样:
interface Printable {
public void print(); //The name of the method should be similar to the name of the interface
}
然后你的课将这样工作:
public class Time implements Printable {
private int hours, minutes;
public void print() {
System.out.println( hours + ":" + minutes );
}
}
对于练习1,您将为数组的每个元素调用print()
。
顺便说一下:已经存在类似于Order
的界面。它被称为Comparable
,它看起来像这样:
public interface Comparable<T> {
public void compareTo(T other); //Returns -1 if less than, 0 if equal, and 1 if greater than other object.
}
如果没有给出参数T
,则它变为Object
。