C#面向对象复习概要

来源:互联网 发布:儿童英语课程 知乎 编辑:程序博客网 时间:2024/06/05 09:40

1.面向对象:我们将具有统一行为和属性的对象抽象划分为类,通过类去创建对象。这种编程思想叫做面向对象的编程思想。

2.属性:对象具有的属性

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 属性{    class Program    {        static void Main(string[] args)        {            Person person1 = new Person();            person1.Name = "tangxuelong";            Console.Write("我的名字是{0}",person1.Name);            person1.Name = "zhangsan";            Console.Write("我的名字是{0}", person1.Name);            person1.Name = "lisi";            Console.Write("我的名字是{0}", person1.Name);        }    }    public class Person    {        //private字段和public属性        private string name;        public string Name {            set {                if (value == "tangxuelong")                {                    this.name = value;                }                else if (value == "zhangsan")                {                    this.name = "sunwukong";                }                else {                    return;                }            }            get { return name; }        }    }}

3.继承:子类拥有父类的属性和行为

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 继承{    class Program    {        static void Main(string[] args)        {            Person person1 = new Person();            person1.eat();            person1.walk();        }    }    class Animal    {        public void eat()        {            Console.WriteLine("eat!");        }    }    class Person : Animal    {        public void walk()        {            Console.WriteLine("walk!");        }    }}

4.静态成员和非静态成员

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 静态成员{    class Program    {        public int 实例成员() {            return 2;        }        public static int 静态成员() {            return 1;        }                static void Main(string[] args)        {            //在静态函数成员中,访问实例成员需要实例化            Program p = new Program();            p.实例成员();            //在静态函数成员中,访问静态成员不需要实例化可以直接访问            静态成员();        }        void 实例函数()         {            //在实例函数成员中,可以直接访问实例成员和静态成员            实例成员();            静态成员();        }    }}

  

0 0