日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python 类

發布時間:2025/6/15 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
類,對象,實例化
class Person(object):
def __init__(self,name,age):
self.name=name
self.age= age
def run(self):
print ("Runing %s"%(self.name))
def speak(self,msg):
print ('%s:%s'%(self.name,msg))

a=Person('jim',22)
b=Person('freeman',23)
a.run()
b.run()
print ("---------------")
a.speak('hello')
b.speak('sb')
############################################################# class Cat(object):
def __init__(self,name,color):
self.name=name
self.color=color
def run(self):
print ('run %s'%(self.name))
def cry(self):
print ('miao miao miao.....')
def catch_mouse(self):
print ('the catch mouse...') class Dog(object):
def __init__(self,name,color):
self.name=name
self.color=color
def run(self,name):
print ('running %s'%(self.name))
def eat(self,eatfood):
print ('%s now is eat'%(self.name,eatfood))
def cry(self):
print ('bark bark')
def swim(self):
print ('The Doe swimming')
def __del__(self):
print ('Done') class Elephant(object):
def __init__(self,name,color):
self.name=name
self.color=color
def cry(self):
print ("The elephant is shouting...")
def spary_water(self):
print ('The elephant is spraying water....') import cat
import dog
import elephant

class Pet(object):
def __init__(self,pet):
self.pet=pet
def pet_cry(self):
self.pet.cry()

#pet= elephant.Elephant('alex','heise')
pet = dog.Dog("fe",'black')
#pet = cat.Cat('mao','color')
My_pet =Pet(pet)
My_pet.pet_cry()
################################### 抽象類
from abc import ABCMeta , abstractmethod
class h1(object):
__metaclass__ = ABCMeta
def __init__(self,a,b):
self.a=a
self.b=b

@abstractmethod
def start(self):
pass
@abstractmethod
def end(self):
pass
@abstractmethod
def abc(self):
print ('abc')

from h1 import h1
class h2(h1):
def __init__(self,a,b):
h1.__init__(self,a,b)
self.name=a
def start(self):
print ('start %s'%(self.name))
def end(self):
print ( 'End')

a=h2('FM','sam')
a.start()
a.end()
print (a.name)
############################################## 類的繼承
#from abc import ABCMeat, abstractclassmethod

class Car(object):
def __init__(self,color,brand):
self.color=color
self.brand=brand
def run(self):
print ('The car is running')
def info(self):
print ("Thar car info %s %s"%(self.color,self.brand))
'''
class Car1(object):
__metaclass__ = ABCMeat
@abstractclassmethod
def start(self):
pass

''' from car import Car
class Bmw(Car):
def __init__(self,color,brand,a):
Car.__init__(self,color,brand)
self.a=a
self.color=color

def start(self):
print ('BMW STARTING %s %s'%(self.a,self.color))
b=Bmw('bmw','bmw123','a')
b.run()
b.info()
b.start()
########################
靜態類寫法 class Tools(object):
#@classmethod
@staticmethod
#def encrypt(cls,data):
def encrypt(data):
print ("Encrypt data....%s"%(data))

#Tools.encrypt(Tools,"Love You")
Tools.encrypt('haha')
###############

轉載于:https://www.cnblogs.com/yjz1/p/5381602.html

總結

以上是生活随笔為你收集整理的Python 类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。