我有以下代码,并且不能为我的生活弄清楚为什么它给我错误调用方法SeeAllTweeters(); 这是代码,并提前感谢您的帮助!
TweetManager.cs
namespace Assign3_Twitter
{
public class TweetManager
{
private List<Tweet> tweets = new List<Tweet>();
TweetManager()
{
Tweet tw1 = new Tweet ("Austen", "Hello World!", "#Hey", "Twitter");
tweets.Add (tw1);
Tweet tw2 = new Tweet ("Test1", "Hello World! x2", "#Howdy", "Tweeter");
tweets.Add (tw2);
Tweet tw3 = new Tweet ("Test2", "Hello World! x3", "#Hey", "Twitter");
tweets.Add (tw3);
Tweet tw4 = new Tweet ("Test3", "Hello World! x4", "#Howdy", "Tweeter");
tweets.Add (tw4);
Tweet tw5 = new Tweet ("Test4", "Hey there!", "#Hey", "Twitter");
tweets.Add (tw5);
Tweet tw6 = new Tweet ("Test5", "Woah this is cool!", "#Howdy", "Tweeter");
tweets.Add (tw6);
}
public void SeeAllTweeters()
{
foreach (Tweet Tweets in tweets)
{
Console.WriteLine(Tweets);
}
}
Tweet.cs
namespace Assign3_Twitter
{
public class Tweet
{
public string HashTag { get; private set; }
public string Message { get; private set; }
public string Sender { get; private set; }
public string Recipient { get; private set; }
public Tweet (string sender, string message, string hashtag, string reciepient)
{
this.Sender = sender;
this.HashTag = hashtag;
this.Message = message;
this.Recipient = reciepient;
}
public override string ToString ()
{
return string.Format ("[Tweet: HashTag={0}, Message={1}, Sender={2}, Recipient={3}]", HashTag, Message, Sender, Recipient);
}
}
}
Program.cs的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assign3_Twitter
{
class MainClass
{
public static void Main (string[] args)
{
Tweet tw100 = new Tweet("Austen", "Hey guys!", "awesome", "Meee");
SeeAllTweeters();
}
}
}
答案 0 :(得分:3)
SeeAllTweeters
类是在TweetManager
类上定义的,因此您只能通过引用该类的实例来调用它。例如:
class MainClass
{
public static void Main (string[] args)
{
TweetManager tweetManager = new TweetManager();
tweetManager.SeeAllTweeters();
}
}
进一步阅读