Python 语言基础 对象,属性,继承,方法

1.对象:万物皆对象

#类 + 类名 ()括号里为继承的对象

#object对象    object相当于祖类

#对象有两部分:①属性  ②方法

class People(object):
      name=''
      age=''
      sex=''
      height=''
      weight=''
class People(object):
    def __init__(self,name='',age='',sex='',yaowei='',height=''):
        self.name = name
        self.age = age
        self.sex = sex
        self.yaowei = yaowei
        self.height = height
        # 4
    def sexIsFit(self , other):
        if self.sex == True and self.sex == other.sex :
            print('我是男的,但是你也是一个男的')
            # 在此 用False代表相亲失败
            return False
        if self.sex == False and self.sex == other.sex :
            print('我是女的,但是你也是女的')
            return  False
        # 6
    def ageIsFit(self ,other):
        if self.sex == False and self.age > other.age :
            print('小弟弟,你太小')
            return False
        if self.sex == True and self.age < other.age:
            print('大姐姐,你太大')
            return False

class Man(People):
    def __init__(self , salary='',house_area='',house_value=''):
        super(Man,self).__init__(name='',sex='',age='',height='',yaowei='')
        self.salary = salary
        self.house_area = house_area
        self.house_value = house_value

        # 2
    def makeFriendWithGirl(self ,other):
        # 3
        result =  super(Man,self).sexIsFit(other)
        if result == False :
            return

        # 5
        result = super(Man,self).ageIsFit(other)
        if result == False :
            return
        # 7
        if other.height < 165 :
            print('我喜欢个子高的,你很好,但是我们不合适')
            return
        # 8
        if other.yaowei > self.yaowei :
            print('我喜欢稍微瘦一点的')
            return
        # 9
        if other.readCount < 100 :
            print('好看的皮囊和有趣的灵魂我都喜欢')
            return
        # 10
        print('你是我的女神')


class Woman(People):
    def __init__(self , name='',age='',sex='',yaowei='',height='',readCount =''):
        super(Woman,self).__init__(name,age,sex,yaowei,height,)
        self.readCount = readCount
    def makeFriendWithBoy(self ,other):
        result = super(Woman, self).sexIsFit(other)
        if result == False:
            return
        result = super(Woman, self).ageIsFit(other)
        if result == False:
            return
        if self.age < other.age - 10 :
            print('你比我大了超过10岁')
            return
        if self.yaowei * 1.5 < other.yaowei :
            print('你的要比我粗的太多')
            return
        if other.salary < 200000 :
            print('你的工资太低');
            return
        if other.house_area < 120 and other.house_value < 2000000 :
            print('你的房子不行')
            return
        print('你是我的男神')


jack = Man()
jack.sex = True
jack.age = 21
jack.height = 176
jack.yaowei = 35
jack.salary = 20000000
jack.house_area = 130
jack.house_value = 2000000

rose = Woman(name='rose',sex=False,age=16,height=167,yaowei=25,readCount=108)

# 1
# jack.makeFriendWithGirl(rose)
rose.makeFriendWithBoy(jack)

def eat(self): print('人类出生就会吃东西') def sleep(self): print('人类天生会睡觉') def work(self): print('每个人都有劳动的权利')p = People()p.name='小明'p.age=23p.sex=Flasep.height=175p.weight='50kg'print(p.name) p.eat()


class People(object):
#init 初始化  系统的方法  用来初始化对象
   def _init_(self,name,age,sex):
    self.name=name
    self.age=age
    self.sex=sex
    def fond(self):
      print('我喜欢泡妹子')
p = People('小陈',23,'男')
p.fond()

#习题练习

1.男生女生都属于人类,有身高,性别,年龄,姓名等属性
# 2.创建一个男生,叫陈烨,包括其他一些属性
# 3.创建一个女生,叫崔莺莺,包括其他一些属性
# 4.女生来判别男生,如果是男的,年龄相差不超过5岁,身上没有负债
#   则愿意和他在一起


class People(object):
   def _init_(self,name='',age=0,sex=Flase,money=0):
     self.name=name
     self.age=age
     self.sex=sex
     self.money=money
   def isFitMySelf(self,other):
       if self.sex == other.sex:
           print('同性排斥')
           return
        if self.age-other.age>5  or self.age-other.age<-5:
             print('年龄不合适')
         if other.money<0:
              print('结束相亲')
              return
          print('相亲成功')
p = People('陈烨',23,Ture,20000000000)
p1 = People('崔莹莹',22,False)
p.isFitMySelf(p1)
class People(object):
      name='道德经'
      age=''
     def _init_(self,fond=''):
          self.fond=fond
     def say(self):
           print('hello')
p = People()
print(p.name)

#对象属性不能通过类名+属性的方式来调用  只能通过对象来调用
# 类属性可以通过类名+属性的方式来调用 也可以通过对象来调用
#对象方法可以通过对象 + 方法名 这种形式类调用
# 也可以通过类名 + 方法名,然后将对象当成参数传入方法中这种形式来调用

