在学习中遇到的问题,在C#入门经典第三版的11章第一个例子。我照打但是有错误,希望大家帮忙解决。
添加了3个类。using System;
using System.Collections.Generic;
using System.Text;namespace ch1101
{
public abstract class Animal
{
protected string name;
public string name
{
get
{
return name;
}
set
{
name = value;
}
}
public Animal()
{
name = "the animal with no name";
}
public void feed()
{
Console.WriteLine("{0} has been fed",name);
}
}
}
添加了3个类。using System;
using System.Collections.Generic;
using System.Text;namespace ch1101
{
public abstract class Animal
{
protected string name;
public string name
{
get
{
return name;
}
set
{
name = value;
}
}
public Animal()
{
name = "the animal with no name";
}
public void feed()
{
Console.WriteLine("{0} has been fed",name);
}
}
}
using System.Collections.Generic;
using System.Text;namespace ch1101
{
public class Cow : Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked", name);
}
public Cow(string newname)
: base(newname)
{ }
}
}
using System;
using System.Collections.Generic;
using System.Text;namespace ch1101
{
public class Chicken:Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg", name);
}
public Chicken(string newname): base(newname)
{ }
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace ch1101
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("create an array type collection of animal" +
"object and use it:");
Animal[] animalarray = new Animal[2];
Cow mycow1 = new Cow("Deirerd");
animalarray[0] = mycow1;
animalarray[1] = new Chicken("ken"); foreach (Animal myanimal in animalarray)
{
Console.WriteLine("new {0} object add to array collection,"+
"name={1}",myanimal.ToString(),myanimal.name);
}
Console.WriteLine("array colletion contains {0} objects.",animalarray.Length);
animalarray[0].feed();
((Chicken)animalarray[1]).LayEgg();
Console.WriteLine(); Console.WriteLine("create an arraylist type collection of animal" +
"object and use it:");
ArrayList animalarraylist = new ArrayList();
Cow mycow2 = new Cow("hayley");
animalarraylist.Add(mycow2);
animalarraylist.Add(new Chicken("roy"));
foreach (Animal myanimal in animalarraylist)
{
Console.WriteLine("new {0} object add to arraylist collection,"+
"name={1}",myanimal.ToString(),myanimal.name);
}
Console.WriteLine("array colletion contains {0} objects.", animalarraylist.Count);
((Animal)animalarraylist[0]).feed();
((Chicken)animalarraylist[1]).LayEgg();
Console.WriteLine(); Console.WriteLine("additional manipulation of arraylist:");
animalarraylist.RemoveAt(0);
((Animal)animalarraylist[0]).feed();
animalarraylist.AddRange(animalarray);
((Chicken)animalarraylist[2]).LayEgg();
Console.WriteLine("the animal called {0} is at index {1},",
mycow1.name,animalarraylist.IndexOf(mycow1));
mycow1.name = "janice";
Console.WriteLine("the animal is now called {0}.",
((Animal)animalarraylist[1]).name );
Console.ReadKey();
}
}
}
public string name
{
get
{
return name;
}
set
{
name = value;
}
}建议不要使用一样的名字来定义属性和字段
提示"animal"方法中没有采用"1"个参数的重载。请问这个怎么解决?
base(newname) 这个不是说的animal吗,它的构造函数没有参数的,所以newname没用。或者把这句删了,或者类一修改一下。