c#学习之静态成员

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/boy_of_god/article/details/83269904

1、静态成员

被所有实例共享,所有实例都访问同一内存位置。被一个实例改变,所有实例可见。例如QQ的在线状态的控制

2、静态成员的的调用语法

  • 如果静态成员和main方法在同一个Program类中调用时 类名.静态成员(或者不加类名)
  • 1)、在非静态类中,既可以有实例成员(非静态成员)也可以有静态成员(静态字段、静态方法、静态静态属性)
  • 2)、在调用实例成员(非静态成员)的时候需要使用 对象名.实例成员()
  • 3)、在调用静态成员的时候,需要使用 类名.静态成员() 如Convert.ToInt() Console.WriteLine();
  • 4)、静态成员必须使用类名调用,实例成员(非静态成员)使用对象调用
  • 5)、总结: 静态函数中(方法中),只能访问静态成员(静态字段,静态方法,静态属性),不允许访问实例成员(非静态成员)。
  • 6)、实例函数(方法中)(非静态成员方法中可以访问静态成员)可以访问非静态成员
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _0201静态成员
{
    class Program
    {
        public static void StaticMethod() 
        {
            Console.WriteLine("我是一个静态方法");
        }
        public void Method()
        {
            Console.WriteLine("我是一个非静态方法");
        }
        static void Main(string[] args)
        {
            Program pro = new Program();
            pro.Method();
            Program.StaticMethod();//类名.静态成员
            StaticMethod(); //不加类名  // 如果静态成员和main方法在同一个类中调用时  类名.静态成员  (或者不加类名)
            Console.ReadKey();
        }
    }
}

运行结果:
在这里插入图片描述

2.1静态类与实例类(非静态类)的使用

  • 1)、如果你想要你的类当做一个工具类使用,这时候考虑把类写成静态类如:Console类因为使用时不需要创建对象直接类名调用

  • 2)、静态类在整个项目中资源共享。类不占内存创建的实例对象是占内存的,如一些字段的默认值需要开辟空间存储。
    但是静态类占内存,因为他没有实例对象所以数据存在类中
    堆 栈 静态区(谁都可以访问静态存储区)
    当程序完全结束时才会释放空间

  • 3)、静态类中只允许出现静态成员

  • 4)、静态类中不允许创建非静态成员

  • 5)、静态类不允许实例化对象

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

namespace _02静态和非静态类
{
    class Person
    {
        private static string _name;
        public static string Name
        {
            get { return Person._name; }
            set {Person._name = value; }
        }

        public void M1()
        {    
            Console.WriteLine("我是非静态的方法");
        }
        public static void M2()
        {
            Console.WriteLine("我是一个静态方法");
        }
    }
}

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

namespace _02静态和非静态类
{
   public static class Student
    {
       private static string _name;
       public static string Name
       {
           get { return Student._name; }
           set { Student._name = value; }
       }
       public static void M2()
       {
           Console.WriteLine("我是静态类中的静态方法");
       }

      // private int _age;//错误	1	“_02静态和非静态类.Student._age”: 不能在静态类中声明实例成员	E:\vsc#code\net学习第九天\02静态和非静态类\Student.cs	26	20	02静态和非静态类

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

namespace _02静态和非静态类
{
    class Program
    {
        static void Main(string[] args)
        {
            //调用实例成员
            Person p = new Person();
            p.M1();//实例方法
            Person.M2();//静态方法
            Console.WriteLine();
            

             //Student s = new Student();//错误	3	无法创建静态类“_02静态和非静态类.Student”的实例	E:\vsc#code\net学习第九天\02静态和非静态类\Program.cs	23	25	02静态和非静态类
            Student.M2();//类名.静态成员
            Console.ReadKey();
        }
    }
}

运行结果;
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/boy_of_god/article/details/83269904