静态类与非静态类
静态的东西不要大量使用,只要程序在运行的时候,静态哦的东西会一直存在,可能造成计算机卡顿。
using System;
namespace _010静态类与非静态类
{
class Program
{
static void Main(string[] args)
{
people.Name = "张三";//静态类外界不用实例化对象,直接调用
people.Age = 13;
Student student = new Student();//实例化对象
student.Name = "lala";
student.Num1();//用对象调用非静态方法
Student.Age = 12;//静态字段,直接调用
Student.Num2();//静态方法,直接调用
}
}
static class people
{
static string name;//静态类中的字段属性和调用函数都是静态的
static int age;
public static string Name { get => name; set => name = value; }
public static int Age { get => age; set => age = value; }
public static void Get()
{
Console.WriteLine("这个是Get方法");
}
}
class Student
{
string name;
static int age;
public string Name { get => name; set => name = value; }
public static int Age { get => age; set => age = value; }
public void Num1()
{
Console.WriteLine("方法1");
}
public static void Num2()
{
Console.WriteLine("方法2");
}
}
}
光标在放在字段上,ctrl+r+e就能生成相应的属性
ctrl+k+d可以对齐代码