Establish [python learning] class

# class phone:
#     '''
# Restrictions, not allowed to add property
#     '''
#     __slots__=["cpu"]

#     def call(self,num):
# Print ( "with cpu:% s Call% s"% (slef.cpu, num))

#print(phone.__dict__)
#iphone=phone()

#iphone.cpu="A11"
# print(iphone.cpu)

# iphone=phone()
# iphone.call("110")
'''
__new__ was equivalent to c ++ constructor
__init__ is equivalent to initialize
Double underline to become private front, and renamed the process, different ways interpreter renamed
getter and setter:
        Objective and access private variables set by the built-in methods to achieve access to the private variable
        String set to None
        Single underline is directly accessible, but the agreement is commonly known as private variables, try not to use.
The same method with double underscore in front is private methods, but also underscore a
'''
class phone:
    def __init__(self,name):
        self.__name=name
        self._cpu=None
    def setname(self,name):
        if len(name)>10:
            print ( "name can not be more than 10 characters")
            return
        self.__name=name
    def getname(self):
        return self.__name
    def setcpu(self,cpu):
        self._cpu=cpu
    def getcpu(self):
        return self._cpu
    def __connect(self):
        print ( "connected base station")
    def _private(self):
        print ( "private methods accessible")
iphone=phone("iphone")
#print(iphone._phone__name)
# This method can access but not recommended

#print("iphone.__dict__:",iphone.__dict__)

#print("phone.__dict__:",phone.__dict__)

# iphone.setname("apple")
# print(iphone.getname())
# iphone.setcpu("amd")
# print(iphone.getcpu())
iphone._private()

Guess you like

Origin www.cnblogs.com/cyber-shady/p/11588215.html