亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Java 中, 是否有方法, 強(qiáng)制某 method 不接受 null 類型參數(shù) ?
天蓬老師
天蓬老師 2017-04-18 10:34:33
0
2
499

讓某 method 明確聲明: 我不接受 null 參數(shù), 傳了 null , 我馬上崩潰 !

并且, 該拋錯在編譯階段拋出, 不要跑到運(yùn)行時去

天蓬老師
天蓬老師

歡迎選擇我的課程,讓我們一起見證您的進(jìn)步~~

reply all(2)
PHPzhong

Java's type system does not support it and can only be implemented through plug-ins: Checker Framework, you need to add annotations and specify the plug-in during compilation.

The above implements the compiler's detection of null. As for the crash if null is passed, this can be achieved by inserting null check code through a plug-in.

巴扎黑

Good question. I usually only focus on how to implement functions, but I haven’t thought much about why Java doesn’t implement them this way, such as this question. It also evoked a lot of wild imagination in me. It would be great if Java syntax was supported. The development efficiency would be at least several times higher. But when I think about it again, something seems wrong.

NullPointerException is a runtime exception for a certain reason

Assuming Java supports such syntax, use annotation: @NotNullIndicate that the parameter is not empty

A scenario like this: After the user successfully logs in, update the user's login time and IP.

User data includes:

name password lasttime ip
aaa 123456 2017-01-09 192.168.1.1
bbb 123456 2017-01-08 192.168.1.1

Update user information pseudo code:

public void updateAccount(@NotNull Account account) {
    // 因?yàn)榭隙ú粸榭?,可以放心大膽的更新Account的最新登錄時間、ip
}

@NotNull表示參數(shù):account means the parameter: account cannot be empty, there is no problem here.

Get user information to verify login pseudo code:

public Account login(String name) {
    // 1、連接數(shù)據(jù)庫
    // 2、根據(jù)用戶名獲取Account
    // 3、驗(yàn)證用戶信息
    // 4、驗(yàn)證成功返回Account信息,驗(yàn)證失敗返回null
}

We just make sure that the parameters cannot be empty, and we don’t want the return value to be empty, so there is no problem.

Main logic judgment pseudo code:

public static void main(String[] args) {
    AccountService service = new AccountService();
    // aaa登錄
    Account account = service.login("aaa","123456");
    service.updateAccount(account);
    
    // bbb登錄
    Account account = service.login("bbb","123456");
    service.updateAccount(account);
    
    // ccc登錄
    Account account = service.login("ccc","123456");
    service.updateAccount(account);
}

Then the question arises, whether the account parameter can be passed inservice.updateAccount()方法,編譯能不能通過?從數(shù)據(jù)庫中讀取的Accoount對象本來就是個模凌兩可對象,有可能代表某個人,也有可能是null.

Then make the return value non-null?

Yes, it can be like this. This becomes a pattern called NullObject Pattern , which means to create a dedicated empty object to represent that the result is empty.

Please check for details:

  • https://segmentfault.com/q/10...

  • http://www.cnblogs.com/haodaw...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template