1
我有類似下面的抽象類,轉換與基類的抽象類到界面C#
public abstract class YouTubeFailureRecoveryActivity : YouTubeBaseActivity, IYouTubePlayerOnInitializedListener
{
private static int RECOVERY_DIALOG_REQUEST = 1;
public void OnInitializationFailure(IYouTubePlayerProvider provider, YouTubeInitializationResult errorReason)
{
if (errorReason.IsUserRecoverableError)
{
errorReason.GetErrorDialog(this, RECOVERY_DIALOG_REQUEST).Show();
}
else
{
String errorMessage = String.Format(GetString(Resource.String.error_player), errorReason.ToString());
Toast.MakeText(this, errorMessage, ToastLength.Long).Show();
}
}
public virtual void OnInitializationSuccess(IYouTubePlayerProvider provider, IYouTubePlayer player, bool wasRestored)
{
//throw new NotImplementedException();
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
//base.OnActivityResult (requestCode, resultCode, data);
if (requestCode == RECOVERY_DIALOG_REQUEST)
GetYouTubePlayerProvider().Initialize(KeyKeeper.YOUTUBE_API_KEY, this);
}
protected abstract IYouTubePlayerProvider GetYouTubePlayerProvider();
}
我想轉換成一個接口,這樣我可以做到以下幾點,
public class foo : INewInterface
{
//implement all the methods in the above abstract class
}
這挑戰了我的面向對象的理解,我無法找出解決方案,我總結自己說這是不可能的,但想到在這裏發佈它來確認它或找到可能的解決方案(如果有的話)。
如果你說沒可能你有什麼依據? – CodeNotFound
你爲什麼要改變它到一個界面?你可以做和現在一樣的事情。 – AntiTcb
您不能強制實現接口的類必須從抽象類繼承。但你不應該需要。抽象類應該已經實現了一個接口並通過它來使用,而不是「直接」使用。因此,您可以輕鬆地讓您的新界面從該界面繼承(可能還有其他幾個界面)。現在,每個實現新界面的類都可以自由地實現它,但是它需要。包括但不限於從抽象基類繼承。 – Corak