ระบบ OOP สไตล์จาวาที่แข็งแกร่งสำหรับ Python พร้อมสนับสนุนสถิติการห่อหุ้มและการสืบทอด
ดู PYPI / สร้างโดย Kento Nishi และ Ronak Badhe
Python ++ อนุญาตให้โปรแกรมเมอร์ Python ใช้หลักการการเขียนโปรแกรมเชิงวัตถุใน Python
แพ็คเกจมีอยู่ใน PYPI คุณสามารถติดตั้งแพ็คเกจด้วยคำสั่งต่อไปนี้:
pip install pythonppคุณสามารถนำเข้า Python ++ โดยใช้คำสั่งนำเข้าไวด์การ์ด
from pythonpp import * ประกาศคลาส Python ++ ด้วย @PythonPP Dorjisonator
@ PythonPP
class MyClass :
pass # class code here ประกาศตัวแปรและวิธีการสำหรับคลาส Python ++ ภายใน namespace
@ PythonPP
class MyClass :
def namespace ( public , private ):
pass # methods and variables here รหัสภายใน namespace สามารถเข้าถึงขอบเขตต่อไปนี้:
| ขอบเขต | คำอธิบาย |
|---|---|
public | ขอบเขตอินสแตนซ์สาธารณะ |
private | ขอบเขตอินสแตนซ์ส่วนตัว |
public.static | ขอบเขตคงที่สาธารณะ |
private.static | ขอบเขตคงที่ส่วนตัว |
ประกาศการเริ่มต้นแบบคงที่สำหรับคลาส Python ++ โดยใช้ @staticinit Decorator initializers แบบคงที่ไม่สามารถเข้าถึงตัวแปรอินสแตนซ์และวิธีการ initializers แบบคงที่ไม่สามารถมีพารามิเตอร์อินพุต
@ PythonPP
class MyClass :
def namespace ( public , private ):
@ staticinit
def StaticInit ():
public . static . publicStaticVar = "Static variable (public)"
private . static . privateStaticVar = "Static variable (private)" อีกทางเลือกหนึ่งสามารถประกาศตัวแปรคงที่ใน namespace เปล่า หากการกำหนดตัวแปรเป็นค่าคงที่ ไม่แนะนำให้ ใช้การประกาศตัวแปรแบบคงที่
ผู้สร้างสามารถประกาศได้โดยใช้ @constructor Decorator ตัวสร้างสามารถมีพารามิเตอร์อินพุต
@ PythonPP
class MyClass :
def namespace ( public , private ):
@ constructor
def Constructor ( someValue ):
public . publicInstanceVar = "Instance variable (public)"
public . userDefinedValue = someValue มีการประกาศวิธีการโดยใช้มัณฑนา @method(scope) กับขอบเขต public และ private ใน namespace
@ PythonPP
class MyClass :
def namespace ( public , private ):
@ method ( public )
def publicMethod ():
pass # public instance method here
@ method ( private )
def privateMethod ():
pass # private instance method here
@ method ( public . static )
def publicStaticMethod ():
pass # public static method here
@ method ( private . static )
def privateStaticMethod ():
pass # private static method here ประกาศวิธีการในตัวพิเศษโดยใช้ @special Decorator
@ PythonPP
class MyClass :
def namespace ( public , private ):
@ special
def __str__ ():
return "Some string value"คลาสสามารถขยายคลาสอื่น ๆ โดยใช้การสืบทอดคลาส Python มาตรฐาน
@ PythonPP
class ParentClass :
def namespace ( public , private ):
@ staticinit
def StaticInit ():
public . static . staticVar = "Static variable"
@ constructor
def Constructor ( param ):
print ( "Parent constructor" )
public . param = param
@ PythonPP
class ChildClass ( ParentClass ): # ChildClass extends ParentClass
def namespace ( public , private ):
@ staticinit
def StaticInit ():
ParentClass . staticinit () # Call parent static initializer
public . static . staticVar2 = "Static variable 2"
@ constructor
def Constructor ( param ):
# Call parent constructor
ParentClass . constructor ( param ) from pythonpp import *
@ PythonPP
class ParentClass :
def namespace ( public , private ):
@ staticinit
def StaticInit ():
public . static . publicStaticVar = "Public static variable"
private . static . privateStaticVar = "Private static variable"
@ constructor
def Constructor ( parameter ):
private . privateVariable = parameter
@ PythonPP
class ChildClass ( ParentClass ):
def namespace ( public , private ):
@ staticinit
def StaticInit ():
ParentClass . staticinit ()
@ constructor
def Constructor ( parameter ):
ParentClass . constructor ( parameter )
public . publicVariable = "Public variable"
private . privateVariable = "Private variable"
@ method ( public )
def getPrivateVariable ():
return private . privateVariable
@ method ( public . static )
def getPrivateStaticVar ():
return private . static . privateStaticVar
@ special
def __str__ ():
return "ChildClass object" print ( ChildClass . publicStaticVar )
# > Private static variable
print ( ChildClass . getPrivateStaticVar ())
# > Private static variable
obj = ChildClass ( "Parameter value" )
print ( obj )
# > ChildClass object
print ( obj . publicVariable )
# > Public variable
print ( obj . getPrivateVariable ())
# > Parameter value
try :
obj . privateVariable # results in an error
except Exception as e :
print ( e )
# > 'ChildClass' object has no attribute 'privateVariable' คุณสามารถดูตัวอย่างสมุดบันทึก Jupyter ทั้งหมดได้ที่นี่