在16年软考中看到的一道题,super的方法是如何调用printInvoice(),没搞懂
代码:
public class Invoice {
public void printInvoice(){
System.out.println("This is the content of the invoice!");
}
}
class Decorator extends Invoice{
protected Invoice ticket;
public Decorator(Invoice t){
ticket = t;
}
public void printInvoice() {
if (ticket!=null){
ticket.printInvoice();
}
}
}
class HeadDecorator extends Decorator {
public HeadDecorator(Invoice t){
super(t);
}
public void printInvoice(){
System.out.println("This is the header of invoice!");
super.printInvoice();
}
}
class FootDecorator extends Decorator {
public FootDecorator(Invoice t){
super(t);
}
public void printInvoice(){
super.printInvoice();
System.out.println("This is the footnote of the invoice!");
}
}
class test {
public static void main(String[] args){
Invoice t = new Invoice();
Invoice ticket;
ticket = new HeadDecorator(new FootDecorator(t));
ticket.printInvoice();
System.out.println("-------------");
ticket = new HeadDecorator(new FootDecorator(null));
ticket.printInvoice();
}
}
输出:
This is the header of invoice!
This is the content of the invoice!
This is the footnote of the invoice!
-------------
This is the header of invoice!
This is the footnote of the invoice!
想问下super的调用方法是怎么调用的,只调用一次吗?