Python study notes (36-40) classes and objects

Learning Topics: Classes and Objects
Learning Date: 2020-02-09
Python Version: 3.7.4
Learning Description: It's hard here, especially the first contact with the object-oriented concept, I write below is not very well understood.

36 speak object concept
for a description of how the object.
For example, the object is to dog
the basic characteristics of a dog is that a head, four legs, hairy, have tails, and so
the dog can do, he can run, eat things can bite, can smell things and so on .

To summarize:
the object attribute = + method , where a class might be more accurate point, so that a class, Class.
Class Chinese meaning is: Like attracts like. A class of people. What it is to have a person of the same features, such as an alcoholic, right, so a group of people that love to drink, they love to drink this property, they have to drink this ability method.
Concept of the object is very broad, in Python, a function, a method, a variable what can be called an object. So, Python is everywhere object.

  • The definition of a class
#下面就定义了“狗”类
#记住:Python中类名首字母要大写,函数的首字母小写,这样就容易区别了
class  Dog:
    #属性
    legs=4
    head=1
    mouth=1

    #方法
    def beat(self):
        print('dog can beat ')
    def run(self):
        print('dog can run')
    def smell(self):
         print('dog can smell')
  • Call the "class"
>>> 
 RESTART: C:/Users/SNIPER/AppData/Local/Programs/Python/Python37/SniperPyWorks/game4.py 
>>> d=Dog()
>>> d
<__main__.Dog object at 0x0000000002F65B08>
>>> type(d)
<class '__main__.Dog'>
>>> d.legs
4
>>> d.beat
<bound method Dog.beat of <__main__.Dog object at 0x0000000002F65B08>>
>>> d.beat()
dog can beat 
>>> 

OO is the What?
That is to orient object object-oriented meaning

Characteristics of OO: encapsulation, inheritance, polymorphism.

#继承举例说明
>>> class list1(list):
	pass
>>> list2=list1()
>>> list2.append(1)
>>> list2
[1]
#举例说明什么是多态
#定义一个类A
>>> class A:
	def fun(self):
		print('i am A')

#定义一个类B		
>>> class B:
	def fun(self):
		print('i am B')

		
>>> a=A()
>>> b=B()
>>> a.fun()
i am A
>>> b.fun()
i am B
#发现上面同样是fun函数,但是结果不同,所以这就是 多态

Object-Oriented Programming Lecture 37

What self is?
Python's self is equivalent to C ++ self pointer of this
public and private?
Python is a pseudo-private

Article 38 speaks inherited
make similar passed, do not have to re-write. This approach is called inheritance.
Inherited is called: the base class (called parent class or superclass)
successor called: sub-categories
used as follows:

#DerviedClassName  子类
#BaseClassName     基类(或叫父类,超类 )
class DerviedClassName(BaseClassName):
    ...

For chestnuts

>>> class Parent:
	def hello(self):
		print('正在调用父类的方法...')

		
>>> class Child(Parent):
	pass

>>> p=Parent()
>>> p.hello()
正在调用父类的方法...
>>> c=Child()
>>> c.hello()
正在调用父类的方法...
>>> 

>>> class Parent:
	def hello(self):
		print('正在调用父类的方法...')

>>> class Child(Parent):
	def hello(self):
		print('正在调用  子类 的方法')

		
>>> 
>>> c=Child()
>>> c.hello()
正在调用  子类 的方法

Use super function.

The use of multiple inheritance.

39 speak some concepts

combination

>>> class Classmate:
	def __init__(self,y):
		self.num=y

		
>>> class Teacher:
	def __init__(self,x):
		self.num=x

		
>>> class Classroom:
	def __init__(self,x,y):
		self.teacher=Teacher(x)#实例化
		self.classmate=Classmate(y)#实例化
	def print_num(self):
		print('教室里面有老师 %d 名,学生 %d 名'% (self.teacher.num,self.classmate.num))

>>> classroom=Classroom(1,40)
>>> type(classroom)
<class '__main__.Classroom'>
>>> classroom.print_num
<bound method Classroom.print_num of <__main__.Classroom object at 0x0000000002FAA988>>
>>> classroom.print_num()
教室里面有老师 1 名,学生 40

It is an example of a combination of the above. Several the inheritance relationship is not, generally parallel relationship transversely several classes, of the instantiated together.

Class, an object class, instance objects

#C是类 定义
>>> class C:
	num=0
#a,b为 实例对象,下面的C是类对象
>>> a=C()
>>>> a.num
0
>>> a.num=10
>>> a.num+=5
>>> a.num
15
>>> b=C()
>>> b.num
0

If the same properties and methods, then what will happen

>>> class C:
	def x(self): #x方法
		print('x!!')
		
>>> c=C()
>>> c.x()
x!!
>>> c.x=1
>>> c.x #查看属性
1
>>> c.x()#查看方法
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    c.x()
TypeError: 'int' object is not callable
#z这里已经报错了

So, pay attention:

  • Do not try to define all properties and methods can think of in a mine inside, one should use a combination of inheritance and mechanism for expansion.
  • Different speech named attribute names as noun, verb method.

Binding
Python method requires stringent requirements in order to be invoked instance, this restriction is actually called Python bindings concept.

Lecture 40 some relevant BIF

#issubclass(class,classinfo)
>>> class A:
	pass

>>> class B(A):
	pass


>>> issubclass(B,A)
True


>>> class C:
	pass

>>> issubclass(B,C)
False
#hasattr(object,name)测试一个对象里面是否有某个属性
#注 attr的全称为attribute ,属性的意思


#getattr(object,name[,default])
#setattr(object,name,value)
#property

Published 81 original articles · won praise 57 · views 10000 +

Guess you like

Origin blog.csdn.net/hahahahhahha/article/details/104242309
Recommended