簡體   English   中英

Java 中的括號放在哪里?

[英]Where to put parentheses in Java in methods?

我對我正在為我的 class 學習的以下代碼感到困惑。 我想知道是否有人可以以“像我五歲一樣解釋”的方式為我分解以下部分:

(ICustomerSvc) factory.getService(ICustomerSvc.NAME);

本質上,我對為什么ICustomerSvcICustomerSvc.NAME在括號中感到困惑。

public void create (Customer cust) throws ServiceLoadException, CustomerSvcException {
    Factory factory = Factory.getInstance();
    ICustomerSvc custSvc = (ICustomerSvc) factory.getService(ICustomerSvc.NAME);
    custSvc.store(cust);
}
ICustomerSvc custSvc = (ICustomerSvc)factory.getService(ICustomerSvc.NAME);

(ICustomerSvc)正在轉換factory.getService(ICustomerSvc.NAME)的返回值,以便可以將其分配給ICustomerSvc類型的變量。

這可能是必要的,因為factory.getService返回一些其他類型,例如CustomerSvcService 我不能確切地說,因為你沒有顯示定義。

如果該返回值不是ICustomerSvc的實例(或 null),則會因ClassCastException而失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM