Answer the question
In order to leave comments, you need to log in
Delegation in interface (Java)?
At the university, one of the tasks in the lab:
“Inherit the IPrinterDelegate interface from the IPrinter interface. Extend the IPrinterDelegate interface with a method that allows you to classify the relationship between IPrinterDelegate objects and IPrintable objects as a delegation relationship (IPrinterDelegate objects are delegates, IPrintable objects are delegates). Create an example of a concrete class of the IPrinterDelegate family and provide an example of client code."
IPrinter implementation:
public interface IPrinter {
abstract void PrintString(String string);
abstract void PrintChar(char character);
}
public interface IPrintable {
abstract void Print(IPrinter printer);
}
Answer the question
In order to leave comments, you need to log in
Реализация IPrintable:
public interface IPrintable {
abstract void Print(IPrinter printer);
}
1. It's better to call print rather than printAll. The printAll method implies an IPrintable collection.
2. The names of methods according to the code convention are written with a small letter.
To complete the picture, it is better to add another implementation of IPrintable, for example:
public class SingleRowDocument implements IPrintable {
private String row;
public SingleRowDocument(String row) { this.row = row; }
public String getRow() { return row; }
public void print(IPrinter printer) {
printer.printString(row);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question