面向对象简述

简述面向对象三大特性并用示例解释说明?

1,封装:将对象的属性集成在

class person:
    def __init__(self,name,idnum):
        self.name=name
        self.idnum=idnum

2,继承:子类自动拥有父类的的封装,除了非私有之外

class person:
    def __init__(self,name,idnum):
        self.name=name
        self.idnum=idnum
class child(person):
        pass
child=child("bigc","1000101")
print(child.name)

3,多态:一个变量具有多种形态

class person:
    def __init__(self,name,idnum):
        self.name=name
        self.idnum=idnum
class child(person):
        pass
child=child("bigc","1000101")
print(child.name)

child1=child("bigc1","10001011")

面向中的变量分为哪几种?并用示例说明区别?

实例变量:

class person:
    def __init__(self,name,idnum):
     # 成员变量(实例变量) self.name
=name self.idnum=idnum aman=person("hallo kitty","6688")

类变量:

class person:
    def __init__(self,name,idnum):
        self.name=name
        self.idnum=idnum
    @classmethod
    def test(cls):
        p=person()

面向对象中方法有哪几种?并用示例说明区别?

实例方法:在使用的时候用self的方式

类方法:@classmethod传递类名的时候使用

静态方法:@staticmethod不需要传递当前类的对象的时候使用

面试题: 说一说, 静态方法, 类方法和实例方法的区别.

面向对象中的属性有什么?并用示例说明?

属性
用方法来描述我们的属性信息.
注意:
1. @propery 改变一个方法成为属性
2. 这个方法只能有一个参数, self
3. 必须有返回值.

class person:
    def __init__(self,name,birth,edu):
        self.name=name
        self.birth=birth
        self.edu=self.edu
        @propery
         def age(self):
                return 2018-birth

简述静态方法与类方法的区别

1静态方法相当于函数:里面的对象不需要被传递

2类方法 传递的是类名

面向对象的方法中那个无需传参数?

静态方法:不需要传递类或者对象

面向对象的方法中共有和私有成员在编写和调用的时候有哪些不同?

class haoguanyuan:
    def __init__(self,name,waimiao,money,fangchan,qingfu):
        self.name=name
        self.waimiao=waimiao
        self.__money=money
        self.__fangchan=fangchan
        self.__qingfu=qingfu
haoren=haoguanyuan("李逵","和蔼可亲","9999999","8套房","几个新来的实习女大学生")
print(haoren.name)
print(haoren.waimiao)

》》》李逵

》》》和蔼可亲

》》》print(haoren.money)

AttributeError: 'haoguanyuan' object has no attribute 'money'

公有类,name waimiao ,私有类:money ,qingfu,fangchan,私有类只能内部访问使用,子类不能继承

私有类在属性绑定的时候会需要两个下划线

class haoguanyuan:
    def __init__(self,name,waimiao,money,fangchan,qingfu):
        self.name=name
        self.waimiao=waimiao
        self.__money=money
        self.__fangchan=fangchan
        self.__qingfu=qingfu
    def buy(self):
        print("我有%s的钱"%(self.__money))
haoren=haoguanyuan("李逵","和蔼可亲","9999999","8套房","几个新来的实习女大学生")
print(haoren.name)
print(haoren.waimiao)
haoren.buy()

李逵
和蔼可亲
我有9999999的钱

 结果:1

12

999

11

11

a2报错

error

1

error  不存在

2

1

2

666
1
18
99

1
1000
1017
  

 1 class Foo(object):
 2     hobby="大保健"
 3     def __init__(self,num):
 4         self.num=num
 5         self.__salary=1000
 6     def f1(self):
 7         print(Foo.hobby)
 8     @staticmethod
 9     def f2():
10         print(Foo.hobby)
11     @classmethod
12     def f3(cls):
13         print(cls.hobby)

<class '__main__.Foo'>

 

<class '__main__.Foo'>

<class "_main_.Foo">
<class "_main_.Foo">

1 foo.f2
2 base.f1
3 <class "_main_.Foo">

请编在写一个私有的静态方法,并通过代码证明私有方法不不能再外部方法但可以在内部访问。

class Person:
    age = 0
    wegiht = 0
    __sex = ''
    def __init__(self):
        pass
    @staticmethod
    def get_stactic_age():
        Person.age += 1    # 这里不能用self.age访问
    def __add(self):
        print("这是一个私有方法,只能在类中使用")
    def get_weight(self):
        print("这是一个公有方法,可以被类的实例使用")
tom = Person()
tom.get_weight()    #这个可以正常访问
tom.__add()           #这个不能正常访问,报AttributeError: 'Person' object has no attribute '__add'异常

现有500W条数据.请使用面向对象的思维来完成这500W条数据的分页

class Page:
    def __init__(self,lst,pagesize):
        self.lst=lst
        self.pagesize=pagesize#测试40行为1页
    def start(self):
        return self.lst[0:self.pagesize]
    def end(self):
        return self.lst[-len(self.lst)%self.pagesize:]
    def index(self,num):
        return self.lst[(num-1)*(self.pagesize):num*(self.pagesize)]
data=["%s测试数据
"%i for i in range(1,5000001)]
show=Page(data,40)
ye=int(input("请输入你要查询的数据的页数:"))
print(show.index(ye))
原文地址:https://www.cnblogs.com/bigc008/p/9706851.html