Victor.Stone
dot net 备忘录
posts - 21,  comments - 25,  trackbacks - 0
    public abstract class Animal
    
{
        
public string type = "**类";

        
public abstract void ShowType();

        
public void Eat()
        
{
            Console.WriteLine(
"Animal都是会吃的.");
        }


        
public string Color = "**色";
    }


    
public class Bird : Animal
    
{
        
public string type = "Bird类";

        
public override void ShowType()
        
{
            Console.WriteLine(
"当前的Type:{0}", type);
        }


        
private string color;

        
public string Color
        
{
            
get return color; }
            
set { color = value; }
        }

    }


    
public class Chicken : Bird
    
{
        
public string type = "Chicken类";

        
public override void ShowType()
        
{
            
//base.ShowType();
            Console.WriteLine("当前的Type:{0}", type);
        }


        
public void ShowColor()
        
{
            Console.WriteLine(
"颜色:", Color);
        }

    }


    
class Program
    
{
        
static void Main(string[] args)
        
{
            Bird b 
= new Bird();
            b.Color 
= "白色";
            Console.WriteLine(
"b Color:{0} ", b.Color);
            b.ShowType();
            Console.WriteLine(
"-".PadLeft(40'-'));

            Chicken c 
= new Chicken();
            c.Color 
= "黑黑";
            Console.WriteLine(
"c Color:{0} ", c.Color);
            c.ShowType();
            Console.WriteLine(
"-".PadLeft(40'-'));

            Console.ForegroundColor 
= ConsoleColor.Cyan;
            Animal b2 
= new Bird();
            b2.Color 
= "白色2";
            Console.WriteLine(
"b2 Color:{0} ", b2.Color);
            Console.WriteLine(
"b2.Type:{0} ", b2.type);
            b2.ShowType();
            Console.WriteLine(
"-".PadLeft(40'-'));

            Animal c2 
= new Chicken();
            
//c2.Color = "黑黑2"; //如果这样,会调用基类的颜色
            Console.WriteLine("c2 Color:{0} ", c2.Color);
            Console.WriteLine(
"c2.Type:{0} ", c2.type);
            c2.ShowType();  
//因为ShowType()方法被override重写了所以会调用子类的方法, 但属性和成员则会取自于父类的
            Console.WriteLine("-".PadLeft(40'-'));

            Console.ReadLine();
        }

    }

运行结果:





posted on 2012-03-09 23:50 Victor.Stone 阅读(165) 评论(0)  编辑 收藏 引用 所属分类: .net framework
只有注册用户登录后才能发表评论。

<2012年3月>
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(2)

随笔分类(17)

文章分类

搜索

  •  

积分与排名

  • 积分 - 23821
  • 排名 - 192

最新随笔

阅读排行榜