C#面向对象基础--对象引用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 继续
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public virtual void SayHello()
        {
            Console.WriteLine("我的名字是:{0},年龄是{1}",Name,Age);
        }
    }

    //声明中国人类,继承Person类
    public class Chinese:Person
    {
        public override void SayHello()
        {
            base.SayHello();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Chinese c1 = new Chinese();
            c1.Name = "老张";
            c1.Age = 40;
            c1.Age++; //年龄加1

            //对象是引用类型,所以老李的年龄也应该加1
            Chinese c2 = c1;
            c2.Name = "老李";
            c2.SayHello();
        }
    }
}
posted @ 2012-11-13 16:38  菜鸟程序猿  阅读(415)  评论(0编辑  收藏  举报