Krit Th.
20+ yrs in programming professional markets, FMCG researchers, system analyst, university lecturers, broadcasting expert, and so on.
Aj.Krit Th.
class MyClass:
x = 5p1 = MyClass()
print(p1.x)del p1p1 = MyClass()
p2 = MyClass()
p3 = MyClass()
print(p1.x)
print(p2.x)
print(p3.x)class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("อ.กฤษณ์", 26)
print(p1.name)
print(p1.age)class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name)
p1 = Person("Emil", 25)
p1.greet()class Person:
def __init__(self, name):
self.name = name
def printname(self):
print(self.name)
p1 = Person("ม.พชร")
p2 = Person("ม.สรรค์พงษ์")
p1.printname()
p2.printname()class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
car1 = Car("Toyota", "Cross")
print(car1.brand)
print(car1.model)class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("อ.กฤษณ์", 26)
print(p1.age)
p1.age = 27
print(p1.age)class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("ศรีวรการ", 52)
del p1.age
print(p1.name)class Person:
workplace = "ศรีวรการ" # Class property
def __init__(self, name):
self.name = name # Instance property
p1 = Person("กฤษณ์")
p2 = Person("พชร")
print(p1.name)
print(p2.name)
print(p1.workplace)
print(p2.workplace)class Person:
lastname = ""
def __init__(self, name):
self.name = name
p1 = Person("สรรค์พงษ์")
p2 = Person("พชร")
Person.lastname = "วรศิลป์"
print(p1.lastname)
print(p2.lastname)class Person:
def __init__(self, name):
self.name = name
p1 = Person("กฤษณ์")
p1.age = 26
p1.city = "กรุงเทพ"
print(p1.name)
print(p1.age)
print(p1.city)class Person:
def __init__(self, name):
self.name = name
def greet(self):
print("สวัสดี, ฉันชื่อ " + self.name)
p1 = Person("กฤษณ์")
p1.greet()class Calculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
calc = Calculator()
print(calc.add(2, 6))
print(calc.multiply(12, 5))class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def celebrate_birthday(self):
self.age += 1
print(f"สุขสันต์วันเกิด! ปีนี้อายุครบ {self.age} ขวบ")
p1 = Person("กฤษณ์",26)
p1.celebrate_birthday()
p1.celebrate_birthday()class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"{self.name} อายุ {self.age} ขวบ"
p1 = Person("กฤษณ์", 35)
print(p1)
class Playlist:
def __init__(self, name):
self.name = name
self.songs = []
def add_song(self, song):
self.songs.append(song)
print(f"Added: {song}")
def remove_song(self, song):
if song in self.songs:
self.songs.remove(song)
print(f"Removed: {song}")
def show_songs(self):
print(f"Playlist '{self.name}':")
for song in self.songs:
print(f"- {song}")
my_playlist = Playlist("เพลงโปรด")
my_playlist.add_song("กลัวว่าฉันจะไม่เสียใจ")
my_playlist.add_song("ใจฉันตามเธอไป")
my_playlist.show_songs()
© Aj. Krit Th.
https://www.kritth.com
By Krit Th.