首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从另外两个抽象类构造抽象类

从另外两个抽象类构造抽象类
EN

Stack Overflow用户
提问于 2014-03-18 08:41:59
回答 2查看 61关注 0票数 1

从另外两个抽象类构造抽象类。例如

代码语言:javascript
复制
public abstract class PlayerBase
{
    public bool IsPlaying { get; set; }
    public abstract void Play();
}
public abstract class VideoPlayerBase : PlayerBase
{
    public abstract void Rotate();
}
public abstract class AudioPlayerBase : PlayerBase
{
    public abstract void Mute();
}

我想要的是使用另外两个(VideoPlayerBase,AudioPlayerBase)抽象类来构建一个新的VedioAudioPlayerBase抽象类,而不需要太多的重复工作。

代码语言:javascript
复制
public abstract class VedioAudioPlayerBase
{


}

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-18 08:45:15

C#不支持类的多继承,但它确实支持接口的多个实现。

因此,您可以做的是将适当的方法提取到IVideoPlayerIAudioPlayer接口中,然后编写一个实现这两种方法的类。

不幸的是,这意味着您不能继承任何实现。

但是,您可以将音频和视频逻辑封装在实现IVideoPlayer和IAudioPlayer的类中,并将它们用作AudioVideoPlayer类的实现:

代码语言:javascript
复制
public interface IPlayer
{
    bool IsPlaying
    {
        get;
        set;
    }

    void Play();
}

public interface IVideoPlayer: IPlayer
{
    void Rotate();
}

public interface IAudioPlayer: IPlayer
{
    void Mute();
}

public interface IAudioVideoPlayer: IVideoPlayer, IAudioPlayer
{
}

public abstract class PlayerBase: IPlayer
{
    public bool IsPlaying
    {
        get;
        set;
    }

    public abstract void Play();
}

public abstract class VideoPlayerBase: PlayerBase, IVideoPlayer
{
    public abstract void Rotate();
}

public abstract class AudioPlayerBase: PlayerBase, IAudioPlayer
{
    public abstract void Mute();
}

public class VideoPlayer: VideoPlayerBase
{
    public override void Play()
    {
        Console.WriteLine("VideoPlayerBaseImpl:Play()");
    }

    public override void Rotate()
    {
        Console.WriteLine("VideoPlayerBaseImpl:Rotate()");
    }
}

public class AudioPlayer : AudioPlayerBase
{
    public override void Play()
    {
        Console.WriteLine("AudioPlayerBaseImpl:Play()");
    }

    public override void Mute()
    {
        Console.WriteLine("AudioPlayerBaseImpl:Mute()");
    }
}

public class AudioVideoPlayer: IAudioVideoPlayer
{
    public void Rotate()
    {
        _videoPlayer.Rotate();
    }

    public void Mute()
    {
        _audioPlayer.Mute();
    }

    public bool IsPlaying
    {
        get
        {
            return _audioPlayer.IsPlaying && _videoPlayer.IsPlaying;
        }

        set
        {
            _audioPlayer.IsPlaying = value;
            _videoPlayer.IsPlaying = value;
        }
    }

    public void Play()
    {
        _audioPlayer.Play();
        _videoPlayer.Play();
    }

    private readonly AudioPlayer _audioPlayer = new AudioPlayer();
    private readonly VideoPlayer _videoPlayer = new VideoPlayer();
}
票数 2
EN

Stack Overflow用户

发布于 2014-03-18 09:01:07

C#不支持实现的多重继承。在这种情况下,标准的方法是使用接口和组合:

代码语言:javascript
复制
public interface IPlayerBase
{
    IsPlaying { get; set; }
    void Play();
}
public interface IVideoPlayerBase : PlayerBase
{
    void Rotate();
}
public interface IAudioPlayerBase : PlayerBase
{
    void Mute();
}


public abstract class AbstractVideoPlayer : IVideoPlayerBase 
{
    public void Rotate() {
       // implementation
    }
}
public abstract class AbstractAudioPlayer : IAudioPlayerBase 
{
    public abstract void Mute() {
       // implementation
    }
}


public abstract class VedioAudioPlayerBase : IVideoPlayerBase, IAudioPlayerBase
{
    IVideoPlayerBase video;
    IAudioPlayerBase audio;

    public void Rotate() {
       video.Rotate();
    }

    public void Mute() {
       audio.Mute();
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22474143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档