首页 > 解决方案 > Java接口和信息输出到控制台的问题

问题描述

最初,我的一段代码如下所示:

public interface Picture {
    public String toString();
}

class PolyLine extends Line implements Picture{
    int numSides =0;
    Line sideLengths[];
    public PolyLine(Line[] l1) {
        super(l1);
        sideLengths = l1;
        numSides = l1.length;
    }

    @Override
    public String toString() {
        
        return " "+"number of sides "+ numSides + " "+ "coordinates of the beginning and end of the lines that make up the polygon\n"+ Arrays.toString(sideLengths) + " "  ;
    }
}

而且一切都很好。但是由于我可以不使用接口来实现String toString方法,所以有点没用,但是我有这样的任务。所以我决定让接口中的方法有点不同。但是一切都停止了工作。也就是说,信息已停止输出到控制台。但我不知道为什么。再次,这是我的任务。我知道我可以使用第一个版本,但我想知道为什么第二个版本不起作用。

public interface Picture {
    public void Draw();
}

class PolyLine extends Line implements Picture{
    int numSides =0;
    Line sideLengths[];
    public PolyLine(Line[] l1) {
        super(l1);
        sideLengths = l1;
        numSides = l1.length;
        
    }

    @Override
    public void Draw() {
        
        System.out.println(" "+"number of sides "+ numSides + " "+ "coordinates of the beginning and end of the lines that make up the polygon\n"+ Arrays.toString(sideLengths) + " " ) ;
    }
}

完整代码:

import java.util.Arrays;

abstract class Point {
 Double x;
 Double y;

public Point(double x, double y) {

    this.setX(x);
    this.setY(y);
    }

    public double getX() {
    return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
        }
    public Point() {
        x=0.0;
        y=0.0;
        
    }
    public String toString() {
        
        return x + " " + y + "\n";
    }
}
class ColoredPoint extends Point{
    public Color color;
    
    public ColoredPoint (double x, double y,Color color) {
    super(x,y); 
    this.color=color;
    }
    public Color getColor() {
    return color;
    }
    public void setColor(Color color) {
        this.color = color;
    }
    public ColoredPoint () {
        x=0.0;
        y=0.0;
        color=Color.getRandomColor();
    }
    @Override
    public String toString() {
        
        return"("+ x + " " + y +") "+ " " + color + "\n";
    }
}
class Line extends Point{
    public Point start;
    public Point end;
    public Line(Point p1, Point p2) {
        start = p1;
        end = p2;   
    }
    public Line(Line[] l1) {
        // TODO Auto-generated constructor stub
    }
    public double Length()
    {
        return Math.sqrt(Math.pow((end.x - start.x), 2) + Math.pow((end.y - start.y),2));
    }
    @Override
    public String toString() {
        
        return "start point "+ start + " "+ "end point "+ end + " " + "lenght "+Length() ;
    }
}
class ColoredLine extends Line {
    public Color color;
    public ColoredLine(Point p1, Point p2,Color color ) {
        super(p1, p2);
        this.color=color;
    }
    public Color getColor() {
    return color;
    }
    public void setColor(Color color) {
        this.color = color;
    }
    @Override
    public String toString() {
        
        return "start point "+ start + " "+ "end point "+ end + " " + "lenght "+Length() + " \n line color "+color ;
    }
    
}
class PolyLine extends Line implements Picture{
    int numSides =0;
    Line sideLengths[];
    public PolyLine(Line[] l1) {
        super(l1);
        sideLengths = l1;
        numSides = l1.length;
        // TODO Auto-generated constructor stub
    }

    @Override
    public void Draw() {
        
        System.out.println(" "+"number of sides "+ numSides + " "+ "coordinates of the beginning and end of the lines that make up the polygon\n"+ Arrays.toString(sideLengths) + " " ) ;
    }
}
public class FourthLab {

    public static void main(String[] args) {
        
        ColoredPoint A = new ColoredPoint(3.5,4.0, Color.Blue);
        System.out.println("A = " + A.toString());
        ColoredPoint B = new ColoredPoint();
        System.out.println("B = " + B.toString());
        ColoredPoint C = new ColoredPoint(0.0,4.0, Color.Orange);
        System.out.println("C = " + C.toString());
        Line L1 = new Line (A,B);
        Line L2 = new Line (B,C);
        Line L3 = new Line (C,A);
        System.out.println("L1 = \n" + L1.toString());
        Line Colored_L1 = new ColoredLine (A,B,Color.getRandomColor());
        System.out.println("L1_colored = \n" + Colored_L1.toString());
        Line[] l1 = {L1,L2,L3};
        PolyLine Triangle = new  PolyLine (l1) ;
        System.out.println("Triangle = \n" + Triangle.toString());

    }

}

标签: javainterfacetostringimplementation

解决方案


Picture您在第一个版本中看到的输出与界面无关。您看到它是因为您正在将实例打印到 STDOUT,例如:

System.out.println(myObj);

这反过来使用toString()您覆盖的方法。

尝试:

Systenm.out.println(myObj.Draw());

带有 a 的接口public String toString();是无用/无意义的,因为由于Object类具有该方法,因此每个对象都有该方法。


推荐阅读