#! /usr/bin/python3.4
"""
在python中,类的属性和方法默认为"公共的"
以__开头定义为私有,会自动变形名称(_Classname__Methodname)
私有属性防止多重继承的时候出错
"""
class Students():
def __init__(self,my_name,my_id,my_year): #自动运行
self.s_name = my_name
self.s_id = my_id
self.__year = my_year
print("Students init success .. ")
def print_name(self):
print(self.s_name)
def print_id(self):
print(self.s_id)
def __print_year(self):
print(self.__year)
class my_Students(Students): #继承 Students
"""也可以定义自己的__init__
def __init__(self,my_name,my_id):
self.s_name = my_name
self.s_id = my_id
print("my_Students init success .. ")
"""
def show_student(self):
print(self.s_id,':',self.s_name)
if __name__ == "__main__":
s = Students("feng",132,19)
print(s.s_name)
s.print_name()
print(s.s_id)
s.print_id()
print(s._Students__year) #变形后可访问
s._Students__print_year() #变形后可访问
print("-------------")
my_s = my_Students("zhang",101,20)
# my_s = my_Students("zhang",123) #自定义的
my_s.print_name()
my_s.print_id()
my_s._Students__print_year() #变形后可访问
my_s.show_student()