01学习类,对象,字段,属性,方法,构造函数,析构函数


```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 学习类_对象_字段_属性_方法_构造函数_析构函数
{
    **class Program**
    {
        static void Main(string[] args)
        {
            //创建Person类的对象
            Person p = new Person("liu",20,"女");
           // p.Name  = "liu";
            //p.Age  = 20;
            //p.Sex  = "女";
            p.Introduce();
            Person p2 = new Person("zhang", 19, "男");
                p2.Introduce();
            Person p3 = new Person("wang", 16);
            p3.Introduce();
            p.Ip();//非静态方法,用对象名来调用
            Person.Mangement();//静态方法,用类名来调用
            Console.ReadKey();
        }
    }
}

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

namespace 学习类_对象_字段_属性_方法_构造函数_析构函数
{
    //这是一个静态类
   **public static class People**
    {
        private static string _Name;
        public static string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        //静态类中只能存在静态成员
    }
}

List item

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

namespace 学习类_对象_字段_属性_方法_构造函数_析构函数
{
    //这是一个非静态类
    public class Person
    {//析构函数:用来释放空间,在所有代码执行完成时
        ~Person()
        {

        }
        
        
        //构造函数:是对属性赋值,也是对对象的实例化,可以重载,且可以互相调用
        public Person(string Name,int Age,string Sex)
        {
            this.Name = Name;
            this.Age = Age;
            this.Sex = Sex;

        }
        public Person(string Name,int Age):this(Name,Age," ")
        {

        }





        private string _Name;
        public string Name
        {
            get {
                if(_Name!="zhang")
                {
                    return "zhang";
                }
                return _Name; }
            set{ _Name = value; }
        }

        private  int _Age;
        public int Age
        {
            get { return _Age; }
            set {
                if(value!=18)
                {
                   value = 18;
                }
                _Age = value; }
        }
        private  string _Sex;
        public string Sex
        {
            get { return _Sex;}
            set { _Sex = value; }
        }

        public void Introduce()
        {
            Console.WriteLine("我是{0},今年{1}岁了,是个{2}生", this.Name,this. Age,this. Sex);
        }
        //在静态类中,既可以存在静态成员,也可以存在非静态成员
        public static void Mangement()
        {
            //静态函数中只能访问静态成员
            
            Console.WriteLine("我是一个静态方法,只有类名才能调用我");
        }
        public void Ip()
        {
            //非静态方法中既可以访问静态成员,也可以访问非静态成员
            Console.WriteLine("我是一个非静态方法,只有对象名才能调用我");
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值