【发布时间】:2019-04-07 11:11:21
【问题描述】:
大家好,我需要帮助我不知道如何序列化/反序列化,我尝试了很多在堆栈溢出时看到的东西,但没有任何效果。 情况就在这里,我有一个创建列表的动物园类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;
namespace TP3
{
class Zoo
{
private List<object> zooList;
public Zoo()
{
zooList = new List<object>();
}
public void add ( object animal)
{
zooList.Add(animal);
}
public void remove(object animal)
{
zooList.Remove(animal);
}
public void clear( )
{
zooList.Clear();
}
public void move()
{
foreach(IAnimal specimen in zooList)
{
if (specimen is IMammal)
{
Console.Write("Mammal " + specimen);
specimen.move();
}
else
{
Console.Write("Reptile " + specimen );
specimen.move();
}
}
}
public void eat()
{
foreach (IAnimal specimen in zooList)
{
if (specimen is IMammal)
{
Console.Write("Mammal " + specimen);
specimen.eat();
}
else
{
Console.Write("Reptile " + specimen);
specimen.eat();
}
}
}
public void count()
{
int compteur = 0;
int countReptile= 0;
int countMammal = 0;
foreach (IAnimal specimen in zooList)
{
if (specimen is IMammal)
{
countMammal++;
}
else
{
countReptile++;
}
compteur++;
}
Console.WriteLine("Le zoo à "+ compteur + " animales");
Console.WriteLine(countMammal + " mammifères, et " + countReptile+ " Reptiles");
}
public void display ()
{
Console.WriteLine("Le zoo contient :");
Console.WriteLine("--------DEBUT-------------------");
foreach (IAnimal specimen in zooList)
{
if (specimen is IMammal)
{
Console.WriteLine("Mammal " + specimen);
}
else
{
Console.WriteLine("Reptile " + specimen);
}
}
Console.WriteLine("--------FIN-------------------");
}
}
}
这个列表包含了不同的对象动物,比如这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TP3
{
public class Lion : IMammal
{
private string Name;
public string _name
{
get { return Name; }
}
public Lion(string name)
{
Name = name;
}
public void move()
{
Console.WriteLine(" Superbe course du lion");
}
public void eat()
{
Console.WriteLine(" Bonne viande ");
}
public void NiceFur()
{
Console.WriteLine("Une crinière Majestueuse");
}
void IMammal.Lay()
{
}
public override string ToString()
{
return _name;
}
}
此外,我使用这样的主程序,而保存和加载功能正是我想要实现的目标
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TP3
{
class Program
{
static void Main(string[] args)
{
Zoo vincennes = new Zoo();
Cow cow1 = new Cow ("cow 1");
Cow cow2 = new Cow ( "cow 2" );
// string file = @"C:\temp\zoosave.out";
string file = Environment.CurrentDirectory + "\\myText.txt" ;
vincennes.add(new Lion ("lion 1"));
vincennes.add(cow1);
vincennes.add(new Lizard ("lizard 1"));
vincennes.add(new Platypus("platypus 1"));
vincennes.add(new Snake("snake 1"));
vincennes.display();
vincennes.remove(cow1);
vincennes.remove(cow2);
vincennes.display();
vincennes.move();
vincennes.eat();
vincennes.count();
vincennes.save(file);
vincennes.clear();
vincennes.display();
vincennes.load(file);
vincennes.display();
System.Console.Read();
}
}
对不起,我可能要求很多,但我不知道该怎么做。
【问题讨论】:
标签: c# list object serialization deserialization