「Java」抽象クラス(abstract class、abstract method)の継承を使う方法
構文
1.public abstract class クラス名
2.public abstract 変数名
親クラスの定義
package com.arkgame.study; import java.util.HashMap; import java.util.Map; public abstract class ParentMap { private Map<String, String> cftA = null; // <String,String> public abstract Map<String, String> strMap(); public ParentMap() { this.cftA = this.strMap(); } public void test() { Map<String, String> mp = new HashMap<String, String>(); if (this.cftA.containsKey("xxx")) { // some code } } }
子クラスの定義
package com.arkgame.study; import java.util.HashMap; import java.util.Map; public class ChildMapDemo extends ParentMap { @Override public Map<String, String> strMap() { Map<String, String> strMap = new HashMap<>(); strMap.put("strKey01", "A001"); strMap.put("strKey02", "B002"); strMap.put("strKey03", "C003"); return strMap; } }