同前两篇文章一样,链接如下
一天PYTHON速成(PART 01)_m0_57628341的博客-CSDN博客
一天PYTHON速成(PART 02)_m0_57628341的博客-CSDN博客
本文是基础章节的最终章,后面会更新面向对象编程的进阶(有需要就继续看哈),本章也讲了面向对象的思维,但是比较简单
代码如下
使用方法第一篇文章里有讲解
#####第三篇文章
##开始讲:类 啥是类呢,比如,int,float,string,list这些都是一直类
# 但其实类是可以自己进进行创建的
#
#举个例子 : CreditCard - hide() show() pay()
# 那么对于Creditcard来说。hide\show\pay这些玩意儿就是你自己的一个变量
# class CreditCard :
# pass PASS是一个为了防止报错而存在的函数,主要应用于DEF和CLASS
#
# name = 'jim'
# my_card = CreditCard()
# print(type(name)) #<class 'str'> 这是输出的结果,可以方便理解为啥定义了一个类啥的,STR是系统,也可以理解为别人定的类型
# print(type(my_card)) #<class '__main__.CreditCard'> 而这里可以看出,我们输出的MY_CARD已经转变成了我们自己的类了
#
#
##复杂用法
#在这里直接使用类来表示我们C语言中的结构体部分,感觉更加方便、当然,这似乎比结构体更抽象
# class CreditCard :
# pass
#
#
# my_card = CreditCard()
# my_card.number = "1234245615421478"
# my_card.limit = 50000
# my_card.company = 'JoeJim'
#
# print(my_card.company)
#
#
##多实例的创建
#
#
#第一种形式,开始i迷糊了,这里使用的时类中的定义,这种定义时自带的__xxx__函数,很有意思
# class CreditCard :
# def __init__(self) :
# self.number = "1234512365421452"
# print("fxxk")
#
# c1 = CreditCard()
# print(c1.number)
#
#
#引入新的维度,这和C++有很大的不同
# class CDCARD() :
# def __init__(self, number) :
# self.numberX = number
#
# c1 = CDCARD(number = '1452264512458745')
# c2 = CDCARD(number = '5412549865235478')
#
# print(c1.numberX, c2.numberX)
#
#
#
# class CD_card : 其实这里存在强制的赋值和非强制赋值
# def __init__(self, number, limit, company): 一般来说,在__INIT__中,可以对limit, number啥的进行一次赋值,这个属于是默认的
# self.number = number 不过也可以在我的c1或者c2里面在强制赋值
# self.limit = limit
# self.company = company
# c1 = CD_card(number="11111111", limit= 5000, company='Jim')
# c2 = CD_card(number='22222222', limit= 8000, company='Jack')
#
#
# print(c1.number, c1.limit, c2.company)
#
#
##现在来讲一讲在类里面常用的一些方法
# class CD_card :
# def __init__(self, number, company, limit=1000) : #linit这里使用体格默认值,可以在第一个实例里面强行修改
# self.number = number
# self.limit = limit
# self.company = company
# def hide(self) : #同样的道理,这里的定义它给的SELF也必须在CD_card这个实例里使用,这个定义没法儿在其他里使用
# self.number = f'**** **** **** {self.number[-4:]}' #输出之后可以看见,就是类里面自定义的类方法,非常有意思
# c1 =CD_card(number = '4567854214589563', company = 'JimWil')
# print(c1.number)
# c1.hide()
# print(c1.number)
#
#
##调用函数的方法展示
# class CD_card :
# def __init__(self, number, company, limit=1000) :
# self.number = number
# self.limit = limit
# self.company = company
# def hide(self) :
# print('fxxker')
# c1 = CD_card(number = '4567854214589563', company = 'JimWil')
# print(c1.number)
# c1.hide()
# CD_card.hide(c1)
#
#
##创建只属于该类的变量
# class CD_card :
# limit_raise_amount = 1.5
# def __init__(self, number, company, limit=1000) :
# self.number = number
# self.limit = limit
# self.company = company
# def hide(self) :
# self.number = f'**** **** **** {self.number[-4:]}'
# c1 = CD_card(number = '4567854214589563', company = 'JimWil')
# c2 = CD_card(number = '4567854214589563', company = 'JimWil')
# print(CD_card.limit_raise_amount) 首先明确,C1是在我的CD_card里面找到的这个数字
# c2.limit_raise_amount = 2 这里对c2中的数进行了更新
# print(c2.limit_raise_amount) 所以说C2是在上一行找的,而不是在我CD_card这个类里面去找到的
# 而我的实例属性,比如number之类的,它在外面是没法儿进行修改的,就是我把number提出来、
# 之后,依然无法进行进一步修改,只能在c1的类里面修改,没法像limit——raise这种修改
#
#
##一个综合小练习,其实和之前的number的练习,大同小异
# class CD_card :
# limit_raise_amount = 1.25
# def __init__(self, number, company, limit=1000) :
# self.number = number
# self.limit = limit
# self.company = company
# def hide(self) :
# self.number = f'**** **** **** {self.number[-4:]}'
# def raise_limit(self) :
# self.limit = CD_card.limit_raise_amount * self.limit
# print(f'set to {self.limit}')
# c1 = CD_card(number = '4567854214589563', company = 'JimWil', limit = 1200)
# c1.hide()
# print(c1.number)
# c1.raise_limit()
# print(c1.limit)
#
#
##第一个大项目:纸牌游戏,运用类的思维
# import random
# deck = 10
# p_win_score = (deck / 2) + 1
# class Player :
# def __init__(self, name) :
# self.name = name
# self.points = 0
# self.cards = list(range(1,deck + 1,1))
#
# random.shuffle(self.cards)
# def pick_card(self) :
# picked_card = self.cards[0]
# self.cards.remove(picked_card)
# print(f'{self.name} card is {picked_card}')
# return picked_card
# def add_point(self) :
# self.points += 1
# print(f'a point has been added to {self.name}')
# def is_game_over(self) :
# return len(self.cards) == 0 or self.points == p_win_score
#
#
# P1 = Player(name = 'Player 1')
#
# P2 = Player(name = 'Player 2')
#
# P1.pick_card()
# P2.pick_card()
# print("Game Start ...")
# while True :
# input("Press Enter to pick a card")
# P1_CARD = P1.pick_card()
# P2_CARD = P2.pick_card()
# if P1_CARD > P2_CARD :
# P1.add_point()
# elif P2_CARD > P1_CARD :
# P2.add_point()
# else:
# print('TIE!')
#
# if P1.is_game_over() or P2.is_game_over() :
# print("Game Over")
# if P1.points > P2.points :
# print(f'{P1.name} Wins')
# elif P2.points >P1.points :
# print(f'{P2.name} Wins ')
# else:
# print('Score os TIE !')
# print(f'Final Score : {P1.points} - {P2.points}')
# break
|