- IntelliJ IDEA 2021.2.3
å¤ãªãã¸ã§ã¯ããªã¯ã©ã¹ãå¼å¸ãããããã«ä½ã£ã¦ããã¨ããããããåãããï¼ãã¨ãªããã¨ããã¾ã«ããã¾ãã 使ç¨ç®æãå°ãªããã°æä½æ¥ã§ãã¼ã¸ãã¦ãããã®ã§ãããå¤ãã®ç®æã§ä½¿ã£ã¦ããã¯ã©ã¹ã ã¨å¤§å¤ã æååã®ä¸æ¬ç½®æã§ãããªãã¯ãªããã§ãããæ£è¦è¡¨ç¾åã試ããã¾ããã誤置æãæãã§ããimportæã¨ãããããåãååãé¨åçã«ä½¿ã£ã¦ããã¯ã©ã¹ãã¡ã½ãããç½®æãã¦ãã¾ãã¨å°ã£ããã¨ã«ãªãã¾ããï¼ããªãã äºæ ã£ããï¼
ããããæãInline Super Classï¼è¦ªã¯ã©ã¹ã®ã¤ã³ã©ã¤ã³åï¼ã使ãã¾ãã
ä¾
ã¡ãã£ã¨é·ãã§ãããå ¨ä½ãè²¼ãã¾ãã試ãã¦ã¿ããããã¯ã©ããã
class OrderService { OrderNumber æ°è¦æ³¨æ(OrderDetail orderDetail) { // ... ä½ããã®å¦ç OrderNumber orderNumber = new OrderNumber("dummy"); return orderNumber; } Order 注æ詳細(ReceiptNumber receiptNumber) { if (!receiptNumber.isValid()) { throw new IllegalArgumentException(); } // ... ä½ããã®å¦ç Order order = new Order(receiptNumber, new OrderDetail()); return order; } BillingNumber è«æ±(OrderNumber orderNumber) { // ... ä½ããã®å¦ç BillingNumber billingNumber = orderNumber.toBillingNumber(); return billingNumber; } } /** åä»çªå· */ class ReceiptNumber { String value; ReceiptNumber(String value) { this.value = value; } String value() { return value; } boolean isValid() { return !value.isBlank(); } } /** 注æçªå· */ class OrderNumber { String value; OrderNumber(String value) { this.value = value; } String value() { return value; } BillingNumber toBillingNumber() { // ä½ããã®ã«ã¼ã«ã«å¾ã£ã¦çæãã return new BillingNumber(); } } /** 注æ */ record Order(ReceiptNumber receiptNumber, OrderDetail orderDetail) { } /** 注æå 容 */ class OrderDetail { } /** è«æ±çªå· */ class BillingNumber { }
ããã®ãReceiptNumber 㨠OrderNumber ãåãã§ãããããã¨æ°ã¥ããã¨ãã¾ããããï¼ç»å ´å ´é¢ãã¡ã½ãããéãããéãã§ãããããªãã ãã©ãä»åã¯ã¯ã©ã¹ã®ãã¼ã¸ã®ç´¹ä»ãªã®ã§ç®ãçã£ã¦ãã ãããï¼
å®è¡ã¹ããã
- ãã¹ããéãã®ã確èªãã
- çæ¹ã§ããçæ¹ãç¶æ¿ãã
- ãã¹ããéãã®ã確èªãã
Inline Super Class...
ãå®è¡ãã- ãã¹ããéãã®ã確èªãã
- æ°ã«å ¥ããªãã¡ã½ããã®ä¸¦ã³é ãå¤æ°åãç´ã
- ãã¹ããéãã®ã確èªãã
- ã³ããããã
æä½èªä½ã¯1åãããã¾ããããã¡ã¤ã«æ°ã«ãã£ã¦ã¯ã¤ã³ã©ã¤ã³åã«ãããããããããããã¾ããããã¹ãã®å®è¡æéã¯åªåãã¦ãã ããã
åæç¶æ
çæ¹ã§ããçæ¹ãç¶æ¿ãã
ãã¼ã¸ãªãã§ã©ã£ã¡ãã©ã£ã¡ã§ãæ§ãã¾ããã
å¯ãããæ¹ãããã°ãã¡ããåã¯ã©ã¹ã«ããã°ããã§ãããã©ã¡ãã«ãããã¼ã¸å¾ã«ååå¤æ´ããã°ããã ãã®è©±ã§ãã
extends
ãã¦ãã³ã³ã¹ãã©ã¯ã¿ã§ super
å¼ã¶ããã«ãã¦ããã£ã¼ã«ããæ¶ãã
-class OrderNumber { +class OrderNumber extends ReceiptNumber { - String value; OrderNumber(String value) { - this.value = value; + super(value); } - String value() { - return value; - }
ãã®ä½æ¥ã¯æä½æ¥ã«ãªãã¾ãããªã®ã§ãã¹ãå¿ é ã
ãã£ã¼ã«ãã private
ã ã¨ä½¿ç¨ãã¦ããã¡ã½ãããã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ãããä¸æçã«protected
ã«ããã¨ããã¢ã¯ã»ãµã¡ã½ãããä»ãããªã©ã§å¯¾å¿ã§ãã¾ãã
Inline Super Class...
ãå®è¡ãã
extends
ã®å¾ãï¼ä»å㯠ReceiptNumber
ï¼ã«ã«ã¼ã½ã«ãæã£ã¦è¡ã£ã¦ Inline Super Class...
ãå®è¡ãã¾ãã
ç§ã¯Refactor Thisã«Hotkeyãå
¥ãã¦ãã®ã§åé ã®ç»åã®ããã«ãªãã¾ãããã³ã³ããã¹ãã¡ãã¥ã¼ãããªããã®è¾ºã§ããï¼â¥âN
ãªãã¦è¦ãã¦ã¾ããããªããæã¯åæã«ä½¿ããã©ãï¼
é¸æãã¤ã¢ãã°ã§ã¯ãã¯ã©ã¹ããã¼ã¸ãããã®ã§è¦ªã¯ã©ã¹ã«æ¶ãã¦ãããæ¹ãé¸ã³ã¾ããkeepãããªãremoveã®æ¹ã
çµæã以ä¸ã®ãã㪠OrderNumber
ã«ãªãã¾ãã
class OrderNumber { String value; OrderNumber(String value) { this.value = value; } BillingNumber toBillingNumber() { // ä½ããã®ã«ã¼ã«ã«å¾ã£ã¦çæãã return new BillingNumber(); } String value() { return value; } boolean isValid() { return !value.isBlank(); } }
ReceiptNumber
ã«ãã£ãã¡ã³ããæã£ã¦ãããã¦ã¾ããprivateã¡ã½ãããstaticã¡ã½ããããã£ã¦ãæã£ã¦ãã¦ããã¾ãã
ããã«å ããReceiptNumber
ã使ç¨ãã¦ããã³ã¼ãããã¹ã¦OrderNumber
ã«ç½®ãæãããã¾ããåã®ä½¿ç¨ç®æã§å¦çãã¦ãããã®ã§ãæååç½®æã¨ã¯å®å¿æã段éãã§ãã
super
å¼ã³åºããã¤ã³ã©ã¤ã³åããããããåã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¯super
å¼ã³åºã以å¤ãããªããã¨ãä»ã®å¦çãããã¦ããã¨æåãå¤ãããã¨ã«ãªãã¾ããã¾ããåãã¡ã½ãããå®ç¾©ããã¦ããå ´åï¼ä»åã 㨠value()
ãåé¤ããªãã£ãå ´åï¼ãè¦åãã¤ã¢ãã°ãåºã¦ãã¾ããã©ãã¨ã§ããªãã®ã§ãããæãã«ãã¾ãããã
ä¸æ°ã«å¤ãã£ã¦ã¾ãããåå¥ã«ã¯ä»¥ä¸ã«ãªãã¾ãããªãã¡ã³ãã®push downã¯ãªãã¡ã¯ã¿ãªã³ã°ã¡ãã¥ã¼ããåå¥ã«å®è¡ã§ãã¾ãã
ããããã®ã¯ã使ç¨ã¯ã©ã¹ã®å¤æ´ããªãã§ããããããå®å ¨ã«è¡ãã®ãæ¬ç¨¿ã®å 容ã«ãªãã¾ãããããªãã®è¦æ¨¡ã§æ ¹åºã®ãã¡ã¤ã³ãå¤ããã¨ãæ°ç¾ãã¡ã¤ã«åæã«ç·¨éãããããã¾ããæä½æ¥ã§ã¯é¢åã§ä¸å®ã
ããæ¸ããåæ©
åã¨IDEã®æ©è½ã使ãã°ç´ æ©ãå®å®ãã¦ã³ã¼ããããããåãã¾ãããã®ä¸ä¾ã
å¤ãªãã¸ã§ã¯ããä½ãã¨ãã¯ã©ã¹ãããããã«ãªãéãã¦å°ãã®ã§ã¯ãã¿ãããªã®ãããã¾ããããã®å°ããã¨ã®ã²ã¨ã¤ã«ãéãã¨æã£ã¦ããã©å®ã¯åãã ã£ããã¨ããã®ãããã¾ãããã®æã«æéã¨ç²¾ç¥åãæ¶è²»ãã¦å¿ ã誤ãæä½æ¥ï¼é ·ã表ç¾ã ï¼ããç¥ãã®æååç½®æãè¡ãã®ã§ã¯æ°ãéããããä½ããã¨ããæã«ãéãããã«è¦ãããã©æ¬å½ã¯åããªã®ã§ã¯ï¼ãã¨ç©¿ã£ã¦ã¿ãå¿ è¦ãåºã¦ãã¾ãã
ä¸å¡ã®ãã®ãåãããããåããã¦ãããã®ããã£ã¤ããã»ãããã»ã©ç°¡åã§ãã
ãã¡ããä¾å¤ã¯ãããã©ãä¸å¡ã®ãã®ãåé¢ããã®ã¯ãªããªãæ©æ¢°çã«ã¯ã§ãã¾ãããããã¤ãã§ãããã¯ããã¯ã¯ããã¾ãï¼ããã¯ã¾ããã®ãã¡â¦â¦ã¨è¨ã£ã¦æ¸ãã¦ãªãã®ã©ãã ãããã ããï¼ãããã¤ãã§ããããã§ã¯ããã¾ããã ä¸æ¹ãåããã¦ãããã®ããã£ã¤ããã®ã¯ã»ã¼ã»ã¼æ©æ¢°çã«ã§ãã¾ãããæ©æ¢°çã«ã§ãããã®ã¯ä»åã®ããã«IDEã§ä¸çºã ã£ãããã¾ãã
ã¦ãã¨ã§ãã¾ãç¥èãååã§ãªãæã¯ããªããéããã®ã£ã½ããªããã¨æãããããããããã¯ã©ã¹ãä½ã£ã¦ããã¨ããã¨æãã¾ããåããã©ãããå¤æããã®ã¯å¾ããã§ãã§ããããåãã¨æ±ã£ã¦ãããã®ãå¾ããåãããããããéãã¨æã£ã¦ãããã®ãå®ã¯åãã ã£ããããã£ã¤ãããã®æ¹ã楽ã ã¨æãã¾ãã
ãã¤ã¼ãã®ãªã³ã¯ãã¾ããªãã ãè¦ã¯ãã©ã³ã¹ã§ãããã¯ãã
ãã¾ã: ç°¡åã§ä¸å®å®ãªå¥è§£
å°ã ä¹±æ´ã§ããã以ä¸ã®æ¹æ³ãããã¾ãã
- 2ã¤ã®ã¯ã©ã¹ã®å 容ãä¸è´ããããï¼ã³ãããï¼
- çæ¹ã®ã¯ã©ã¹ããã¡ã¤ã«ãã¨åé¤ãããï¼ä½¿ç¨ç®æãããã¨ãè¦åããã¾ããç¡è¦ãã¦å®è¡ãã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªããï¼
- æ®ã£ãæ¹ã®ååãåé¤ããæ¹ã®ååã«å¤æãããï¼ãªãã¡ã¯ã¿ãªã³ã°æ©è½ã使ç¨ãããï¼
ã³ã³ãã¤ã«ã¨ã©ã¼ç¶æ ã§ã®ãªãã¡ã¯ã¿ãªã³ã°æ©è½ã¯ç²¾åº¦ãè½ã¡ãã®ã§æ¨å¥¨ã¯ãã¾ãããæè¿ã¯ã ãã¶ããæãã«åãã¾ãããå®å¿ãã¦ä½¿ããã¨ã¯è¨ãé£ãã§ãã ãã®æé ã§ã¯ã2çªç®ã§ãã¡ã¤ã«ãåé¤ããªãã£ããããã¡ã¤ã«åãä¸è´ãããã¨ãã§æããã¾ãã
ããã«æ´¾çã§åé¤ãã代ããã«åç §å ã¢ã¸ã¥ã¼ã«ã«æã£ã¦ãããã¨ã§ã³ã³ãã¤ã«ã¨ã©ã¼ãåé¿ããã¨ããããã¾ããããã«ãã¢ã¸ã¥ã¼ã«æ§æã§ãªããã°ã§ãã¾ãããã大æããã«ãªãã®ã§ä»ã®äºæ è¦å ãåºã¦ãã¾ããã©ããã¦ãå¿ è¦ãªæã«ã£ã¦æãã§ããã
è足: recordã®ã¨ã
recordã使ãã¨ç¶æ¿ã使ç¨ã§ããªãã®ã§ãç´ ç´ã«ã¯ã§ããªããªãã¾ããç¾æ®µéã§ã¯ä¸æ¦recordãclassã«æ¸ãæãã¦ä¸è¨ã®æé ãå®æ½ããã®å¾recordã«æ»ãâ¦â¦ããªãã¨æã£ã¦ã¾ããIntelliJ IDEAã§classã¨recordã®å¤æã¯ç¸äºã«ã§ããã®ã§ããããå¥ã«æéãããªãããªã£ã¦ã
ãããã¦ãã¿ããï¼
ã©ã¤ãã³ã¼ãã£ã³ã°ä¸ã«ä½¿ã£ãIntelliJ IDEAã®æ©è½ãé©å½ã«ç´¹ä»ãã¦ã¾ããä»åæ¸ããã®ã¯æèãã¦ä½¿ãæ©è½ã§ããããªã³ã¯å ã§æãã¦ãã®ã¯ã»ãã¨ç¡æèã«ä½¿ãã¬ãã«ã®ãã®ã