python类

python中的类和对象

风流意气都作罢 提交于 2019-12-03 21:04:53
摘要:本文主要介绍了python中的类和对象的基础内容。由于在c++中对类和对象已经有了较为深刻的认识,所以此部分就是主要熟悉一下创建方法和使用方法。 1、类、对象的创建和简单使用 主要观察创建类的方法,然后用类创建一个对象,然后在创建的对象身上使用类中的方法: 1 class person(): 2 def Output_name(self): 3 print('Tom') 4 5 per=person() 6 per.Output_name() #Tom 在上面的例子中,类中函数的参数有一个self, 其实这个东西相当于c++中的this指针,self相当于创建的对象 ,从下面的例子中可以清楚的理解: 1 class person(): 2 def Output_name(self): 3 print('Tom') 4 print(self) 5 6 per=person() 7 print(per) 8 per.Output_name() #Tom 执行结果如下: 1 <__main__.person object at 0x000001F03FBA65C0> 2 Tom 3 <__main__.person object at 0x000001F03FBA65C0> 2、在类外对对象的属性进行设定 和c++有很大的不同,python可以直接在类外对对象进行属性的设定: 1

python同时继承多个类且方法相同

爱⌒轻易说出口 提交于 2019-12-03 17:26:54
class A(object): def getName(self): print("name is A") class B(object): def getName(self): print("name is B") class C(A, B): def __init__(self): print("class is C") c = C() c.getName() class D(B, A): def __init__(self): print("class is D") d = D() d.getName() 执行结果为: "D:\Program Files\python3.6.7\python.exe" D:/pythonWorkspace/untitled1019/test/test1.py class is C name is A class is D name is B Process finished with exit code 0 来源: https://www.cnblogs.com/harryTree/p/11804747.html

Python类的继承

懵懂的女人 提交于 2019-12-03 10:14:26
基本概念 面向对象三要素之一:继承inheritance。人类和猫类都是继承自动物类。 个体继承自父母,继承了父母的一部分特征,但也可以有自己的个性。在面向对象的世界中,从父类继承,就可以直接拥有父类的属性和方法,这样可以减少代码,多复用。子类可以定义自己的属性和方法。 先看一个不用继承的例子: class Animal: def shout(self): print("Animal shouts") a = Animal() a.shout() class Cat: def shout(self): print("cat shouts") c = Cat() c.shout() 结果为: Animal shouts cat shouts 上面的两个类虽然有关系,但是定义时并没有建立这种关系,而是各自完成定义。 动物类和猫类都有吃,但是它们的吃有区别,所以分别定义。 class Animal: def __init__(self,name): self._name = name def shout(self):#一个通用的吃方法 print("{} shouts".format(self.__class__.__name__)) @property def name(self): return self._name a = Animal("monster") a.shout()

python类的__setattr__()怎么用

試著忘記壹切 提交于 2019-12-03 04:24:07
参考: https://www.cnblogs.com/huchong/p/8287799.html 1.重写了setattr方法后进行赋值 class ClassA(object): def __init__(self, classname): self.classname = classname def __setattr__(self, name, value): # self.name = value # 如果还这样调用会出现无限递归的情况 #self[name]=value object.__setattr__(self,name,value)#如果自定义了__setattr__函数,那么需要这样子赋值 print('invoke __setattr__') insA = ClassA('a') # __init__中的self.classname调用__setattr__。 # invoke __setattr__ print(insA.__dict__) # {} insA.tag = 'insA' # invoke __setattr__ print(insA.__dict__) # {} 输出: invoke __setattr__ {'classname': 'a'} invoke __setattr__ {'classname': 'a', 'tag':

以例子说明python的类、继承和多态

匿名 (未验证) 提交于 2019-12-02 22:54:36
## 1-10 1 class Player : ## written by LiSongbo def __init__ ( self , name ) : self ._name = name self ._score = 0 def reset_score ( self ) : self ._score = 0 def incr_score ( self ) : self ._score = self ._score + 1 def get_name ( self ) : return self ._name def __str__ ( self ) : return "name = %s,score=%s" % ( self ._name, self ._score) def __repr__ ( self ) : return 'Player(%s)' % str ( self ) class Human (Player) : ## Player ,## written by LiSongbo def __repr__ ( self ) : return 'Human(%s)' % str ( self ) def get_move ( self ) : while True : try : n = int ( input ( '%s move (1-10): ' %

