初学面向对象

# 定义Student类
class Student:
                          
    def __init__(self,name,xuehao,phone):          # 初始化函数
        self.name = name
        self.xuehao = xuehao
        self.phone = phone
        self.__password = '123'  
        
    def say_hello(self):
        print("你好,我是{n},密码是{p}".format(n=self.name,p=self.__password))
        self.print_phone()
    
    def print_phone(self):
        print("你好,我是{n},我的电话是{m}".format(n=self.name,m=self.phone))
# 实例化
zhangsan = Student("zhangsan","01","123456")
# 属性

zhangsan.xuehao
'01'
# 调用方法
zhangsan.say_hello()
你好,我是zhangsan,密码是123
你好,我是zhangsan,我的电话是123456
zhangsan.print_phone()
你好,我是zhangsan,我的电话是123456
上一篇:Java:将DKIM私钥从RSA转换为DER for JavaMail


下一篇:五分钟学Java:“吃人”的那些Java名词(对象、引用、堆、栈)