The sea does not like to be restrained.
Emektar Üye
- Katılım
- 15 Tem 2021
- Mesajlar
- 1,724
- Çözümler
- 86
- Tepki puanı
- 665
- Ödüller
- 10
- Yaş
- 25
- Sosyal
4 HİZMET YILI
In Python, a class is a blueprint for creating objects. Objects are instances of a class, which contain their own data and methods. Here's an example of a simple class:
In this example, we define a class called Person that has two attributes: name and age. The __init__() method is a special method that is called when an object is created. It takes two parameters, name and age, and initializes the corresponding attributes.
The class also has a method called greet() that prints a greeting message that includes the person's name and age.
To create an object of the Person class, you simply call the class and pass in the required arguments. For example:
This creates a new Person object called person1 with a name of "Alice" and an age of 30.
You can access the attributes of the object using the dot notation. For example:
This would output:
You can also call the methods of the object using the dot notation. For example:
This would output:
You can create multiple objects of the same class, each with their own data and methods. For example:
Python:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name + " and I am " + str(self.age) + " years old.")
In this example, we define a class called Person that has two attributes: name and age. The __init__() method is a special method that is called when an object is created. It takes two parameters, name and age, and initializes the corresponding attributes.
The class also has a method called greet() that prints a greeting message that includes the person's name and age.
To create an object of the Person class, you simply call the class and pass in the required arguments. For example:
Python:
person1 = Person("Alice", 30)
This creates a new Person object called person1 with a name of "Alice" and an age of 30.
You can access the attributes of the object using the dot notation. For example:
Python:
print(person1.name)
print(person1.age)
This would output:
Python:
Alice
30
You can also call the methods of the object using the dot notation. For example:
Python:
person1.greet()
This would output:
Python:
Hello, my name is Alice and I am 30 years old.
You can create multiple objects of the same class, each with their own data and methods. For example:
Python:
person2 = Person("Bob", 25)
person3 = Person("Charlie", 40)