简体   繁体   中英

java interface method return value is itself

I am a beginner at java. I want to create new append string method where

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

would add the String "This" into buf but

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

would print the not enough space error.

I have 2 java classes & 2 java interfaces as follows:

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());
}
}

But instead of

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

the output is

This is MyBufferDemo
A
AD

And I am actually confused in the method append where the return value is its own interface. Is it possible to do that? Thank you.

First of all in this code do you really need this interfaces to complete the work you are seeking? I dont think so. However, you need to append the characters just like you did in the lines before buf.append(“Hello”) this should work.

You can't use the interface myAppendable to call method append, it dont have explicit declaration, is just an interface.

Apart of that I recomend you dont use interfaces and abstracts methods in the same code. There are a few differences between them.

Simply check if the string being added can fit, if it can then add it and subtract the total length of the word from the buffer. Currently you subtract 1 from the buffer but if the word is 5 characters long, you would want to subtract 5 from the buffer, not 1 like you are currently doing,

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;
    }
}

Lastly you never use the returned object so i'm not sure why you return one in the append method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM