abstract talk python- 07 (polymorphism, encapsulation and inheritance)

Chapter VII talk about abstract # 
# 7.1 Object magic
# polymorphic: can perform the same operation on different types of objects, and these operations as "enchanted" as capable of running
# Package: external hidden objects related work principle details
# Inheritance: You can create a special class based on the general category
# 7.1.1 polymorphism (without knowing the class (type of object) object belongs)
# 7.1.2 and polymorphism method (without knowing the structure of the object)
# 7.1 .3 package
'' '
O = the OpenObject ()
o.set_name (' Sir Lancelot ')
Print (o.get_name ())
' ''
# 7.1.4 Inherited
# 7.2 class
# 7.2.1 class in the end what subclass, superclass
# 7.2.2 create a custom category
class the Person:
DEF set_name (self, name):
self.name name =
DEF get_name (self):
return self.name
DEF the greet (self):
#Print ( 'the Hello, world! the I \ '{m}'. the format (the self.name))
Pass
foo = the Person ()
= the Person bar ()
foo.set_name ( 'Luke Skywalker')
bar.set_name ( 'Anakin Skywalker')
Print (foo.greet ())
Print (bar.greet ())

# 7.2.3 Method property function and
class Class:
Method DEF (Self):
Print ( 'A Self have have the I!')
DEF function ():
Print ( 'the I DOT \' T ... ')

instance Class = ()
Print (instance.method ())
instance.method function =
Print (instance.method ())

# 7.2.4 to talk about hidden

# 7.2.4 to talk about the hidden (two _ prohibit access, a _ Do not modify the properties or methods from outside)
# 7.2.5 class namespace

class MemberCounter:
Members = 0
DEF the init (Self):
MemberCounter.members +. 1 =

M1 = MemberCounter ()
m1.init ()
Print (MemberCounter.members)

MemberCounter = M2 ()
Print (MemberCounter.members)
# 7.2.6 Specify superclass
class the Filter:
DEF the init (Self):
self.blocked = []
DEF filter (Self, Sequence):
return [X for X in Sequence IF X in self.blocked Not]
class spamfilter (Filter): Filter # spamfilter is a subclass
DEF the init (Self):
self.blocked = [ 'SPAM']

F = Filter ()
f.init ()
Print (f.filter ( [. 1, 2,. 3]))

S = spamfilter ()
s.init ()
Print (s.filter ([ 'SPAM', 'SPAM', 'eggs']))

# 7.2.7 depth study Inherited
print (issubclass (sPAMFilter, Filter)) # is not determined spamfilter Filter subclass
Print (issubclass (Filter, spamfilter))
Print (spamfilter .__ bases__) base class # View spamfilter
Print (the Filter .__ bases__) #

Print (the isinstance (S, spamfilter)) # Analyzing class is an instance of the object
Print (the isinstance (S, the Filter))
Print (the isinstance (S, STR))
C = '22 is'
Print (the isinstance ( C, STR))

Print (S class__ is .__) # view object belongs, class
# plurality 7.2.8 superclass
class the Calculator:
DEF the Calculate (Self, expression the):
self.value = the eval (expression the)

class Talker:
DEF Talk (Self):
Print ( 'the Hi, My valus {IS}' the format (self.value).)
class TalkingCalculator (the Calculator, Talker):
Pass

TC = TalkingCalculator ()
tc.calculate ( '*. 1 + 2. 3')
TC .talk ()
Print (TalkingCalculator .__ bases__)
# 7.2.9 Interface and introspection
 related things together. If a global variable is a function of the operation, they are preferably used as the properties and methods of a class.  Do not let too close between objects. The method should only be concerned with the properties of their respective instance, for the status of other instances, they let themselves to manage just fine.  caution inheritance, especially multiple inheritance. Inheritance is sometimes useful, but may bring unnecessary complexity in some cases. To properly use multiple inheritance is difficult, more difficult to exclude one of the bug.  Keep it simple. Let methods short and compact. In general, it should ensure that most of the methods can be read and understood in 30 seconds. For the rest of the methods, which as far as possible in a space or a control panel. When determining which classes and these classes need what method should contain, try to do it like this. (1) The description of the problem (the program what to do) record, and to all the nouns, verbs and adjectives marked. (2) identify possible class nouns. (3) a method in identifying possible verb. (4) identify possible properties in adjectives.




















(5) identify the methods and attributes assigned to each category.
With the sketch of the face of the object model, the need to consider the relationship between classes and objects (such as inheritance or collaboration) as well as their responsibilities. To further improve the model, you can do it like this.
(1) recording (or scenario) with a series of cases, i.e., using the program scene, and to try to ensure that these use cases cover all functions.
(2) thoroughly and carefully consider each scenario, ensure that the model contains everything you need. If they are missing, they add; if there is not quite the place, on the amendment. This process is repeated continuously, until the model is satisfied.
With proven model after you think you can start writing the program. You probably need to modify some parts of the model or program, but fortunately it is very easy in Python, do not worry. Click here to just say do just fine.
(If you need more detailed object-oriented programming guide, see Chapter 19 recommended books.)
'' '
# 7.4 summary
' ''
 objects: the composition of the object properties and methods. But variable attribute belonging to the object, the function is stored in the property. Compared to other functions
(associated) method has a different place, that is, it is always an object to which it belongs as a first argument, and this argument is usually named self.
 class: class represents a group (or class) objects, and each object belongs to a particular class. The main task is to define a method of the class will contain examples thereof.
 Polymorphism: polymorphism refers to the same can be applied to different types of objects and classes, i.e., without knowing which class the object belongs to its methods can be invoked.
 Package: An object may be hidden (package) its internal state. In some languages, the state (property) which means that the object can be accessed only by its methods.
In Python, all attributes are public, but caution should be exercised when programmers direct access to the state of the object, as this may result in an inconsistent state inadvertently.
 succession: a class can be a subclass or more classes, in this case, the subclass inherits all the methods of the superclass.
You can specify more than one superclass, by doing so can be a combination of orthogonal (independent and unrelated) functions. To this end, a common practice is to use a core of super class and one or more mixed superclass.
 interfaces and introspection: In general, you do not have too much depth study, but only depends on the methods needed to call polymorphism. However, if you want to determine which object contains a method or property, you have a number of functions available to complete this work.
 abstract base class: abc usage module to create an abstract base class. Abstract base class used to specify which functions subclass must provide, but do not implement these functions.
 object-oriented design: on how to proceed and whether the design of object-oriented object-oriented design, there are many different points of view. No matter what point of view you hold, must deeply understand the problem, and then create an easy-to-understand design.

Whether callable (object) is determined object can be called (e.g., whether it is a function or method)
getattr (Object, name [, default]) Gets the value of the property, can also provide default values
hasattr (object, name) to determine whether an object has designated attribute
isinstance (object, class) determines whether the specified object is an instance of class
issubclass (a, B) to determine whether a is a subclass of B
random.choice (sequence) select an element from a non-empty sequence randomly
setattr (object , name, value) to set the specified property to the specified value of the object
type (object returned object) type
'' '
' ''
a = int (iNPUT ( 'enter an integer:'))
#print(x in range[a] if y in range[x] and (y ** 2 < x) and (y % x = 0))
b = []
c = []
for x in range(2, a + 1):
d = 0
for y in range(2, x):
if (y ** 2 <= x) and (x % y == 0):
d = 1
break
if d == 1:
b.append(x)
else:
c.append(x)
print(b)
print(c)
'''

Guess you like

Origin www.cnblogs.com/fuyouqiang/p/11844639.html