Каблуков Станислав
PyNSK #8
http://academmedia.com/
class ClassWithProperty(object):
__slots__ = ('__x', 'x')
def __init__(self, x=0):
self.__x = x
def get_x_int(self):
return self.__x
def set_x_int(self, value):
self.__x = value
def del_x(self):
del self.__x
x = property(get_x_int, set_x_int, del_x, "X property")
klass_with_property = ClassWithProperty()
klass_with_property.get_x_int()
0
klass_with_property.set_x_int(10)
klass_with_property.get_x_int()
10
klass_with_property.new_attr = 19
*** AttributeError: 'ClassWithProperty' object has no attribute
'new_attr'public class TestClass {
private int x = 0;
public int get_x() {
return x;
}
public void setX(int x) {
this.x = x;
}
}public class Main {
public static void main(String[] args){
TestClass tClass = new TestClass();
tClass.get_x();
tClass.setX(10);
tClass.y = 19; //Выдаст ошибку
}
}
class ClassWithDecorator(object):
__slots__ = ['_x']
def __init__(self, x):
self._x = x
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x#import <Foundation/Foundation.h>
@interface PropertyClass : NSObject
@property int x;
@property (strong, readwrite, setter=customSetterForY:, getter=customGetterForY) NSString* y;
- (NSString*)customGetterForY;
- (void)customSetterForY:(NSString*)valueOfString;
@end#import "PropertyClass.h"
@implementation PropertyClass
@synthesize y = _y;
- (NSString *)customGetterForY {
NSLog(@"y value: %@", _y);
return _y;
}
- (void)customSetterForY:(NSString *)valueOfString {
_y = valueOfString;
NSLog(@"Now y value: %@", _y);
}
@endPropertyClass * kls = [[PropertyClass alloc] init];
kls.x;
kls.x = 12;
[kls x];
[kls setX:10];
kls.y;
kls.y = @"string custom";
[kls customGetterForY];
[kls customSetterForY:@"string custom"];@property int x;
@property (setter=customSetterForY:, getter=customGetterForY) NSString* y;
@property (strong, readonly, setter=, getter=,) obj* name;
from kivy.properties import *
from kivy.uix.widget import Widget
class MyWidget(Widget):
uid = NumericProperty(0)
name = StringProperty("Widget Name")
my_list = ListProperty([])
locations = ObjectProperty()
happy = BooleanProperty(False)
number = BoundedNumericProperty(0, min=-5, max=5)
state = OptionProperty("None", options=["On", "Off", "None"])
list_property = ReferenceListProperty(uid, number)
dict_property = DictProperty({})
wid = MyWidget()
print wid.uid
0
wid.uid = "stroka" # error
my_list = wid.my_list = [1, 5, 7]
print my_list is wid.my_list
# False
my_list.append(10)
print my_list, wid.my_list
# [1, 5, 7, 10], [1, 5, 7]
print wid.happy
# FalseAnchorLayout
BoxLayout
FloatLayout
RelativeLayout
GridLayout
PageLayout
StackLayout
from pyobjus import autoclass
from pyobjus.dylib_manager import load_framework, INCLUDE
load_framework(INCLUDE.AppKit)
# get both nsalert and nsstring class
NSAlert = autoclass('NSAlert')
NSString = autoclass('NSString')
# shortcut to mimic the @"hello" in objective C
ns = lambda x: NSString.alloc().initWithUTF8String_(x)
# create an NSAlert object, and show it.
alert = NSAlert.alloc().init()
alert.setMessageText_(ns('Hello world!'))
alert.runModal()
//Objective C
- (void) sumNumber:(int)a and:(int)b andAlso:(int)c { ... }
#Python
sumNumber_and_andAlso_(1, 2, 3)
sumNumber_A_and_B_andAlso_C(1, 2, 3)