python如何调用父类中的属性_Python:类的继承,调用父类的属性和方法基础详解...

前面省略继承含义的解释,直接上代码了,更直观点

(1)继承,调用父类属性方法

class Father():

def __init__(self):

self.a='aaa'

def action(self):

print('调用父类的方法')

class Son(Father):

def __init__(self):

self.a='bbb'

def action(self):

print('子类重写父类的方法')

son=Son() # 子类Son继承父类Father的所有属性和方法

son.action() # 调用action方法,这里是自身的action方法而不是父类的action方法

son.a

首先,继承一个类:Son类  继承Father类

class Father():

def __init__(self):

self.a='aaa'

def action(self):

print('调用父类的方法')

class Son(Father):

pass

son=Son() # 子类Son 继承父类Father的所有属性和方法

son.action() # 调用父类方法

son.a # 调用父类属性

输出结果:

840a67dff601bbc846ca1c23e74bb56d.png

注意:在上面的例子中,子类Son没有属性和action的方法,所以会从父类调用,那我们再来看看,子类Son有自身的属性和方法的结果是怎样的?

上述代码修改为:

class Father():

def __init__(self):

self.a='aaa'

def action(self):

print('调用父类的方法')

class Son(Father):

def __init__(self):

self.a='bbb'

def action(self):

print('子类重写父类的方法')

son=Son() # 子类Son继承父类Father的所有属性和方法

son.action() # 子类Son调用自身的action方法而不是父类的action方法

son.a

输出结果:

d8d6db66bad5de9ed58577b406934b9c.png

这里,子类Son已经重写了父类Father的action的方法,并且子类Son也有初始化,因此,子类会调用自身的action方法和属性。总结:如果子类没有重写父类的方法,当调用该方法的时候,会调用父类的方法,当子类重写了父类的方法,默认是调用自身的方法。

另外,如果子类Son重写了父类Father的方法,如果想调用父类的action方法,可以利用super()

代码:

#如果在重新父类方法后,调用父类的方法

class Father():

def action(self):

print('调用父类的方法')

class Son(Father):

def action(self):

super().action()

son=Son()

son.action()

输出结果:

a5649761657ef9ccdd15a2adc5d53bc7.png

(2)强制调用父类私有属性方法

如果父类的方法是私有方法,如 def __action(self)  这样的话再去调用就提示没有这个方法,其实编译器是把这个方法的名字改成了 _Father__action(),如果强制调用,可以这样:

代码:

class Father():

def __action(self): # 父类的私有方法

print('调用父类的方法')

class Son(Father):

def action(self):

super()._Father__action()

son=Son()

son.action()

输出结果:

47920acc77ac9f357f0ae7bdf9ba00f2.png

(3)调用父类的__init__方法

如果自己也定义了 __init__ 方法,那么父类的属性是不能直接调用的,比如下面的代码就会报错

class Father():

def __init__(self):

self.a=a

class Son(Father):

def __init__(self):

pass

son=Son()

print(son.a)

结果报错:

9f4bde1509b25c2067cd44d6305182da.png

修改方法:可以在 子类的 __init__中调用一下父类的 __init__ 方法,这样就可以调用了

class Father():

def __init__(self):

self.a='aaa'

class Son(Father):

def __init__(self):

super().__init__()

#也可以用 Father.__init__(self) 这里面的self一定要加上

son=Son()

print(son.a)

输出结果:

5879465b6cd25e77c58df41f463566a2.png

增加一个函数,参数是继承父类初始化过程中的参数,作为例子,看看结果

代码:

class Father():

def __init__(self):

self.a=1

self.b=2

class Son(Father):

def __init__(self):

super().__init__()

#也可以用 Father.__init__(self) 这里面的self一定要加上

def add(self):

return self.a+self.b

son=Son()

print(son.add())

输出结果:

6dc66a87640bdf73a1e39b54b88b17c5.png

以上是关于Python类的继承,调用父类的属性和方法基础内容,在编程过程中,所遇到的几种继承和调用父类属性所应该注意的问题,上述案例都列举出来,可以自己动手写些案例,加深理解。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 调用 C# dll 库最简方法有以下两种: 1.使用Pythonnet库:Pythonnet是一个能够把 Python 和 .NET 程序集融合在一起的库,可以很方便地调用 C# dll 库。首先需要安装Pythonnet库,在Python导入clr模块,然后使用clr.AddReference添加dll库的引用,最后就可以使用C# dll函数了。 示例代码: ```python import clr clr.AddReference("test.dll") # 添加dll引用 from test import Test # 导入C#Test if __name__ == '__main__': t = Test() print(t.HelloWorld()) # 调用C#Test的HelloWorld方法 ``` 2.使用ctypes库:ctypes是Python的标准库之一,也可以用来调用C# dll库。可以使用LoadLibrary函数加载dll库,然后使用dll函数。 示例代码: ```python import ctypes # 加载dll库 test_dll = ctypes.WinDLL("test.dll") # 定义C#Test的HelloWorld函数 test_dll.Test_HelloWorld.restype = ctypes.c_char_p test_dll.Test_HelloWorld.argtypes = [] if __name__ == '__main__': print(test_dll.Test_HelloWorld().decode()) # 调用C# dll函数Test_HelloWorld ``` 需要注意的是,使用ctypes库调用C# dll库需要在C#使用__declspec(dllexport)标记暴露函数,例如: ```csharp using System; using System.Runtime.InteropServices; namespace Test { public class Test { [DllImport("test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr Test_HelloWorld(); public string HelloWorld() { return Marshal.PtrToStringUni(Test_HelloWorld()); } } } ``` 以上就是Python 调用 C# dll 库最简方法的详细介绍。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值