Created
January 31, 2022 19:05
-
-
Save demohero101/0808ead1d87b69f3a2323513b5ccd1b0 to your computer and use it in GitHub Desktop.
Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Class for a car (Araba için class) | |
class Car(): | |
# Class Variables (Class değişkenleri) | |
# Attributes (Özellikler) | |
model = "Golf" | |
color = "Sarı" | |
# Objects of Car class (Nesneler) | |
car1 = Car() | |
car2 = Car() | |
print(car1.model) | |
print(car2.color) | |
############################################### | |
############################################### | |
class Car(): | |
# init method-constructor (Yapıcı fonksiyon) | |
# instance variables | |
def __init__(self, model, color): | |
self.model = model | |
self.color = color | |
# Objects of Car class (Nesneler) | |
car1 = Car("Golf", "Sarı") | |
car2 = Car("Passat", "Gümüş") | |
print(car1.model) | |
print(car2.color) | |
############################################### | |
############################################### | |
class Student: | |
# init method-constructor (Yapıcı fonksiyon) | |
# instance variables | |
def __init__(self, name, surname, age): | |
self.name = name | |
self.surname = surname | |
self.age = age | |
# Objects of Student class (Nesneler) | |
std1 = Student("Ahmet", "Yalçın", 14) | |
std2 = Student("Aylin", "Yılmaz", 15) | |
print(std1.name, std1.surname, std1.age) | |
print(std2.name, std2.surname, std2.age) | |
############################################### | |
############################################### | |
# Class for cat (Kedi için class) | |
class Cat: # animal olarak değiştirip örnek verebilirsin | |
# init method-constructor (Yapıcı fonksiyon) | |
# instance variables | |
def __init__(self, name, color, age): | |
self.name = name | |
self.color = color | |
self.age = age | |
# Object of cat class (Nesneler) | |
cat1 = Cat("DEJA-VU", "Siyah", 6) | |
cat2 = Cat("Pamuk", "Beyaz", 3) | |
print(cat1.name, cat1.color, cat1.age) | |
print(cat2.name, cat2.color, cat2.age) | |
############################################### | |
############################################### | |
# Class for a character (Bir oyun karakteri için class) | |
class Character: | |
life = 5 # Class Variable (Class değişkeni) | |
# Method (Metod) | |
def attacked(self): | |
self.life -= 1 | |
print("Saldırıya uğradın ve", str(self.life) + " canın kaldı.") | |
# Method (Metod) | |
def health(self): | |
self.life += 1 | |
print("Yardım paketi buldun ve canın", str(self.life), "oldu.") | |
gamer1 = Character() | |
gamer1.attacked() | |
gamer1.health() | |
############################################### | |
############################################### | |
class Person: | |
# init method-constructor (Yapıcı fonksiyon) | |
# instance variables | |
def __init__(self, name, surname): | |
self.name = name | |
self.surname = surname | |
# Method | |
def greet(self): | |
print("Hoş geldiniz sayın {} {}.".format(self.name, self.surname)) | |
# Object of Person class (Nesne) | |
per_info = Person("Murat", "Yılmaz") | |
per_info.greet() | |
# print(per_info.name, per_info.surname) | |
############################################### | |
############################################### | |
# Class for a student (Öğrenci için class) | |
class Student: | |
# Class Variables (Class değişkenleri) | |
# Attributes (özellikler) | |
attrib1 = "Çalışkanlık" | |
attrib2 = "Azim" | |
# method | |
def per_quality(self): | |
print("Öğrencide bulunması gereken ilk özellik: ", self.attrib1) | |
print("Öğrencide bulunması gereken ikinci özellik: ", self.attrib2) | |
# Objects of Student class (Nesne) | |
person = Student() | |
person.per_quality() | |
# print(Person.attrib1, Person.attrib2) | |
############################################### | |
############################################### | |
# Class for a dog (Köpek için class) | |
class Dog: | |
# Class Variables (Class değişkenleri) | |
breed1 = "Bulldog" | |
breed2 = "Shepherd" | |
# init method-constructor (Yapıcı fonksiyon) | |
# instance variables | |
def __init__(self, color, age): | |
# Instance Variables (Instance değişkenleri) | |
self.color = color | |
self.age = age | |
# Objects of Dog class | |
lucy = Dog("Beyaz", 3) | |
buddy = Dog("Siyah", 6) | |
print("Lucy'nin türü:", Lucy.breed1) | |
print("Lucy'nin rengi:", Lucy.color) | |
print("Lucy'nin yaşı:", Lucy.age) | |
print("\nBuddy'nin türü:", Buddy.breed2) | |
print("Buddy'nin rengi:", Buddy.color) | |
print("Buddy'nin yaşı:", Buddy.age) | |
# print(Dog.breed1) | |
# print(Dog.breed2) | |
############################################### | |
############################################### | |
# Parent Class for cat (Kedi için class) | |
class Cat: | |
# init method-constructor (Yapıcı fonksiyon) | |
# instance variables | |
def __init__(self, name, color, age): | |
self.name = name | |
self.color = color | |
self.age = age | |
# method | |
def show_info(self): | |
print("Evcil hayvanın bilgileri:", self.name, self.color, self.age) | |
# Objects of Cat class (Nesneler) | |
cat1 = Cat("DEJA-VU", "Siyah", 6) | |
cat1.show_info() | |
# Child Class for Dog (Köpek için class) | |
class Dog(Cat): | |
pass | |
# Objects of Dog class (Nesneler) | |
dog1 = Dog("Buddy", "Beyaz", 8) | |
dog1.show_info() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment