【问题标题】:java interface method return value is itselfjava接口方法返回值本身
【发布时间】:2018-07-09 23:34:36
【问题描述】:

我是java初学者。我想在哪里创建新的追加字符串方法

MyBuffer buf = new MyBuffer(1); buf.append("This");

会将字符串 "This" 添加到 buf 但是

MyBuffer buf = new MyBuffer(1); buf.append("This"); buf.append("That");

将打印空间不足的错误。

我有 2 个 java 类和 2 个 java 接口,如下所示:

public interface MyAppendable {
public abstract MyAppendable append(String word);
}

public interface MyFlushable {
public abstract void flush();
}

public class MyBuffer implements MyFlushable, MyAppendable {
String buffer = "";
int initialSize;
int bufferSize;
public MyBuffer(int initialSize) {
    this.initialSize = initialSize;
    this.bufferSize = initialSize;
}
public MyAppendable append(String word) {
    MyAppendable myappendable = new MyBuffer(bufferSize - 1);
    if(bufferSize > 0) {
        buffer = buffer + word;
        bufferSize--;
    } else {
        System.out.println("oops, not enough space, cannot add " + word + "into buffer");
    }
    return myappendable;
}

public void flush() {
    buffer = "";
    bufferSize = initialSize;
}

public String toString() {
    return buffer;
}

}

public class MyBufferDemo {
public static void main(String[] str) {
    MyBuffer buf = new MyBuffer(5);
    buf.append("This");
    buf.append(" ");
    buf.append("is");
    buf.append(" ");
    buf.append("MyBufferDemo");
    System.out.println(buf.toString());
    buf.flush();
    buf.append("A").append("B").append("C");
    System.out.println(buf.toString());
    buf.append("D").append("E").append("F");
    System.out.println(buf.toString());
}
}

但不是

This is MyBufferDemo
ABC
oops, not enough space, cannot add F into buffer
ABCDE

输出是

This is MyBufferDemo
A
AD

实际上我对 append 方法的返回值是它自己的接口感到困惑。有可能这样做吗?谢谢。

【问题讨论】:

  • bufferSize - 1 将在您创建MyBuffer 时失败if(bufferSize > 0) 检查,其中1 为initialSize
  • 您的代码没有意义。 append() 应该改变对象或复制它,而不是两者兼而有之。
  • 我认为您将初始大小与缓冲区大小混淆了。传递给缓冲区对象的参数是缓冲区大小而不是初始大小更有意义。由于您从不将字符串传递给构造函数中的缓冲区,因此您的初始大小应为 0,缓冲区大小将是通过构造函数参数传入的大小。

标签: java string interface implements


【解决方案1】:

首先在这段代码中你真的需要这个接口来完成你正在寻找的工作吗?我不这么认为。但是,您需要像在 buf.append(“Hello”) 之前的行中那样添加字符,这应该可以。

你不能使用接口myAppendable来调用方法append,它没有显式声明,只是一个接口。

除此之外,我建议您不要在同一代码中使用接口和抽象方法。它们之间有一些区别。

【讨论】:

    【解决方案2】:

    只需检查要添加的字符串是否适合,如果可以添加它并从缓冲区中减去单词的总长度。目前,您从缓冲区中减去 1,但如果单词长度为 5 个字符,则您可能希望从缓冲区中减去 5,而不是像您目前正在做的那样 1,

    public MyAppendable append(String word) {
        if(bufferSize - word.length() >= 0) {
            buffer = buffer + word;
            bufferSize -= word.length();
            //create your copy...
            MyAppendable myappendable = new MyBuffer(this.initialSize);//i believe this should be the size of the buffer and not the initial size variable
            myappendable.buffer = this.buffer;
            myappendable.initialSize = this.initialSize;
            myappendable.bufferSize = this.bufferSize;
            return myappendable;
        } else {
            System.out.println("oops, not enough space, cannot add " + word + "into buffer");
            return this;
        }
    }
    

    最后你从不使用返回的对象,所以我不确定你为什么在 append 方法中返回一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 2019-02-10
      • 1970-01-01
      • 2017-05-09
      • 2021-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多