using system;
using system.reflection;
using system.collections.generic;
using system.runtime.compilerservices;
class example
set}
private int _instanceproperty = 42;
public int instanceproperty
set}
public static void main()
", example.staticproperty);
propertyinfo pishared = typeof(example).getproperty("staticproperty");
pishared.setvalue(null, 76, null);
console.writeline("final value of class-level property: ",
example.staticproperty);
example exam = new example();
console.writeline("\ninitial value of instance property: ",
exam.instanceproperty);
propertyinfo piinstance =
typeof(example).getproperty("instanceproperty");
piinstance.setvalue(exam, 37, null);
console.writeline("final value of instance property: ",
exam.instanceproperty);
exam[17] = "string number 17";
exam[46] = "string number 46";
exam[9] = "string number 9";
console.writeline(
"\ninitial value of indexed instance property(17): ''",
exam[17]);
// by default, the indexer is named item, and that name must be used
// to search for the property. in this example, the indexer is given
// a different name by using the indexernameattribute attribute.
propertyinfo piindexedinstance =
typeof(example).getproperty("indexedinstanceproperty");
piindexedinstance.setvalue(
exam,
"new value for string number 17",
new object );
console.writeline(
"final value of indexed instance property(17): ''",
exam[17]); }}
/* this example produces the following output:
initial value of class-level property: 41
final value of class-level property: 76
initial value of instance property: 42
final value of instance property: 37
initial value of indexed instance property(17): 'string number 17'
final value of indexed instance property(17): 'new value for string number 17'
*/
Python訪問類的私有屬性和私有方法的正確姿勢
encoding utf 8 classperson 只允許擁有私有的name和age屬性 slots name age def init self name,age self.name name self.age age property defname self returnself.name ...
php中類外部訪問類私有屬性的方法
我們都知道,類的私有屬性在類外部是不可訪問的,包括子類中也是不可訪問的。比如如下 class example1 r function example1 e a new example1 var dump r a 執行結果 fatal error cannot access private prope...
類的屬性 欄位和方法
visual basic 語言概念 一般情況下,欄位和屬性表示物件的資訊,而方法表示物件可以採取的操作。下面的主題描述向類中新增屬性 欄位和方法的機制及處理與這些項關聯的問題。本節內容 向類新增欄位和屬性 提供關於宣告欄位和屬性的資訊。屬性和屬性過程 解釋property過程如何工作以及如何實現公共...