python 面向对象-类和对象的概念和简单使用

时间:2020-08-05 12:27:31   收藏:0   阅读:71

目标:

1、写出有意义的面向对象

2、面向对象的核心(类+对象)

基础1:

基础2:

基础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

基础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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!