亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

隔離注解的實例化

隔離注解的實例化

尚方寶劍之說 2023-07-28 10:19:33
我有一個巨大的@OpenApi注釋(基本上它是Javalin?/Kotlin 端點的文檔),它占據了很多行:public class ModuleGrader {? ? final int examID = 123;? ? //String excellent=null;? ? //String good=null;? ? //String satisfactory=null;? ? //String compensatableFail=null;? ? //String outrightFail=null;? ? int grade;? ? public String gradeModule(int mark) {? ? ? ? String result = null;? ? ? ? if (mark>=70 && mark<=100)?? ? ? ? {? ? ? ? ? ? result = "excellent";? ? ? ? ? ? System.out.println(" ");? ? ? ? ? ? }? ? ? ? else if (mark>=60 && mark<=69)? ? ? ? {? ? ? ? ? ? result = "good";? ? ? ? }? ? ? ? else if (mark>=50 && mark<=59)? ? ? ? {? ? ? ? ? ? result = "satisfactory";? ? ? ? }? ? ? ? else if (mark>=40 && mark<=49)? ? ? ? {? ? ? ? ? ? result = "compensatableFail";? ? ? ? }? ? ? ? else if (mark>=0 && mark<=39) {? ? ? ? ? ? result = "outrightFail";? ? ? ? }? ? ? ? else {? ? ? ? ? ? System.out.println("Invalid entery, please insert an number between 100-0");? ? ? ? ? ? ? ? ? ? }? ? ? ? return result;? ? }
查看完整描述

2 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

您可以定義單獨的注釋:


annotation class MyOwnApi(

    val openApi: OpenApi = OpenApi(

          summary = "",

          description = "Lists all customers",

          path = "customers",

          queryParams =

          // ...........

          // ...........

          // etc

        )

)


annotation class UserOpenApi(

        val openApi: OpenApi = OpenApi(

              summary = "Something",

              description = "Lists all users",

              // ...........

              // ...........

              // etc

            )

    )

優點:

  • 代碼分離

  • 可重用的注釋類

  • 您甚至可以創建一個構建器類并測試它

缺點:

  • 令人困惑

  • 注解不能繼承、擴展類或實現接口

  • 如果直接檢查類/對象,可能無法@OpenApi實現或需要復雜的代碼更改。在這種情況下,您將需要另一個反射搜索來從字段中提取注釋!


查看完整回答
反對 回復 2023-07-28
?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

好的,您想要的是@OpenApi將文檔與 REST 處理程序代碼分開。您可以通過刪除實現而不是刪除注釋來做到這一點。


因此,在所有注釋與 REST 處理程序代碼混合的當前文件中@OpenApi,您可以調用輔助函數,如下所示:


@OpenApi(

   summary = "",

   description = "Lists all customers",

   path = "customers",

   queryParams =

   // ...........

   // ...........

   // etc

)

override fun handle(context: Context) {

   handleGetCustomers(context)

}

然后您將所有 REST 處理程序放入該文件的頂部或另一個文件中以進行更多隔離,這樣您就可以在它們之間滾動,而不會受到注釋的干擾@OpenApi:


// Collected at the top of the file, or in a separate file

fun handleGetCustomers(context: Context) {

    // body of the REST handler

}

然后,您可以輕松地在 REST 處理程序代碼之間滾動,而不會受到@OpenApi噪音的困擾。


請注意,您應該使用Android Studio 的“轉到” -> “定義”功能,以避免滾動到handleGetCustomers().


查看完整回答
反對 回復 2023-07-28
  • 2 回答
  • 0 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號