Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的接口问题_Java_Generics_Interface - Fatal编程技术网

Java中的接口问题

Java中的接口问题,java,generics,interface,Java,Generics,Interface,我正在完成一份关于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 obj

我正在完成一份关于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?
}
/*练习2
(a) 编写一个接口Printable,该接口有一个方法put,其目的是显示其类实现Printable的任何对象的字符串表示形式。通过编译来检查它*/
接口可打印{
公开作废认沽权证(o标的);
}
/*(b) 从第2章的注释中增加课程的学习点和时间,以实现可打印性。通过编译来检查它们*/
类点实现可打印{
静态扫描仪sc=新扫描仪(System.in);
私有双x,y;//坐标
点(双x,双y){//所有参数构造函数
这个.x=x;这个.y=y;
}
Point(){};//没有参数构造函数(默认值适用)
void get(){
x=sc.nextDouble();
y=sc.nextDouble();
}
公共字符串toString(){
返回“(“+x+”、“+y+”)”;
}
双距离(点r){//距离r
双xdist=x-r.x;双ydist=y-r.y;
返回(Math.sqrt(xdist*xdist+ydist*ydist));
}
公开作废认沽权(对象o){
如果(o==null)返回;
点p=(点)o;
System.out.println(x+“:”+y);
}
}
类时间实现顺序,可打印{
私有整数小时;//0..23
私有整数分钟;//0..59
时间(){}
时间(整数小时,整数分钟){
这个.小时=小时;
this.mins=分钟;
}
公共布尔lte(对象其他){//注意公共
if(other==null)返回false;
时间t=(时间)其他;

返回小时数*60+分钟您想打印
,而不是一些任意对象
o
您可能希望您的可打印方法是打印(对象对象),而不是放置。

我认为问题在于接口可打印。它与问题不匹配

显示其类实现可打印的任何对象的字符串表示形式

这并不意味着
put()
方法应该具有
Object
类型的参数

这里的对象指的是
this
,类实现
Printable
的对象。方法
put()
应该打印出this
的字符串表示形式。因此,在最简单的情况下,您可以借助
toString()
来实现它


然后,您可以为数组中的每个可打印的
调用
put()

您要做的是使接口可打印的
如下所示:

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 );
    }
}
public interface Comparable<T> {

    public void compareTo(T other); //Returns -1 if less than, 0 if equal, and 1 if greater than other object.

}
然后,您的课程将按如下方式运行:

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 );
    }
}
public interface Comparable<T> {

    public void compareTo(T other); //Returns -1 if less than, 0 if equal, and 1 if greater than other object.

}
对于练习1,然后为数组的每个元素调用
print()

顺便说一下:已经有一个类似于
Order
的界面。它被称为
Comparable
,它看起来像这样:

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 );
    }
}
public interface Comparable<T> {

    public void compareTo(T other); //Returns -1 if less than, 0 if equal, and 1 if greater than other object.

}
公共接口{
public void compareTo(T other);//如果小于,则返回-1;如果等于,则返回0;如果大于其他对象,则返回1。
}

如果未给出参数
T
,它将变为
Object

+1以进行计数,这是一个合理的问题