我试图让我的 CompareTo 方法从 Artist 类中工作,但是它不会从 Album 类中获取 AlbumName 。我很确定这是专辑类上的 IComparable 的问题。请帮助提供代码特定的答案,谢谢。
错误 1“Assignment.Album”未实现接口成员“System.IComparable.CompareTo(object)”
专辑类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment
{
class Album : IComparable
{
public string albumName;
public Album(string albumName)//constructors
{
this.albumName = albumName;
}
public string AlbumName
{
get { return albumName; }
set { albumName = AlbumName; }
}
}
}
艺人类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment
{
public class Artist : IComparable
{
private String name;
int members = 4;//default number of members
//string albumName;
LinkedList<Artist> albums = new LinkedList<Artist>();
public override string ToString()//to make my own toString method
{
return ("Name: " + name + "Number of Members: " + members);
}
public Artist(string name, int members)//constructors
{
this.name = name;
this.members = members;
}
public string Name
{
get { return name; }
set { name = Name; }
}
public int Members
{
get { return members; }
set { members = value; }
}
public int CompareTo(object obj)
{
if (obj is Artist) //compare by name
{
Artist other = (Artist)obj;
return name.CompareTo(other.name);//artist name
}
if (obj is string) //compare against list of album titles
{
string other = obj.ToString();
if (obj.Equals(name.AlbumName))
return 0;
return -1;
}
else
return -999; //indicates can’t make a comparison
}
}
}