Guest User

Untitled

a guest
Jun 25th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. public double length_inches
  2. {
  3. get { return length_metres * 39.0; }
  4. set { length_metres = value/39.0; }
  5. }
  6.  
  7. class Foo(object):
  8.  
  9. def get_length_inches(self):
  10. return self.length_meters * 39.0
  11.  
  12. def set_length_inches(self, val):
  13. self.length_meters = val/39.0
  14.  
  15. length_inches = property(get_length_inches, set_length_inches)
  16.  
  17. class Foo(object):
  18.  
  19. # 2.5 or later
  20. @property
  21. def length_inches(self):
  22. return self.length_meters * 39.0
  23.  
  24. # 2.6 or later
  25. @length_inches.setter
  26. def length_inches(self, val):
  27. self.length_meters = val/39.0
  28.  
  29. var object = {
  30. // .. other property definitions ...
  31. get length_inches(){ return this.length_metres * 39.0; },
  32. set length_inches(value){ this.length_metres = value/39.0; }
  33. };
  34.  
  35. class Thing
  36. attr_accessor :length_inches
  37. end
  38.  
  39. class Thing
  40. def length_inches
  41. @length_inches
  42. end
  43. def length_inches=(value)
  44. @length_inches = value
  45. end
  46. end
  47.  
  48. class Thing
  49. def length_inches
  50. @length_metres * 39.0
  51. end
  52. def length_inches=(value)
  53. @length_metres = value / 39.0
  54. end
  55. end
  56.  
  57. TFoo = class
  58. private
  59. FBar: string;
  60. procedure SetBar(Value: string);
  61. function GetBar: string;
  62.  
  63. public
  64. property Bar: string read GetBar write SetBar;
  65.  
  66. end;
  67.  
  68. TFoo = class
  69. private
  70. FBar: string;
  71.  
  72. public
  73. property Bar: string read FBar write FBar;
  74.  
  75. end;
  76.  
  77. class Length( object ):
  78. conversion = 39.0
  79. def __init__( self, value ):
  80. self.set(value)
  81. def get( self ):
  82. return self.length_metres
  83. def set( self, value ):
  84. self.length_metres= value
  85. metres= property( get, set )
  86. def get_inches( self ):
  87. return self.length_metres*self.conversion
  88. def set_inches( self, value ):
  89. self.length_metres= value/self.conversion
  90. inches = property( get_inches, set_inches )
  91.  
  92. >>> l=Length(2)
  93. >>> l.metres
  94. 2
  95. >>> l.inches
  96. 78.0
  97. >>> l.inches=47
  98. >>> l.metres
  99. 1.2051282051282051
  100.  
  101. Public Property Get LengthInches() As Double
  102. LengthInches = LengthMetres * 39
  103. End Property
  104.  
  105. Public Property Let LengthInches(Value As Double)
  106. LengthMetres = Value / 39
  107. End Property
  108.  
  109. @interface MyClass : NSObject
  110. {
  111. int myInt;
  112. NSString *myString;
  113. }
  114.  
  115. @property int myInt;
  116. @property (nonatomic, copy) NSString *myString;
  117.  
  118. @end
  119.  
  120. @synthesize myInt, myString;
  121.  
  122. - (void)setMyString:(NSString *)newString
  123. {
  124. [myString autorelease];
  125. myString = [newString copy];
  126. }
  127.  
  128. Public Property Name As String
  129. Get
  130. return "someName"
  131. End Get
  132. Set
  133. ...
  134. End Set
  135. End Property
  136.  
  137. class Length {
  138. public $metres;
  139. public function __get($name) {
  140. if ($name == 'inches')
  141. return $this->metres * 39;
  142. }
  143. public function __set($name, $value) {
  144. if ($name == 'inches')
  145. $this->metres = $value/39.0;
  146. }
  147. }
  148.  
  149. $l = new Length;
  150. $l->metres = 3;
  151. echo $l->inches;
  152.  
  153. template <class TValue, class TOwner, class TKey>
  154. class property
  155. {
  156. TOwner *owner_;
  157.  
  158. public:
  159. property(TOwner *owner)
  160. : owner_(owner) {}
  161.  
  162. TValue value() const
  163. {
  164. return owner_->get_property(TKey());
  165. }
  166.  
  167. operator TValue() const
  168. {
  169. return value();
  170. }
  171.  
  172. TValue operator=(const TValue &value)
  173. {
  174. owner_->set_property(TKey(), value);
  175. return value;
  176. }
  177. };
  178.  
  179. class my_class
  180. {
  181. public:
  182. my_class()
  183. : first_name(this), limbs(this) {}
  184.  
  185. struct limbs_k {};
  186. struct first_name_k {};
  187.  
  188. property<std::string, my_class, first_name_k> first_name;
  189. property<int, my_class, limbs_k> limbs;
  190.  
  191. std::string get_property(const first_name_k &);
  192. void set_property(const first_name_k &, const std::string &value);
  193.  
  194. int get_property(const limbs_k &);
  195. void set_property(const limbs_k &, int value);
  196. };
  197.  
  198. class MyClass:
  199. //a field, initialized to the value 1
  200. regularfield as int = 1 //default access level: protected
  201.  
  202. //a string field
  203. mystringfield as string = "hello"
  204.  
  205. //a private field
  206. private _privatefield as int
  207.  
  208. //a public field
  209. public publicfield as int = 3
  210.  
  211. //a static field: the value is stored in one place and shared by all
  212. //instances of this class
  213. static public staticfield as int = 4
  214.  
  215. //a property (default access level: public)
  216. RegularProperty as int:
  217. get: //getter: called when you retrieve property
  218. return regularfield
  219. set: //setter: notice the special "value" variable
  220. regularfield = value
  221.  
  222. ReadOnlyProperty as int:
  223. get:
  224. return publicfield
  225.  
  226. SetOnlyProperty as int:
  227. set:
  228. publicfield = value
  229.  
  230. //a field with an automatically generated property
  231. [Property(MyAutoProperty)]
  232. _mypropertyfield as int = 5
  233.  
  234. public int get_SomeValue() { return someValue; }
  235. public void set_SomeValue(int value) { someValue = value; }
  236. private int someValue = 10;
  237.  
  238. // client
  239. int someValue = someClass.get_SomeValue();
  240. someClass.set_SomeValue(12);
Add Comment
Please, Sign In to add comment