C#类与对象

1,类的组成

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

namespace j1
{
    /********** Begin *********/
    public class Phone{
        string name="MI";
        string model="MIX2";
        int price=2999;

        public void showPhoneInformation()
        {
            Console.WriteLine("Phone name:"+name);
            Console.WriteLine("Phone model:"+model);
            Console.WriteLine("Phone price:"+price);
        }
    }

    /********** End *********/

    public class myCaller
    {
        public static void Main(string[] args)
        {
            Phone myPhone = new Phone();
            myPhone.showPhoneInformation();
        }
    }
}

2,构造函数和析构函数

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

namespace J2
{
    /********** Begin *********/
    public class myDevice{
        string ip;

        public myDevice()
        {
            ip="103.67.193.190";
        }
        public myDevice(string _ip)
        {
            ip=_ip;
        }
        public void showIp()
        {
            Console.WriteLine(ip);
        }
    }


    /********** End *********/

    public class myCaller
    {
        public static void Main(string[] args)
        {
            myDevice phone = new myDevice();
            myDevice computer = new myDevice("103.67.193.195");

            phone.showIp();
            computer.showIp();
        }
    }
}

3,private、protected、public、internal的区别

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

namespace J3
{
    /********** Begin *********/
    //Passenger类
    class Passenger{
      public  bool package;
      public  bool clothing;

      public  Passenger(bool _package,bool _clothing)
      {
          package=_package;
          clothing=_clothing;
      }
    }

    /********** End *********/

    class Worker
    {
        public static void Main(string[] args)
        {
            Passenger p1 = new Passenger(true, true);
            Passenger p2 = new Passenger(true, false);

            /********** Begin *********/
            //使用函数testing查看p1、p2属性值
            testing(p1.package);
            testing(p1.clothing);
            testing(p2.package);
            testing(p2.clothing);
            /********** End *********/

        }
        //手动检测函数
        public static void testing(bool thing)
        {
            if (thing != true)
            {
                Console.WriteLine("Dangerous");
            }
            else
            {
                Console.WriteLine("Pass");
            }
        }
    }
}

4,this关键字

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

namespace J4
{
    class Schedule
    {
        bool Monday;
        bool Tuesday;
        bool Wednesday;
        bool Thursday;
        bool Friday;

        /********** Begin *********/
        //使用this索引器补全代码
        public bool this[int index]
        {
            set{
                switch(index)
                {
                    case 1:
                     this.Monday=value;
                     break;
                    case 2:
                     this.Tuesday=value;
                     break;
                    case 3:
                     this.Wednesday=value;
                     break;
                    case 4:
                     this.Thursday=value;
                     break;
                    case 5:
                     this.Friday=value;
                     break;  
                }
            }
        }

        /********** End *********/
        public void getResult()
        {
            Console.WriteLine("Monday:" + Monday);
            Console.WriteLine("Tuesday:" + Tuesday);
            Console.WriteLine("Wednesday:" + Wednesday);
            Console.WriteLine("Thursday:" + Thursday);
            Console.WriteLine("Friday:" + Friday);
        }
    }
    class myCaller
    {
        public static void Main(string[] args)
        {
            Schedule s = new Schedule();
            s[1] = true;
            s[2] = true;
            s[3] = false;
            s[4] = false;
            s[5] = true;

            s.getResult();

        }
    }
}

5,static静态对象

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

namespace J5
{
    public class BigEvent
    {
        private string time;
        private string location;
        /********** Begin *********/
        public static int counter=0;
        public  BigEvent(){
            counter++;
        }
        /********** End *********/
    }

    public class myCaller
    {
        public static void Main(string[] args)
        {
            BigEvent b1 = new BigEvent();
            BigEvent b2 = new BigEvent();
            BigEvent b3 = new BigEvent();

            Console.WriteLine(BigEvent.counter);
        }
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值