两题python编程题

 1 # 1.矩形与正方形两个类,求周长与面积。分别使用不继承与继承两种方式,并总结出继承的优势。
 2 class Rectangle:
 3     name="矩形"
 4     def __init__(self):
 5         self.a = 4
 6         self.b = 3
 7     def circumfere(self):
 8         c=((self.a)+(self.b))*2
 9         print("{}的周长为{}".format(self.__class__.name,c))
10     def area(self):
11         s=(self.a)*(self.b)
12         print("{}的面积为{}".format(self.__class__.name,s))
13 
14 class Square():
15     def __init__(self):
16         self.a = 4
17     def circumfere(self):
18         c = (self.a) *4
19         print("正方形的周长为{}".format(c))
20     def area(self):
21         s = (self.a) **2
22         print("正方形的面积为{}".format(s))
23 s1=Rectangle()
24 s1.circumfere()#矩形的周长为14
25 s1.area()#矩形的面积为12
26 s2=Square()
27 s2.circumfere()#正方形的周长为16
28 s2.area()#正方形的面积为16
29 
30 #继承
31 class Square2(Rectangle):
32     def circumfere(self):
33         self.b = self.a
34         self.__class__.name = "正方形"
35         super().circumfere()
36     def area(self):
37         self.b = self.a
38         self.__class__.name = "正方形"
39         super().area()
40 s3=Square2()
41 s3.circumfere()#正方形的周长为16
 42 s3.area()#正方形的面积为16
 1 # 2.编写一个分页显示类,初始化传入记录总数。希望可以通过设置每页记录数和页码,可以显示当前页的信息。
 2 # 其中每页记录数与页码使用property实现。注意,如果页码设置不正确(如<1或者>最大页码),提示错误信息。
 3 # 设计方法能够返回当前页显示的记录区间。
 4 # Page(20)
 5 # pagesize=5
 6 # # 一共应该有4页
 7 # currenPage=6
 8 # show可以显示第三页的信息
 9 # 这是第11-15条记录。。。。
10 class Page:
11     def __init__(self,s):
12         self.s=s
13         self.__pagesize=0
14     @property
15     def pagesize(self):
16         return self.__pagesize
17     @pagesize.setter
18     def pagesize(self,pagesize):
19         self.__pagesize=pagesize
20     def show(self,x):
21         if self.__pagesize>0:
22             a=self.s%self.__pagesize
23             b=self.s//self.__pagesize
24             if a==0:
25                 maxpage=b
26                 if x<1 or x>maxpage:
27                     print("输入页数有误")
28                 else:
29                     print("共{}条记录,共{}页".format(self.s,maxpage))
30                     print("这是第{}页,本页为{}-{}条信息".format(x,(x-1)*self.__pagesize+1,x*self.__pagesize))
31             else:
32                 maxpage =b+1
33                 if x<1 or x>maxpage:
34                     print("输入页数有误")
35                 else:
36                     if x!=maxpage:
37                         print("共{}条记录,共{}页".format(self.s, maxpage))
38                         print("这是第{}页,本页为{}-{}条信息".format(x, (x - 1) * self.__pagesize + 1, x * self.__pagesize))
39                     elif x==maxpage:
40                         print("共{}条记录,共{}页".format(self.s, maxpage))
41                         print("这是第{}页,本页为{}-{}条信息".format(x,(x -1) * self.__pagesize + 1, self.s))
42         else:
43             print("pagesize值不合法,应大于0")
44 p1=Page(20)
45 p1.pagesize=5
46 p1.show(4)
47 #共20条记录,共4页
48 #这是第4页,本页为16-20条信息
49 
50 p2=Page(32)
51 p2.pagesize=6
52 p2.show(6)
53 #共32条记录,共6页
54 #这是第6页,本页为31-32条信息

 

posted @ 2018-09-15 09:49  尨挚  阅读(332)  评论(0编辑  收藏  举报