Python面向对象习题

# coding=utf-8
"""
一:定义一个学生类。有下面的类属性:
1 姓名
2 年龄
3 成绩(语文,数学,英语)[每课成绩的类型为整数]
类方法:
1 获取学生的姓名:get_name() 返回类型:str
2 获取学生的年龄:get_age() 返回类型:int
3 返回3门科目中最高的分数。get_course() 返回类型:int
写好类以后,可以定义2个同学测试下:
zm = Student('zhangming',20,[69,88,100])
返回结果:
zhangming
20
100
"""
class Student(object):
    def __init__(self, name, age, scores):
        self.name = name
        self.age = age
        self.scores = scores
    def get_name(self):
        return self.name
    def get_age(self):
        return self.age
    def get_course(self):
        return max(self.scores)
zm = Student('zhangming', 20, [69, 88, 100])
print zm.get_name()
print zm.get_age()
print zm.get_course()
"""
二:定义一个字典类:dictclass。完成下面的功能:


dict = dictclass({你需要操作的字典对象})

1 删除某个key

del_dict(key)


2 判断某个键是否在字典里,如果在返回键对应的值,不存在则返回"not found"

get_dict(key)

3 返回键组成的列表:返回类型;(list)

get_key()

4 合并字典,并且返回合并后字典的values组成的列表。返回类型:(list)

update_dict({要合并的字典})
"""
class dictclass(object):
    def __init__(self, dict):
        self.dict = dict
    def get_dict(self, key):
        if self.dict. has_key(key):
            return self.dict[key]
        return 'not found'
    def del_dict(self, key):
        if self.dict.has_key(key):
            self.dict.pop(key)
        else:
            return 'no that key'
    def get_key(self):
        return self.dict.keys()
    def updata_dict(self, dict2):
        self.dict = dict(self.dict, **dict2)
        return self.dict.values()

A = dictclass({'a': 1, 'b': 2})
print A.get_dict('c')
print A.del_dict('c')
print A.get_key()
print A.updata_dict({'c': 3, 'd': 4})


# 关于删除字典中的元素
# pop 删除并返回对应的value值
B = {'a': 1, 'b': 2}
print B.pop('b')
print B
# del void不返回值
C = {'a': 1, 'b': 2}
del C['a']
print C
# del dict 删除字典,不存在这个字典
# dict.clear()删除字典内所有元素但字典还是存在只是没有元素
D = {'a': 1, 'b': 2, 'c': 3}
D.clear()

# 关于合并字典
A = {'a': 1, 'b': 2, 'c': 3}
B = {'d': 4, 'e': 5, 'f': 6}
C = dict(A.items() + B.items())
D = dict(A, **B)  # 这种合并方法会比上一种快很多对于重复的key,B会覆盖A

"""
定义一个列表的操作类:Listinfo

包括的方法:

1 列表元素添加: add_key(keyname)  [keyname:字符串或者整数类型]
2 列表元素取值:get_key(num) [num:整数类型]
3 列表合并:update_list(list)	  [list:列表类型]
4 删除并且返回最后一个元素:del_key()

list_info = Listinfo([44,222,111,333,454,'sss','333'])
"""


class Listinfo(object):
    def __init__(self, list_val):
        self.varlist = list_val

    def add_key(self, key_name):
        if isinstance(key_name, (str, int)):
            self.varlist.append(key_name)
            return self.varlist
        else:
            return 'error'

    def get_key(self, num):
        if num >= 0 and num < len(self.varlist):
            return self.varlist[num]
        else:
            return 'error'

    def update_list(self, List_et):
        self.varlist.extend(List_et)
        return self.varlist

    def del_key(self):
        if len(self.varlist) >= 0:
            return self.varlist.pop(-1)
        else:
            return 'error'
list_info = Listinfo([44, 222, 111, 333, 454, 'sss', '333'])
print list_info.add_key('1111')
print list_info.get_key(4)
print list_info.update_list(['1', '2', '3'])
print list_info.del_key()
"""
定义一个集合的操作类:Setinfo

包括的方法:

1 集合元素添加: add_setinfo(keyname)  [keyname:字符串或者整数类型]
2 集合的交集:get_intersection(unioninfo) [unioninfo :集合类型]
3 集合的并集: get_union(unioninfo)[unioninfo :集合类型]
4 集合的差集:del_difference(unioninfo) [unioninfo :集合类型]
set_info =  Setinfo(你要操作的集合)
"""
class Setinfo(object):
    def __init__(self, my_Set):
        self.Sett = my_Set

    def add_setinfo(self, keyname):
        if isinstance(keyname, (int, str)):
            self.Sett.add(keyname)
            return self.Sett
        return 'error'

    def get_intersection(self, unioninfo):
        if isinstance(unioninfo, set):
            return self.Sett & unioninfo
        return 'error'

    def get_union(self, unioninfo):
        if isinstance(unioninfo, set):
            return self.Sett | unioninfo
        return 'error'

    def del_difference(self, unioninfo):
        if isinstance(unioninfo, set):
            return self.Sett - unioninfo
        return 'error'

A = set([1, 2, 3, 4, 5, 2])
B = set([5, 6, 3])
set_info = Setinfo(A)
print set_info.add_setinfo('f')
print set_info.get_intersection(B)
print set_info.get_union(B)
print set_info.del_difference(B)
"""
题目一: 写一个网页数据操作类。完成下面的功能:

提示:需要用到urllib模块

get_httpcode()获取网页的状态码,返回结果例如:200,301,404等 类型为int

get_htmlcontent() 获取网页的内容。返回类型:str

get_linknum()计算网页的链接数目。

"""
import urllib
import urllib2
class page_data(object):
    def __init__(self, url_add):
        self.url = url_add

    def get_httpcode(self):
        status = urllib.urlopen(self.url).code
        return status

    def get_htmlcontent(self):
        contentstr = urllib2.urlopen(self.url).read()
        return contentstr

    def get_linknum(self):
        content = urllib2.urlopen(self.url).read()
        return len(content.split('<a href=')) - 1

A = page_data("http://www.baidu.com")
print A.get_httpcode()
print A.get_htmlcontent()
print A.get_linknum()
# coding=utf-8
class SchoolMember:
    # Represents any school member.
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print '(Initialized SchoolMember: %s)' % self.name

    def tell(self):
        # Tell my details.
        print 'Name:"%s" Age:"%s"' % (self.name, self.age),


class Teacher(SchoolMember):
    # Represents a teacher.
    def __init__(self, name, age, salary):
        SchoolMember.__init__(self, name, age)
        self.salary = salary
        print '(Initialized Teacher: %s)' % self.name

    def tell(self):
        SchoolMember.tell(self)
        print 'Salary: "%d"' % self.salary


class Student(SchoolMember):
    # Represents a student.
    def __init__(self, name, age, marks):
        SchoolMember.__init__(self, name, age)
        self.marks = marks
        print '(Initialized Student: %s)' % self.name

    def tell(self):
        SchoolMember.tell(self)
        print 'Marks: "%d"' % self.marks

t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 22, 75)
c = SchoolMember('Swar', 22)
t.tell()
s.tell()
c.tell()





展开阅读全文

没有更多推荐了,返回首页