using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class FarmAnimal
{
public string name;
public virtual string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public virtual void MakeSound()
{
Console.WriteLine(Name + " makes a sound...");
}
} class Cow : FarmAnimal
{
public override void MakeSound()
{
Console.WriteLine(Name + " goes Mooooo...");
}
} class Horse : FarmAnimal
{
public override string Name
{
set
{
base.Name = value + " a Horse";
}
}
} class Program
{
static void Main(string[] args)
{
Cow c = new Cow();
Horse h = new Horse();
c.Name = "Elsie";
h.Name = "Mr.Ed";
c.MakeSound();
h.MakeSound();
Console.WriteLine(h.Name); //////
Console.ReadKey();
}
}
}
在程序中用//////标出的那一行,为什么覆载之后的property没有提供get访问器还是能够访问到?
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class FarmAnimal
{
public string name;
public virtual string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public virtual void MakeSound()
{
Console.WriteLine(Name + " makes a sound...");
}
} class Cow : FarmAnimal
{
public override void MakeSound()
{
Console.WriteLine(Name + " goes Mooooo...");
}
} class Horse : FarmAnimal
{
public override string Name
{
set
{
base.Name = value + " a Horse";
}
}
} class Program
{
static void Main(string[] args)
{
Cow c = new Cow();
Horse h = new Horse();
c.Name = "Elsie";
h.Name = "Mr.Ed";
c.MakeSound();
h.MakeSound();
Console.WriteLine(h.Name); //////
Console.ReadKey();
}
}
}
在程序中用//////标出的那一行,为什么覆载之后的property没有提供get访问器还是能够访问到?
有道理,你重载的是 SET,FarmAnimal 这个基类里面本来就有get方法,所以被继承了.