zoukankan      html  css  js  c++  java
  • python之再学习----简单的类(1)

    # filename:python2.28.py
    # author:super
    # date:2018-2-28


    class Dog(object):

    def __init__(self, name, age):
    self.name = name
    self.age = age

    def sit(self):
    print(self.name.title() + ' is now sitting')

    def roll_over(self):
    print(self.name.title() + ' rolled over')


    class User(object):

    def __init__(self, firstname, lastname):
    self.firstname = firstname
    self.lastname = lastname
    self.login_attempts = 0

    def describe_user(self):
    print("hello , the firstname is " + self.firstname)
    print("hello , the lastname is " + self.lastname)

    def greet_user(self):
    print("hello, " + self.firstname + " " + self.lastname)

    def increment_login_attempts(self):
    self.login_attempts += 1
    return self.login_attempts

    def print_login_attempts(self):
    print(self.login_attempts)

    def reset_login_attempts(self):
    self.login_attempts = 0


    dog1 = Dog('aa', 11)
    dog1.sit()
    dog1.roll_over()

    user1 = User('su', 'super')
    user1.describe_user()
    user1.greet_user()
    print(user1.login_attempts)
    user1.increment_login_attempts()
    user1.print_login_attempts()
    user1.reset_login_attempts()
    user1.print_login_attempts()


    class Car(object):

    def __init__(self, make, model, year):
    self.make = make
    self.model = model
    self.year = year
    self.odometer_reading = 0

    def get_descriptive_name(self):
    long_name = str(self.year) + ' ' + self.make + ' ' + self.model
    return long_name

    def read_odometer(self):
    print("this car have " + str(self.odometer_reading) + "miles")

    def update_odometer(self, mileage):
    self.odometer_reading = mileage

    def increment_odometer(self, miles):
    self.odometer_reading += miles

    def father_function(self):
    print("this is father function")


    class ElectricCar(Car):

    def __init__(self, make, model, year):
    super().__init__(make, model, year)
    self.battery_size = Battery()

    def describe_battery(self):
    print("hello, " + self.battery_size() + "good")

    def father_function(self):
    print("son function")


    # 定义一个新的类a 可以当成一个类b的一个属性来用,通过b.a.function() 可以调用a的方法
    class Battery(object):

    def __init__(self, battery_size=70):
    self.battery_size = battery_size

    def describe_battery(self):
    print("this car's battery is " + str(self.battery_size) + "kwh batter")


    my_son_car = ElectricCar('tesla', 'model s', 2016)
    print(my_son_car.get_descriptive_name())
    # my_son_car.describe_battery()
    my_son_car.father_function()
    my_son_car.battery_size.describe_battery()














  • 相关阅读:
    js对url进行编码和解码(三种方式区别)
    node.js之nodemon 代码热更新 修改代码后服务器自动重启
    深刻理解this的指向和var 定义的变量的问题
    js数组遍历some、foreach、map、filter、every、lastIndexOf、indexOf对比
    div左边固定宽度,右边自适应宽度
    div 自适应高度 自动填充剩余高度
    子组件通过 $emit 触发父组件的自定义事件
    js导出带格式的表格(包括单元格合并,字体样式等)
    vue在生产环境清除console.log
    js判断设备是都是pc端
  • 原文地址:https://www.cnblogs.com/superblog/p/8481950.html
Copyright © 2011-2022 走看看