头歌educoder-Python程序设计-第五阶段 类与对象-类的基础语法

  关卡一:类的声明与定义

# 请在下面填入定义Book类的代码
########## Begin ##########
class Book:
########## End ##########
    '书籍类'
    def __init__(self,name,author,data,version):
        self.name = name
        self.author = author
        self.data = data
        self.version = version

    def sell(self,bookName,price):
        print("%s的销售价格为%d" %(bookName,price))
    

关卡二:类的属性与实例化

class People:
    # 请在下面填入声明两个变量名分别为name和country的字符串变量的代码
    ########## Begin ##########
    def __init__(self, name, country):
        self.name = name
        self.country = country
    ########## End ##########
    def introduce(self,name,country):
        self.name = name
        self.country = country
        print("%s来自%s" %(name,country))
name = input()
country = input()
# 请在下面填入对类People进行实例化的代码,对象为p
########## Begin ##########
p = People(name, country)
########## End ##########
p.introduce(name,country)


关卡三:绑定与方法调用

import fractionSumtest
# 请在下面填入创建fractionSum的实例fs的代码
########## Begin ##########
fs = fractionSumtest.fractionSum()

########## End ##########
n = int(input())
if n % 2 == 0:
    # 请在下面填入调用fractionSumtest类中dcall方法的代码,计算当n为偶数时计算的和
    ########## Begin ##########
    sum = fs.dcall(fs.peven,n)
    ########## End ##########
else:
    # 请在下面填入调用fractionSumtest类中dcall方法的代码,计算当n为奇数时计算的和
    ########## Begin ##########
    sum = fs.dcall(fs.podd,n)
    ########## End ##########
print(sum)




关卡四:静态方法与类方法

class BookSell:
    static_var = 100
    def sell(self,name,author,version,price):
        print("%s的销售价格为%d" %(name,int(price)))
    # 请在下面填入函数修饰符将printStatic()方法声明为静态方法
    ########## Begin ##########
    @staticmethod
    ########## End ##########
    def printStatic():
        print(BookSell.static_var)
    # 请在下面填入函数修饰符将printVersion(cls)方法声明为类方法
    ########## Begin ##########
    @classmethod
    ########## End ##########
    def printVersion(cls):
        print(cls)




        

关卡五: 类的导入

# 从 DataChangetest 模块中导入 DataChange 类,并使用该类中的 eightToten(self,p) 方法,实现将输入的八进制转换成十进制输出。
########## Begin ##########
import DataChangetest
object = DataChangetest.DataChange()
p = input()
object.eightToten(p)




########## End ##########

猜你喜欢

转载自blog.csdn.net/long_0901/article/details/121721630