Calling of class members in C#

//方法 1
使用的是Public,当需要调用类Book中的方法和属性时,
需要先定义 Book book=new Book();
          book.Id=1;
          book.PrintMsg();
class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
   
public void SetBook(int id, string name, double price)
   {
        Id = id;
        Name = name;
        Price = price;
    }
    
public void PrintMsg()
    {
        Console.WriteLine("图书编号:" + Id);
        Console.WriteLine("图书名称:" + Name);
        Console.WriteLine("图书价格:" + Price);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Book book = new Book();
        book.SetBook(1,"C#数据",12.3);
        book.PrintMsg();
    }
}

///Method 2

Method 2 uses the modifier static declaration, static declaration.

You can directly manipulate the methods and properties of its class through the class Book.

Book.Id=1;

Book.PrintMsg();

class Book
{
    public static int Id { get; set; }
    public static string Name { get; set; }
    public static double Price { get; set; }

    public static void SetBook(int id, string name, double price)
    {
        Id = id;
        Name = name;
        Price = price;
    }
    public static void PrintMsg()
    {
        Console.WriteLine("图书编号:" + Id);
        Console.WriteLine("图书名称:" + Name);
        Console.WriteLine("图书价格:" + Price);
    }
}
class Program
{
    static void Main(string[] args)
    {
        Book.SetBook(1, "计算机基础", 34.5);
        Book.PrintMsg();
    }
}

Guess you like

Origin blog.csdn.net/Hat_man_/article/details/105598955