博客笔记_python普通方法、类方法及静态方法学习

今天对python普通方法、类方法及静态方法的知识进行了学习,还是感觉函数间内部的逻辑不是很懂,郁闷啊,难道看的时候分心咯,醉了。知识点都能看懂,但是换个调用方式就又变的迷糊了:

class Fruit: #定义一个类
def init(self): #类的构造函数,用于初始化类的内部状态,为类的属性设置默认值
self.name=name #定义name属性
self.color=color #定义color属性
def grow(self): #定义一个函数,为类的函数,称为方法;它至少有一个参数self
print(‘Fruit grow’)

if name==”main” #当程序作为主程序运行
fruit=Fruit() #实例化:创建一个对象,创建了一个Fruit对象
fruit,grow() #对象调用grow()方法

class Fruit:
price=0 #定义一个类属性
def init(self): #构造函数
self.color=”red” #实例属性,以self为前缀
zone=”China” #局部变量,不以self为前缀
if name==”main“:
print(Fruit.price) #使用类名调用类变量 0
apple=Fruit() #实例化apple
print(apple.color) #打印apple实例的颜色 red
Fruit.price=Fruit.price+10 #将类变量+10
print(“apple’s price:”,+str(apple.price)) #打印apple实例的price 10
banana=Fruit() #实例化banana
print(“banana’s price:”+str(banana.price)) #打印banana实例的price 10
class Fruit:
price=0 #类变量
def init(self): #构造函数
self.__color=”red” #定义一个私有属性,类的外部不能直接访问
def getColor(self): #类方法
print(self.__color) #打印出私有变量

@staticmenthod                      #使用修饰器静态方法
def getPrice():                     #定义一个类方法
    print(Fruit.price)              #打印类变量
def __getPrice():                   #定义私有函数,不能被模块外的类或者方法调用
    Fruit.price=Fruit.price+10      #类变量+10
    print(Fruit.price)
count=staticmenthod(__getPrice)      #定义静态方法

if name==”main“:
apple=Fruit() #实例化apple
apple.getColor() #使用实例私有变量, red;因为创建了对象apple,所以静态属性price执行一次
Fruit.count() #使用列名直接调用静态方法 10
banana=Fruit() #实例化 创建banana对象,所以静态属性第三次执行
Fruit.count() #实例调用静态方法 20
Fruit.getPrice() # 20

object_name = outclass_name.inclass_name()
object_name.method()
第二种:先对外部类进行实例化,然后再实例化内部类,最后调用内部类的方法

out_name = outclass_name()
in_name = out_name.inclass_name()
in_name.method()

class Fruit:
def init(self, color):
self.__color = color
print( self.__color)
def getColor(self):
print( self.__color)
def setColor(self, color):
self.__color = color
print(self.__color)
if name == ‘main‘:
color = ‘red’
fruit = Fruit(color) #red
fruit.getColor() #red

fruit.setColor(‘blue’) #blue

在类里面定义的函数就是方法,类方法需要@ classmethod 修饰并且有个隐藏参数 cls,实例方法必须有个参数 self, 静态方法必须有 @staticmethod修饰,类和实例都可以访问静态方法,实例可以访问实例方法也可以访问类方法,类可以访问类方法也可以访问实例方法,访问实例方法必须要带参数 self, 可以理解为类其实也是一个实例,类访问实例方法不带参数会报错的

--coding:utf-8--

metaclass = type
class Tst:
name = ‘tst’
data = ‘this is data’
# 普通方法
def normalMethod(self,name):
print self.data,self.name
# 类方法,可以访问类属性
@classmethod
def classMethod(cls,name):
print cls.data,name
#静态方法,不可访问类属性
@staticmethod
def staticMethod(name):
print name
tst = Tst()
tst.data = ‘this is new’
tst.normalMethod(‘x’)
tst.staticMethod(‘x’)

tst.classMethod(‘x’)

三种方法都可以通过实例来调用,但是静态方法和类方法无法访问实例属性,所以更改了tst.data仅对普通方法起了作用

普通方法不能通过类名调用,但是静态方法和类方法是可以的

error普通方法必须通过实例调用

Tst.normalMethod(‘name’)

Tst.classMethod(‘name’)
Tst.staticMethod(‘name’)

结果

this is data name
name

普通方法,可以通过self访问实例属性
def normalMethod(self,data)
1
类方法,可以通过cls访问类属性
@classmethod
def classMethod(cls,data)
1
2
静态方法,不可以访问,通过传值的方式
@staticmethod

def staticMethod(data)

Python:类属性,实例属性,私有属性与静态方法,类方法,实例方法
属性分为实例属性与类属性

方法分为普通方法,类方法,静态方法

一:属性:

  尽量把需要用户传入的属性作为实例属性,而把同类都一样的属性作为类属性。实例属性在每创造一个实例时都会初始化一遍,不同的实例的实例属性可能不同,不同实例的类属性都相同。从而减少内存。

  1:实例属性:

    最好在init(self,…)中初始化

    内部调用时都需要加上self.

    外部调用时用instancename.propertyname

  2:类属性:

    在init()外初始化

    在内部用classname.类属性名调用

    外部既可以用classname.类属性名又可以用instancename.类属性名来调用

  3:私有属性:

    1):单下划线_开头:只是告诉别人这是私有属性,外部依然可以访问更改

    2):双下划线__开头:外部不可通过instancename.propertyname来访问或者更改

      实际将其转化为了_classname__propertyname

二:方法

  1:普通类方法:

    def fun_name(self,…):

      pass

    外部用实例调用

  2:静态方法:@staticmethod

      不能访问实例属性!!! 参数不能传入self!!!

      与类相关但是不依赖类与实例的方法!!

  3:类方法:@classmethod

      不能访问实例属性!!! 参数必须传入cls!!!

      必须传入cls参数(即代表了此类对象—–区别——self代表实例对象),并且用此来调用类属性:cls.类属性名

  *静态方法与类方法都可以通过类或者实例来调用。其两个的特点都是不能够调用实例属性

https://github.com/taizilongxu/interview_python#2-python%E4%B8%AD%E7%9A%84%E5%85%83%E7%B1%BBmetaclass


类变量定义在类的定义之后,实例变量则是以为self.开头。例如:
class Foo(object):
val = 0
def init(self):
self.val = 1

if name == ‘main‘:
foo = Foo()
print foo.val
print Foo.val
报错:
class Foo(object):
def init(self):
self.val = 1
class Foo2(Foo):
def init(self):
print self.val

if name == ‘main‘:
foo2 = Foo2()

调用父类的init方法有两种,第一种:
class Foo(object):
def init(self):
self.val = 1
class Foo2(Foo):
def init(self):
Foo.init(self)
print self.val

if name == ‘main‘:
foo2 = Foo2()

class Foo(object):
def init(self):
self.val = 1
class Foo2(Foo):
def init(self):
super(Foo2,self).init()
print self.val

if name == ‘main‘:
foo2 = Foo2()

猜你喜欢

转载自blog.csdn.net/qq_25213395/article/details/81985479