0
namespace ConsoleApplication1
{
    class Program
    {

static void Main(string[] args)
        {
            CAudio Music = new CMusic(new string[] { "One", "Two", "Three" }, 120, "Hardcoded Title", "Advisory Music", new DateTime(2001, 05, 17), new TimeSpan(00, 05, 23), "Album", new string[] { "com1", "com2", "com3" }, "Genre");

            if (Music is CMusic)
            {
                Console.Write(Music.GetDetails());
            }
        }
    }

    abstract class CAudio
    {
        public CAudio(string[] artists, int bitrate, string title, string publisher, System.DateTime releaseDate, System.TimeSpan length)
        {
            this.Length = length;
            this.ReleaseDate = releaseDate;
            this.Publisher = publisher;
            this.Title = title;
            this.Bitrate = bitrate;
            this.Artists = artists;

        }

        private string sTitle;
        public string Title
        {
            get { return sTitle; }
            set { sTitle = value; }
        }

        private string sPublisher;
        public string Publisher
        {
            get { return sPublisher; }
            set { sPublisher = value; }
        }

        private DateTime dtReleaseDate;
        public DateTime ReleaseDate
        {
            get { return dtReleaseDate; }
            set { dtReleaseDate = value; }
        }

        private TimeSpan tsLength;
        public TimeSpan Length
        {
            get { return tsLength; }
            set { tsLength = value; }
        }

        private string[] sArtists;
        public string[] Artists
        {
            get { return sArtists; }
            set { sArtists = value; }
        }

        private int iBitrate;
        public int Bitrate
        {
            get { return iBitrate; }
            set { iBitrate = value; }
        }

        public string GetDetails()
        {
            string sDetails = "";
            sDetails = String.Concat(sDetails, "Title: ", Title, "\n");
            sDetails = String.Concat(sDetails, "Publisher: ", Publisher, "\n");
            sDetails = String.Concat(sDetails, "ReleaseDate: ", ReleaseDate.ToShortDateString(), "\n");
            sDetails = String.Concat(sDetails, "Length: ", Length, "\n");
            sDetails = String.Concat(sDetails, "Artists: ", string.Join(", ", Artists), "\n");
            sDetails = String.Concat(sDetails, "Bitrate: ", Bitrate, "\n");

            return sDetails;
        }

    }

    class CMusic : CAudio
    {
        private string sAlbum;
        public string Album
        {
            get { return sAlbum; }
            set { sAlbum = value; }
        }

        private string[] sComposers;
        public string[] Composers
        {
            get { return sComposers; }
            set { sComposers = value; }
        }

        private string sGenre;
        public string Genre
        {
            get { return sGenre; }
            set { sGenre = value; }
        }

        public new string GetDetails()
        {
            string sDetails = "";
            sDetails = String.Concat(sDetails, "Title: ", Title, "\n");
            sDetails = String.Concat(sDetails, "Publisher: ", Publisher, "\n");
            sDetails = String.Concat(sDetails, "ReleaseDate: ", ReleaseDate.ToShortDateString(), "\n");
            sDetails = String.Concat(sDetails, "Length: ", Length, "\n");
            sDetails = String.Concat(sDetails, "Artists: ", string.Join(", ", Artists), "\n");
            sDetails = String.Concat(sDetails, "Bitrate: ", Bitrate, "\n");
            sDetails = String.Concat(sDetails, "Album: ", Album, "\n");
            sDetails = String.Concat(sDetails, "Composers: ", string.Join(", ", Composers), "\n");
            sDetails = String.Concat(sDetails, "Genre: ", Genre, "\n");

            return sDetails;
        }

        public CMusic(string[] artists, int bitrate, string title, string publisher, System.DateTime releaseDate, System.TimeSpan length, string album, string[] composers, string genre)
            : base(artists, bitrate, title, publisher, releaseDate, length)
        {
            Album = album;
            Composers = composers;
            Genre = genre;
        }

    }

}
4

2 回答 2

1

在你的基类中:

public virtual string GetDetails() { /* ... */ }

在你的子类中:

public override string GetDetails() { /* ... */ }

从您的评论看来,您不想更改基类,因为“为基类编码”。提供样板功能的抽象类没有错,事实上这是一个常见的习惯用法。

对于“真”,没有实现基础,您可以考虑使用接口(接口不是抽象类,真的.. 这是任何实现接口的类都必须遵守的约定):

public interface IAudio
{
    public string GetDetails();
}

public class CAudio : IAudio
{
    public string GetDetails() { /* ... */ }
}

事实上,同时使用接口抽象类来提供样板功能是完全可以接受的:

public interface IAudio { /* .. as above .. */ }

public abstract class CAUdio : IAudio
{
     // provide boiler-plate functionality
     public virtual string GetDetails() { /* ... */ }
}

public class CMusic : CAudio
{
     public override string GetDetails() { /* .. specialise */ }
}
于 2013-07-30T17:41:49.657 回答
1

只需将其转换为 CMusic:

if (Music is CMusic)
{
    Console.Write(((CMusic)Music).GetDetails());
}

但是,在这种情况下,在基类中实际将方法声明为虚拟方法,然后在子类中覆盖它似乎更合适。

于 2013-07-30T17:43:30.490 回答