我试图在Android中设计一个抽象类和方法,并通过从我的父Activity类扩展类来调用这些方法,但我不知道如何调用我的抽象方法.
MyCode:
MainActivity.java
public class MainActivity extends MyActivity { @Override public void onTest() { Log.d("MyLog", "onTest"); } }
MyActivity.java
public abstract class MyActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public abstract void onTest(); }
所以这是上面的代码片段,请告诉我,如何在Android中使用Abstract,因为我之前从未这样做过.
我开发了抽象类的示例:
抽象类:
public abstract class BaseActivity extends Activity { public static final String TAG = "Test"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(myView()); activityCreated(); } public void printMessage(String message){ System.out.print(message); } public abstract int myView(); public abstract void activityCreated(); }
非抽象类,其扩展抽象类:
public class TestActivity extends BaseActivity { @Override public int myView() { return R.layout.activity_main; } @Override public void printMessage(String message) { super.printMessage(message); } @Override public void activityCreated() { Log.i("TestActivity", "Created"); printMessage("Hello Hiren !!!"); } }
结论:
抽象方法的抽象类 必须覆盖在派生类
非抽象方法的抽象类 总是调用方法的超类
希望这一点有道理.
我想用一个例子来完成Hiren Partel Answer.
抽象类的抽象方法必须在Derived类中是Override
抽象类的非抽象方法总是调用超类的方法
如果抽象类实现接口,则可以不实现方法并让最终驱动的类实现接口方法
例如,GOD通过实现此接口(:D)控制他在地球上的生物:
public interface ?AliveCreature{ void breath(); void eat(); void move(); void die(); }
这是具有一个公共方法和一个抽象方法的抽象类实时:
public abstract class MammalAbstract implements ?AliveCreature{ public void feedBabyWithMilk(){ log.i(TAG,"baby was fed"); } abstract void haveDream(); //this is an abstract method and had to implement in the consumer class }
这最终是驱动类,人类:
public class Human extends MammalAbstract { @Override void die() { } @Override public void breath() { } @Override public void eat() { } @Override public void move() { } @Override public void haveDream() { } }
你可以看到人类必须实现抽象方法haveDream()
,并实现abstactclass接口方法!所以这是一个抽象类的强大功能,它可以处理和添加一些方法,并将其余的接口方法传递给使用者,并且非常完全用于编写库.
这是一个示例,可用于使用Abstract类注册和注销ANY BroadcastReceiver
:
BaseClass:
public abstract class BaseReceiverActivity extends AppCompatActivity{ private BroadCastReceiver receiver; private IntentFilter filter; public abstract BroadCastReceiver getReceiver(); public abstract IntentFilter getFilter(); @Override public void onStart(){ super.onStart(); configureReceiver() registerReceiver(receiver, filter); } @Override public void onStop(){ super.onPause(); unregisterReceiver(receiver); } private void registerMyReceiver(){ registerReceiver(receiver, filter); } private void configureReceiver(){ receiver = getReceiver(); filter = getFilter(); } }
子班:
public class WifiScanner extends BaseReceiverActivity{ @Override public void onCreate(Bundle sis){ super.onCreate(sis); setContentView(R.layout.yourLayout); } @Override public BroadCastReceiver getReceiver(){ return new YourReceiver(); } @Override public IntentFilter getFilter(){ return IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); } }完整的工作代码
这里