Python入门之类的其它特性

第1关 类的内建函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import specialmethodtest

sc = specialmethodtest.subClass()

# 请在下面填入判断subClass是否为parentClass的子类的代码,并输出结果
#********** Begin *********#
result=issubclass(specialmethodtest.subClass,specialmethodtest.parentClass)
print(result)
#********** End **********#
# 请在下面填入判断sc是否为subClass实例的代码,并输出结果
#********** Begin *********#
result=isinstance(sc,specialmethodtest.subClass)
print(result)
#********** End **********#
# 请在下面填入判断实例sc是否包含一个属性为name的代码,并输出结果
#********** Begin *********#
print(hasattr(sc,'name'))
#********** End **********#
# 请在下面填入将sc的属性name的值设置为subclass的代码
#********** Begin *********#
setattr(sc,'name','subclass')
#********** End **********#
# 请在下面填入获取sc的属性name的值的代码,并输出结果
#********** Begin *********#
print(getattr(sc,'name'))
#********** End **********#
# 请在下面填入调用subClass的父类的tell()方法的代码
#********** Begin *********#
pc=specialmethodtest.parentClass()
pc.tell()
#********** End **********#

第2关 类的私有化

1
2
3
4
5
6
7
8
9
10
11
12
import Bagtest

price = int(input())
bag = Bagtest.Bag(price)
# 请在下面填入输出Bag类中变量__price的代码
#********** Begin *********#
print(bag._Bag__price)
#********** End **********#
# 请在下面填入输出Bag类中变量_price的代码
#********** Begin *********#
print(bag._price)
#********** End **********#

第3关 授权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class WrapClass(object):
    def __init__(self,obj):
        self.__obj = obj
    def get(self):
        return self.__obj
    def __repr__(self):
        return 'self.__obj'
    def __str__(self):
        return str(self.__obj)

    # 请在下面填入重写__getattr__()实现授权的代码

    #********** Begin *********#
    def __getattr__(self,thelist):
        return getattr(self.__obj,thelist)
    #********** End **********#




thelist = []
inputlist = input()
for i in inputlist.split(','):
    result = i
    thelist.append(result)
# 请在下面填入实例化类,并通过对象调用thelist,并输出thelist第三个元素的代码
#********** Begin *********#
wc=WrapClass(thelist)
wc_list=wc.get()
print(wc_list[2])
#********** End **********#

第4关 对象的销毁

1
2
3
4
5
6
7
8
import delObjecttest
# 请在下面声明类delObject的实例,并将其引用赋给其它别名,然后调用del方法将其销毁
#********** Begin *********#
test=delObjecttest.delObject()
test2=test
del(test)

#********** End **********#

Python基础(8-1)面向对象程序设计——类和对象

第1关 TOM猫原型 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
###############  begin: 完成cat类##################
class cat:
    def __init__(self):
        self.status=0
        self.ph=100
        self.money=0
    def play(self):
        self.ph -= 5
    def eat(self):
        self.ph = 100
    def work(self):
        self.money += 20
    def show(self):                #展示方法show
        print('money=',self.money,'ph=',self.ph)
###############  end   ##############################

Python入门之类的基础语法

第1关 类的声明与定义

1
2
3
4
5
6
7
8
9
10
11
12
13
# 请在下面填入定义Book类的代码

#********** Begin *********#
class Book(object):
#********** 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))

第2关 类的属性与实例化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)

第3关 绑定与方法调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)

第4关 静态方法与类方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)

第5关 类的导入

1
2
3
4
5
6
7
# 请在下面输入调用DataChange模块中eightToten(self,p)的代码,以实现将输入的八进制转换成十进制输出
#********** Begin *********#
from DataChangetest import DataChange
n=input()
dc=DataChange()
dc.eightToten(n)
#********** End **********#