python:面向对象的编程语言,以类的语法写代码

类的语法:

class 类名():

  属性(类似变量)


  方法(类似函数)


class Test1():

#    ip = "10.1.1.1"


   #构造方法,作用:为类中的属性动态赋值

   def __init__(self,ip):

#       print("构造方法")

       self.ip = ip


   def hello(self):

       print("这是个hello测试")



   #通过self调用数据

   def ssh(self):

       self.hello()

       print("ssh root@%s" % self.ip)


if __name__ == '__main__':

   #实例化,创建对象   类名()

   p1 = Test1()

   print(p1)

   print(p1.ip)

   p1.hello()

   p1.ssh()



   p2 = Test1()

   p2.ssh()



   p1 = Test1(ip="10.1.1.1")

   p1.ssh()  

   p2 = Test1(ip="172.16.10.1")

   p2.ssh()  


特殊方法:构造方法/函数


1)名字是固定的   __init__

2) 创建对象时,方法里的代码会自动执行