using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 常量
{
class Program
{
public const float PI = 3.14f; //常量 ,一旦声明不可更改
static void Main(string[] args)
{
Person.TotalCount = 120000;
abc();
Console.WriteLine(Person.TotalCount);
Dog d = new Dog();
d.叫唤();
Person.人口汇报();
Person p1 = new Person();
p1.age = 100;
p1.SayHello();
// ConsoleHelper c = new ConsoleHelper(); //错误 静态类无法被实例化
Console.WriteLine(ConsoleHelper.ReadInt());
Console.ReadKey();
}
public static void abc()
{
Console.WriteLine(Person.TotalCount);
}
public class Person
{
public static int TotalCount; //static变量 全局变量 不用new也可以调用
public static void 人口汇报() //static方法 不用new也可以调用
{
Console.WriteLine("人口 {0}",TotalCount);//static方法 只能调用static成员
//Console.WriteLine("年龄 {0}", age); //错误,在静态方法中不可以使用非static成员。
}
public int age;
public void SayHello() //非static方法可以使用static成员
{
Console.WriteLine("我的年龄{0}",age);
Console.WriteLine("人口 {0}", TotalCount);
}
}
public class Dog
{
public void 叫唤()
{
Console.WriteLine("旺旺,{0}人",Person.TotalCount);
}
}
public static class ConsoleHelper //静态类无法被实例化 静态类一般来实现一个纯的函数库
{
public static int ReadInt()
{
Console.WriteLine("请输入一个整数");
return Convert.ToInt32(Console.ReadLine());
}
}
}
}
C#学习笔记(十七) --静态变量 方法 类
最新推荐文章于 2023-12-28 10:38:23 发布