2.私有属性

class People(object):
   def _init_(self,name='',age='16',fond='学习'):
     self.name=name
     self._age=age
     self.__fond=fond
#  get  set 方法
    @property
    def fond(self):
       print('fond被get了')
       return self.__fond
    fond.setter
     def fond(self,fond):
         print('fond被set了')
         self.__fond=fond
p = People()
p.name='小张'
print(p.name)
p._age=20
print(p._age)
print(p._People__fond)
           
# 对象属性中,凡是带下划线的_,都是不希望外部使用
# 但是并不是说我们完全不能使用
# 如果加的单下划线_ ,可以通过 p1._name = 'xiaozhang'这种方式使用
# 如果加的是双下划线,可以通过p1._People__name这种当时使用

3.继承

class People(object):
    def _init_(self,name='',age=17,sex=''):
    self.name=name
    self.age=age
    self.sex=sex
    def eat(self):
         print('民以食为天')
calss Man(People):
     def _init_(self,name='',age='',sex='',height='')
     super(Man,self)._init_(name,age,sex)
     def smoke(self):
          print('吸烟有害健康')
      def eat(self):
    #继承父类的eat方法
        super(Man,self).eat()
        print('上面这句话是继承自People的,正在说的这句话才是我自己的')

m = Man()
print(m.age)
m.smoke()
m.eat()m

#面向对象编程的三大特点:

#①封装:函数

#②继承:子类继承父类

扫描二维码关注公众号,回复: 2571490 查看本文章

#③多肽:不同对象调用同样的方法 出现不同结果   就是多肽

4.方法

class People(object):
  #类属性
   count = 0
   size = 0
  def _init_(self,name='',age=''):
      #对象属性
     self.name=name
     self.age=age
    #对象方法
   def say(self):
     print('hai')
# class 类  method 方法
  @classmethod
#  cls   class
   def classFun(cls):
    print('hello,我是累方法')
# static 静态   method 方法
     @staticmethod
#不需要指定self或者cls来调用
   def method():
      print('我是静态方法')
People.classFun()
People.method()
p1=People()
p1.classFun()
p1.method()
p1.say()
People.say(p1)



      

事例




class People(object):
    def __init__(self,name='',age='',sex='',yaowei='',height=''):
        self.name = name
        self.age = age
        self.sex = sex
        self.yaowei = yaowei
        self.height = height
        # 4
    def sexIsFit(self , other):
        if self.sex == True and self.sex == other.sex :
            print('我是男的,但是你也是一个男的')
            # 在此 用False代表相亲失败
            return False
        if self.sex == False and self.sex == other.sex :
            print('我是女的,但是你也是女的')
            return  False
        # 6
    def ageIsFit(self ,other):
        if self.sex == False and self.age > other.age :
            print('小弟弟,你太小')
            return False
        if self.sex == True and self.age < other.age:
            print('大姐姐,你太大')
            return False

class Man(People):
    def __init__(self , salary='',house_area='',house_value=''):
        super(Man,self).__init__(name='',sex='',age='',height='',yaowei='')
        self.salary = salary
        self.house_area = house_area
        self.house_value = house_value

        # 2
    def makeFriendWithGirl(self ,other):
        # 3
        result =  super(Man,self).sexIsFit(other)
        if result == False :
            return

        # 5
        result = super(Man,self).ageIsFit(other)
        if result == False :
            return
        # 7
        if other.height < 165 :
            print('我喜欢个子高的,你很好,但是我们不合适')
            return
        # 8
        if other.yaowei > self.yaowei :
            print('我喜欢稍微瘦一点的')
            return
        # 9
        if other.readCount < 100 :
            print('好看的皮囊和有趣的灵魂我都喜欢')
            return
        # 10
        print('你是我的女神')


class Woman(People):
    def __init__(self , name='',age='',sex='',yaowei='',height='',readCount =''):
        super(Woman,self).__init__(name,age,sex,yaowei,height,)
        self.readCount = readCount
    def makeFriendWithBoy(self ,other):
        result = super(Woman, self).sexIsFit(other)
        if result == False:
            return
        result = super(Woman, self).ageIsFit(other)
        if result == False:
            return
        if self.age < other.age - 10 :
            print('你比我大了超过10岁')
            return
        if self.yaowei * 1.5 < other.yaowei :
            print('你的要比我粗的太多')
            return
        if other.salary < 200000 :
            print('你的工资太低');
            return
        if other.house_area < 120 and other.house_value < 2000000 :
            print('你的房子不行')
            return
        print('你是我的男神')


jack = Man()
jack.sex = True
jack.age = 21
jack.height = 176
jack.yaowei = 35
jack.salary = 20000000
jack.house_area = 130
jack.house_value = 2000000

rose = Woman(name='rose',sex=False,age=16,height=167,yaowei=25,readCount=108)

# 1
# jack.makeFriendWithGirl(rose)
rose.makeFriendWithBoy(jack)



猜你喜欢

转载自blog.csdn.net/crq_zcbk/article/details/80931881
今日推荐