我想知道如何在这个类中获得对Object的引用。我有带getter和setter方法的PDFBean类,在这个类中是一个方法createPDF(PDFBean pdf)
,我想使用像createPDF(this)
这样的语法吗?
包含title,fileName,Description字段的类:
public void print() {
PrintToPDF pdf = new PrintToPDF();
try {
if(order.isEmpty())
System.out.println("PDFBean.print() <--\norderisEmpty!");
pdf.createPdf(this);
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
我尝试使用这种语法,但我得到了一个例外NPE
createPDF():
public int createPdf(PDFBean pdf) throws IOException, DocumentException {
OfferFactory of = new OfferFactory(pdf);
Document doc = new Document(PageSize.A4, 34, 34, 110, 45);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(
RESULT));
HeaderFooter event = new HeaderFooter();
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
writer.setPageEvent(event);
doc.open();
doc.add(createTitle(of.getTitle()));
doc.add(createDescription(of.getDescription()));
doc.add(createTable(of.getOrderList()));
doc.add(createSum(of.getSum(), of.getDiscount()));
doc.close();
return 0;
}
OfferFactory:
private List<OrderLine> orderList;
private String fileName, title, description;
private Double sum, discount;
public OfferFactory(PDFBean pdf) {
if(pdf == null) {
throw new IllegalStateException("OfferFactory -> pdf is null");
}
orderList.addAll(pdf.getOrder());
fileName = pdf.getFileName();
title = pdf.getTitle();
description = pdf.getOpis();
sum = pdf.getSuma();
discount = pdf.getDiscount();
}
//getters......
答案 0 :(得分:2)
如果this
引用PDFBean
,则可以使用createPDF
关键字调用this
函数。
this
是一个java关键字,表示使用引用对象的当前实例,其中引用的对象是当前类。