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














  • 相关阅读:
    Web应用指纹识别
    同步I/O 和 异步I/O
    怎样找出自己定义标签的java类
    Android多线程文件下载器
    cocos2d-x 3.0游戏实例学习笔记 《跑酷》 第三步---主角开跑&同一时候带着刚体
    记C++课程设计--学生信息管理系统
    iOS开发--从TQRichTextViewDemo中学会分析project
    九度oj题目&吉大考研10年机试题全解
    setOnFocusChangeListener的使用
    查看网络port占用
  • 原文地址:https://www.cnblogs.com/superblog/p/8481950.html
Copyright © 2011-2022 走看看