目标:
1、写出有意义的面向对象
2、面向对象的核心(类+对象)
基础1:
class StudentHomeWork():
基础2:
class Student():
name = “”
age = 0
def print_file():
pass
class Student():
name = “”
age = 0
def print_file():
pass
student = Student()
student.print_file()
基础3:类的基本作用-封装
#coding=utf-8 class Student(): name = "" age = 0 def print_file(self): print("name:"+self.name) print("age:"+str(self.age)) student = Student() student.name = "anson" student.print_file() # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # name: # age:0
#coding=utf-8 class Student(): name = "" age = 0 def print_file(self): print("name:"+self.name) print("age:"+str(self.age)) student = Student() student.name = "anson" student.print_file() # Project/python_ToolCodes/test6.py" # name:anson # age:0
from test6 import Student student = Student() student.name = "anson" student.print_file()
基础4:类->对象以及对象的行为--写出有意义的类
student的行为是写作业,打印机的行为才是打印
#coding=utf-8 class Student(): name = "" age = 0 def do_homework(self): print(self.name +"do homework") class Printer(): def print_file(self): print("name:"+self.name) print("age:"+str(self.age))
原文:https://www.cnblogs.com/ansonwan/p/13438894.html