c#之static关键字

这里写图片描述

这是我在vs中的实验代码,如有错误欢迎指正;

——补充。2018.8.28

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

namespace static关键字
{
    static class AAA
    {
       // int level = 8; 不能在静态类中声名实例成员
       //  void method01()   不能再静态类中声名实例成员
       // {
      //      Console.WriteLine("123");
      //  }
     //  public AAA() //静态类中不能有实例构造函数
     //   {

       // }
        //静态成员只被创建一次,类加载的时候,所有静态成员就会被创建在静态存储区内。

      //  AAA aaa = new AAA(); 即使在静态类内部也不能实例化
    }
   // class BBB : AAA //静态类不能被继承
   // {

    //}

    //无论对一个类创建多少个实例,它的静态成员都只有一个副本;
    class CCC   //非静态类可以包含静态的方法、字段、属性和事件;
    {
        //private CCC()
        //{

        //}
      //  CCC ccc = new CCC();
        int level = 9;
        static void method01(int level2)
        {
          //  Console.WriteLine(level);//静态方法中不能直接调用实例成员
          //  this.level = level2;  //    this/base 关键字在静态方法中不能使用
            Console.WriteLine(level2);
        }
       public void method02()
        {
            method01(5); //实例方法中,可以调用静态成员
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
           // AAA aaa = new AAA(); 静态类不能实例化
            CCC ccc = new CCC();
            ccc.method02();
            Console.ReadKey();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值