什么是基于实例的成员
What is instance based member
在这里我想知道什么是instance based member?我认为是基于实例的成员 == 实例变量。我对么?如果我是正确的,那么我怎么知道哪个是变量或实例变量?构造函数下的变量将成为实例变量,对吗?还是我理解错了?
实例成员本质上是 class 中未标记为 static
的任何内容。也就是说,它只能在创建 class 实例后使用(使用 new
关键字)。这是因为实例成员属于对象,而静态成员属于class.
成员包括字段、属性、方法等
例如:
class Example
{
public static int Value1 { get; set; } // Static property
public int Value2 { get; set; } // Instance property
public static string Hello() // Static method
{
return "Hello";
}
public string World() // Instance method
{
return " World";
}
}
Console.WriteLine(Example.Hello() + new Example().World()); // "Hello World"
在这里我想知道什么是instance based member?我认为是基于实例的成员 == 实例变量。我对么?如果我是正确的,那么我怎么知道哪个是变量或实例变量?构造函数下的变量将成为实例变量,对吗?还是我理解错了?
实例成员本质上是 class 中未标记为 static
的任何内容。也就是说,它只能在创建 class 实例后使用(使用 new
关键字)。这是因为实例成员属于对象,而静态成员属于class.
成员包括字段、属性、方法等
例如:
class Example
{
public static int Value1 { get; set; } // Static property
public int Value2 { get; set; } // Instance property
public static string Hello() // Static method
{
return "Hello";
}
public string World() // Instance method
{
return " World";
}
}
Console.WriteLine(Example.Hello() + new Example().World()); // "Hello World"