Python类方法、静态方法和实例方法的区别

匿名 (未验证) 提交于 2019-12-02 22:54:36
1 # -*- encoding:utf-8 -*- 2 3 class Date ( object ): 4 def __init__ ( self , year , month , day ): 5 self . year = year 6 self . month = month 7 self . day = day 8 9 # 实例方法 10 def tomorrow ( self ): 11 self . day += 1 12 13 # 静态方法(缺点:需要写入固定的类方法名Date) 14 @staticmethod 15 def parse_from_string ( data_str ): 16 year , month , day = tuple ( data_str . split ( " - " )) 17 return Date ( int ( year ), int ( month ), int ( day )) 18 19 # 类方法(传入cls类本身,无需固定类方法名Date) 20 @classmethod 21 def parse_string ( cls , data_str ): 22 year , month , day = tuple ( data_str . split ( " - " )) 23 return cls ( int (

python继承相关知识--父类init调用

匿名 (未验证) 提交于 2019-12-02 22:51:30
1、子类调用父类init构造函数的两种方法: 1)无参构造函数调用 class A (): def __init__ ( self ): print ( "父类的构造函数" ) class B ( A ): def __init__ ( self ): #如果父类有构造函数,子类必须写构造函数,否则会报错 A . __init__ ( self ) #方法一,注意这里有self参数 print ( "子类的构造函数" ) #super(B,self).__init__() #方法二,这里没有self参数 b = B () #父类的构造函数 子类的构造函数 2)父类有参构造函数调用 class Person : total_person = 0 #类变量,所有的实例共享这个变量 def __init__ ( self , name , sex , province ): print ( "I am init function" ) self . name = name #实例变量,每个实例都有但值不一样 self . sex = sex self . province = province Person . total_person += 1 class Student ( Person ): def __init__ ( self , name , sex , province )

Python类__call__()方法

匿名 (未验证) 提交于 2019-12-02 22:51:30
1. 作用 __call__():Python中,只要在创建类型的时候定义了__call__()方法,这个类型就是可调用的。 Python中的所有东西都是对象,其中包括Int/str/func/class这四类,它们都是对象,都是从一个类创建而来的。元类就是创建这些对象的东西,type就是Python的内建元类。 其中,func是可调用的对象,说明在创建它的类型(父类或它本身)的时候,定义了__call__()方法。 所以一个类实例也可以成为类似函数这样能直接调用的对象,只要定义的时候有__call__()方法就可以。 __call__()方法还可以带参数 来源:博客园 作者: bob_coder 链接:https://www.cnblogs.com/bob-coder/p/11532833.html

python_面向对象_类_设计模式

匿名 (未验证) 提交于 2019-12-02 22:51:30
01-------------类------------------- class Person(object): age = 10; def __init__(self, age): if age is not None: self.age = age def speak(self): print("hello") person1 = Person(30) print(person1.age) person2 = Person(None) print(person2.age) ------------------new------------------------------ class Dog(object): def __new__(cls): print("new") return object.__new__(cls) def __init__(self): print("init") def speak(self): print("speak") def __del__(self): print("delete") dog = Dog(); dog.speak() ---------------------------super---------------------- class Fish(object): def __init__(self, name):

Python 类的调用

匿名 (未验证) 提交于 2019-12-02 22:51:30
# !/usr/bin/python # coding=utf-8 # 类的调用 class tests : # 定义一个 tests 的类 def __init__ ( self , start ): # 设置自身属性 self . state = start def mested ( self , label ): # 设置调用方法 print ( label , self . state ) # 输出结果 self . state += 1 # 自身加一 F = tests ( 0 ) # 调用的是__init__,意义在于把start的值赋予给self.state print F . mested ( ' kater ' ) # 调用方法让数字加一,在初始化以后的基础上 print F . mested ( ' boster ' ) 转载于:https://my.oschina.net/xiaocon/blog/199436 来源:51CTO 作者: chiqu8683 链接:https://blog.csdn.net/chiqu8683/article/details/100607198