3 回答
TA贡献1836条经验 获得超3个赞
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
List<Animal> animals = new List<Animal> { new Cat(), new Cat(), new Dog(), new Cat(), new Dog() };
var dogs = animals.OfType<Dog>().ToList();
dogs.ForEach(dog => dog.Bark());
var cats = animals.OfType<Cat>().ToList();
cats.ForEach(cat => cat.Meow());
var actualTypes = animals.Select(animal => animal.GetType()).ToList();
}
abstract class Animal { }
class Dog : Animal { public void Bark() { } }
class Cat : Animal { public void Meow() { } }
}
}
- 3 回答
- 0 关注
- 72 浏览
添加回答
举报