namespace BillTest
{
public class Test
{
Animal animal = new Animal();
Dog dog = new Dog();
public void initial()
{
animal.initial(6);
animal.Say();
dog.Say();
}
public static void Main()
{
//Animal animal = new Animal();
//Dog dog = new Dog();
Test t = new Test();
t.initial();
//Animal a = new Animal();
//a.Say();
//Dog d = new Dog();
//d.Say();
Console.Read();
}
}
public class Animal
{
public int j;
public int k;
public void initial(int x)
{
j = x;
}
public void Say()
{
Console.WriteLine(j);
// Console.Read();
}
}
public class Dog : Animal
{
}
public class Program
{
}
}
变量的生命周期
最新推荐文章于 2024-12-17 00:26:44 发布
本文通过一个简单的 C# 示例介绍了多态的概念。代码展示了如何定义基类 Animal 和派生类 Dog,并演示了如何使用多态来调用不同类的方法。通过运行 Main 方法可以看到基类和派生类的行为差异。
1456

被折叠的 条评论
为什么被折叠?



