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

//1.静态方法可以访问类中的所有静态成员,但不能访问实例成员
//2.非静态方法可以访问静态成员和非静态成员
//3.在构造器中只能对非静态成员使用this关键字

namespace StaticMethod
{
class SQLServerDb
{
static string progressString1="starting repair...";
string progressString2="...repair finished";
public static void RepairDatabase()
{
Console.WriteLine(
"repairing database...");
}

//静态方法可以访问类中的所有静态成员,但不能访问实例成员
public static void RepirWithStrings()
{
Console.WriteLine(progressString1);
//语法正确
//Console.WriteLine(progressString2);//语法错误
}

//非静态方法可以访问静态成员和非静态成员
public void InstanceRepair()
{
Console.WriteLine(progressString1);
//语法正确
Console.WriteLine(progressString2);//语法正确
}
public SQLServerDb()
{ }

public SQLServerDb(string s1, string s2)
{
//this.progressString1 = s1;//语法错误
//在构造器中只能对非静态成员使用this关键字
this.progressString2 = s2;
}
}
class Program
{
static void Main(string[] args)
{
SQLServerDb.RepairDatabase();
SQLServerDb db
= new SQLServerDb();
db.InstanceRepair();
Console.ReadKey();
}
}
}

相关文章:

  • 2021-11-19
  • 2022-12-23
  • 2022-02-06
  • 2022-03-01
  • 2022-12-23
  • 2022-02-09
  • 2022-01-21
猜你喜欢
  • 2022-12-23
  • 2021-07-20
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
相关资源
相似解决方案