Clases
Classes
class syntax: add another level of functionality and type of programming called Object-Oriented programming. We can create objects, a class with internal attributes, functions and any other of characteristics and then create multiple instances. Organize your code based on object, these objects can relate to one another with inheritance, add or remove information/characteristics though functions and actually exhibit polymorphism.
Polymorphism
is the ability to create one object or class that can exhibit multiple characteristics and commonly respond to similar functions. Class can describe geometry -ie a surface is an object with curvature, centroid, number of u and v, point, etc. also ask for information about surface based on functions embedded within the class and create your own types of objects like 'connection' or 'apertures'.
Class can be nested, have multiple function privacy, modularity.
class MyClass:
"""A simple example"""
x = 10
def test(self): # self is a convention
return 'hello'
obj = MyClass()
print obj.x # return 10
print obj.test()# return hello
obj.x = 5
print obj.x # return 5. We can change the attribute of an object and call functions outside of the class
class Harder:
def __init__(self,m,n): # force the class to give certain attributes whenever it is created (rather than adding them later)
self.i = m
self.j = n
newObj = Harder(10,20)
print newObj.i
print newObj.j
class Weird(MyClass): # example of inheritance, or the ability for a class to take on the qualities of another class, yet have its own differences.
k = 17
newerObj = Weird()
print newerObj.test() # return hello
Última actualización
¿Te fue útil?