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()














  • 相关阅读:
    委托事件学习笔记
    开发小技巧:C#逐个输出字符
    存储过程实例总结(开发中的错误与总结,调试,数据库函数DATEDIFF计算当前日期是否在本周内)
    一个简单的通用面板和菜单类
    PHP配置图文教程
    LaTeX——代码框风格设置
    layui获取弹出层内容
    argis android sdk配置备忘一下
    记录下-两点角度计算
    注册dll命令
  • 原文地址:https://www.cnblogs.com/superblog/p/8481950.html
Copyright © 2011-2022 走看看