面向对象编程

class Myclass:
    i=12345
    def f(self):
        return 'hello word'
x=Myclass()#实例化类
print("Myclass的属性i为:",x.i)
print("Myclass类的方法f输出为:",x.f())
#Myclass的属性i为: 12345
#Myclass类的方法f输出为: hello word

class complex:
    def __init__(self,realpart,imagpart):
        self.r=realpart
        self.i=imagpart

    def f(self):
        return 'hello word'

x=complex(3.0,-4.5)
print(x.r,x.i,x.f())
#3.0 -4.5 hello word

class Test:
    def prt(self):
        print(self)
        print(self.__class__)


t = Test()
t.prt()
# <__main__.Test object at 0x7f7a4546b278>
# <class '__main__.Test'>
#类的方法与普通函数只有一个特别的区别:他们必须有一个额外的第一个参数名称,通常为self
#self代表类的实例,代表当前对象的地址,self.class指向类,main是当前模块

class people:
    name=' '#定义基本属性
    age=0
    __weight=0#私有属性,类外无法直接访问
    def __init__(self,n,a,w):#定义构造方法
        self.name =n
        self.age =a
        self.__weight=w
    def speak(self):
        print("%s 说:我%d岁,%d kg。"%(self.name,self.age,self.__weight))

p=people('ll',10,30)#实例化类
p.speak()
print(p.__weight )#报错,类外不能调用私有变量
# #ll 说:我10岁,30 kg。

class people:
    name=''
    age=0
    __weight=0
    def __init__(self,n,a,w):
        self.name =n
        self.age =a
        self.__weight =w
    def speak(self):
        print("%s说:我%d岁,%d kg"%(self.name ,self.age,self.__weight ))
class student(people):#单继承
    grage=''
    def __init__(self,n,a,w,g):
        people.__init__(self,n,a,w) #调用父类的构造函数
        self.grage=g
    def speak(self):#改写父类的方法
        print("%s 说:我%d岁,在读%d年级 "%(self.name,self.age ,self.grage))

s=student('ken',10,30,6)
s.speak()
#ken 说:我10岁,在读6年级

class people:
    name=''
    age=0
    __weight=0
    def __init__(self,n,a,w):
        self.name =n
        self.age =a
        self.__weight =w
    def speak(self):
        print("%s说:我%d岁,%d kg"%(self.name ,self.age,self.__weight ))
class student(people):#单继承
    grage=''
    def __init__(self,n,a,w,g):
        people.__init__(self,n,a,w) #调用父类的构造函数
        self.grage=g
    def speak(self):#改写父类的方法
        print("%s 说:我%d岁,在读%d年级 "%(self.name,self.age ,self.grage))

class speaker():
    topic=''
    name=''
    def __init__(self,t,n):
        self.topic =t
        self.name =n
    def speak(self):
        print("我叫%s,是个演说家,我演讲的主题是%s"%(self.name,self.topic))
class sample(student,speaker):#多重继承
    a=''
    def __init__(self,n,a,w,t,g):
        student.__init__(self,n,a,w,g)
        speaker.__init__(self,t,n)
test=sample('yim',25,45,'python',5)
test.speak() #方法同名,默认调用靠前的父类的方法
#yim 说:我25岁,在读5年级

class people:
    name=''
    age=0
    __weight=0
    def __init__(self,n,a,w):
        self.name =n
        self.age =a
        self.__weight =w
    def speak(self):
        print("%s说:我%d岁,%d kg"%(self.name ,self.age,self.__weight ))
class student(people):#单继承
    grage=''
    def __init__(self,n,a,w,g):
        people.__init__(self,n,a,w) #调用父类的构造函数
        self.grage=g
    def speak(self):#覆写父类的方法
        print("%s 说:我%d岁,在读%d年级 "%(self.name,self.age ,self.grage))

class speaker():
    topic=''
    name=''
    def __init__(self,t,n):
        self.topic =t
        self.name =n
    def speak(self):
        print("我叫%s,是个演说家,我演讲的主题是%s"%(self.name,self.topic))
class sample(speaker,student):#多重继承
    a=''
    def __init__(self,n,a,w,t,g):
        student.__init__(self,n,a,w,g)
        speaker.__init__(self,t,n)
test=sample('yim',25,45,'python',5)
test.speak()#方法同名,默认调用靠前的父类的方法
#我叫yim,是个演说家,我演讲的主题是python

class parent:
    def mymethod(self):
        print('调用父类方法')
class child(parent):
    def mymethod(self):
        print('调用子类方法')
c=child #子类实例
c.mymethod(1) #子类调用重写方法
super(child,c).mymethod(1)#调用父类方法
# 调用子类方法
# 调用父类方法

class v:#运算符重载
    def __init__(self,a,b):
        self.c=a
        self.d=b
    def __str__(self):
       return 'v(%d,%d)'%(self.c,self.d)
    def __add__(self, other):
        return v(self.c+other.c,self.d+other.d)
v1=v(2,10)
v2=v(5,-2)
#print(v1+v2)
#v(7,8)
# print(v1+v1)
# v(4,20)








没有对象怎么办?当然是new一个对象出来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值