PYTHON機器學習自學/自修 整理[00019] ~ 語言技術:PYTHON GOSSIP(靜態方法、類別方法)

PYTHON機器學習自學/自修 整理[00019] ~ 語言技術:PYTHON GOSSIP(靜態方法、類別方法)

PYTHON機器學習自學/自修 整理[00019] ~ 語言技術:PYTHON GOSSIP(靜態方法、類別方法)


import sys
import decimal#精準度/精度 運算
import random

class C1:
	a = 3#1

	# seter a
	def s1a(self, a):
		self.a = a
		return self.a

	# geter a
	def g1a(self):
		return self.a

	# set class a
	@classmethod
	def s2a(cls, a):
		cls.a = a
		return cls.a

	# get class a
	@classmethod
	def g2a(cls, ):
		return cls.a

	# static method
	@staticmethod
	def s(string = "Hello, World"):
		return string

	# return self
	def r1(self):
		return self

	# return class
	@classmethod
	def r2(cls):
		return cls

I1 = C1()
I2 = C1()

print(I1.g1a())#物件成員函數
print(I2.g1a())

if(I1.a == 1):#物件成員變數
    print ("I1.a == 1")
else:
    print ("I1.a != 1")
    
if(C1.a == 1):#類別成員變數
    print("C1.a == 1")
else:
    print ("C1.a != 1") 
print ('')


print(I1.g1a())
print(C1.g2a())
if(I1.s1a(9) == 9):#物件呼叫物件成員函數,設定物件成員變數
    print("I1.s1a(9) == 9")
print(I1.g1a())
print(C1.g2a())
print ('')

if(I1.s2a(6) == 6):#物件呼叫類別成員函數,設定類別成員變數
    print("I1.s2a(6) == 6")

print(I1.g1a())
print(I1.g2a())
print(C1.g2a())
print(I2.g1a())
print(I2.g2a())
print ('')

print(I2.a)
print(I2.a == 9)
print(I2.__class__.a)
print(C1.a == 9)
print ('')

print(I2.s())
print(C1.s())
print(I1.s("Hi"))
print(C1.s("Hi jash"))

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *