码迷,mamicode.com
首页 > 其他好文 > 详细

C#基础知识—父类和子类的关系

时间:2014-08-05 18:12:49      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:blog   ar   div   line   new   log   har   c#   

 public class ParentClass
    {
       public ParentClass()
       {
 
       }
       public string NamePropety { get; set; }

       public void GetName()
       {
 
       }
    }

 public class ChildClass:ParentClass
    {
       public ChildClass()
       {
 
       }

       public int Age { get; set; }

       public int GetAge()
       {
           return 10;
       }
    }

 ParentClass parent = new ParentClass();
            //parent.NamePropety
            //parent.GetName();

            //子类转父类。
            //ParentClass parent1 = new ChildClass(); 或者ParentClass parent1 = new ChildClass() as ParentClass;
            //parent1.NamePropety
            //parent1.GetName();


            ChildClass child = new ChildClass();
            //child.NamePropety
            //child.GetName();
            //child.GetAge();
            //child.Age;


            //父类转子类。
            //child1为NUll对象。
            //ChildClass child1 = new ParentClass() as ChildClass;  或者 ChildClass child1 = (ChildClass)new ParentClass();
            //Console.WriteLine(child1.NamePropety);  
            //child1.GetName();
            //child1.GetAge();
            //child1.Age;

  

C#基础知识—父类和子类的关系,布布扣,bubuko.com

C#基础知识—父类和子类的关系

标签:blog   ar   div   line   new   log   har   c#   

原文地址:http://www.cnblogs.com/zfanlong1314/p/3892423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!