import urllib.request
···
class MyClass:
'''一个简单的实例类'''
i = 12345
def f(self):
return 'hello,world!'
···
x = MyClass()
print("MyClass 类的属性 i 为:",x.i)
print("MyClass 类的方法 f 的输出为:",x.f())
class Complex:
def init(self,real,ima):
self.r = real
self.i = ima
x = Complex(2,4.4)
print(x.r,x.i)
class Text:
def prt(self):
print(self)
print(self.class)
l = Text()
print(l.prt())
class People:
#类的基本属性
name = ''
age = 0
#定义私有属性,私有属性在类外部无法直接进行访问
__weight = 0
#定义构造方法
def init(self,n,a,w):
self.name = n
self.age = a
self.__weight = w
def speak(self):
print("%s 说:我 %a 岁了" %(self.name,self.age))
print(self.__weight)
p = People('阿涛',22,190)
p.speak()
class Student(People):
grade = 0
def init(self,n,a,w,g):
#调用父类的构造函数
People.init(self,n,a,w)
self.grade = g
#复写父类的方法
def speak(self):
print("%s说 我%d 岁了我在 %d 年级"%(self.name,self.age,self.grade))
s = Student("小珊",21,60,3)
s.speak()