📅  最后修改于: 2020-10-31 03:02:32             🧑  作者: Mango
C#Properites没有存储位置。 C#属性是字段的扩展,并且像字段一样进行访问。
这些属性具有用于设置,获取或计算其值的访问器。
using System;
public class Employee
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
e1.Name = "Sonoo Jaiswal";
Console.WriteLine("Employee Name: " + e1.Name);
}
}
输出:
Employee Name: Sonoo Jaiswal
using System;
public class Employee
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value+" JavaTpoint";
}
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
e1.Name = "Sonoo";
Console.WriteLine("Employee Name: " + e1.Name);
}
}
输出:
Employee Name: Sonoo JavaTpoint
using System;
public class Employee
{
private static int counter;
public Employee()
{
counter++;
}
public static int Counter
{
get
{
return counter;
}
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee();
Employee e2 = new Employee();
Employee e3 = new Employee();
//e1.Counter = 10;//Compile Time Error: Can't set value
Console.WriteLine("No. of Employees: " + Employee.Counter);
}
}
输出:
No. of Employees: 3