Classes and Objects

Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
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:

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)
 
Tired of cheating
Süper Üye
Katılım
16 Haz 2018
Mesajlar
1,338
Çözümler
4
Tepki puanı
147
Ödüller
10
Yaş
33
7 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:

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)
Are we gonna learn from you and keep posting
 
Onaylı Üye
Katılım
10 Mar 2023
Mesajlar
53
Tepki puanı
3
Ödüller
2
Yaş
26
3 HİZMET YILI
Are we gonna learn from you and keep posting
 
⭐⭐⭐⭐
Süper Üye
Katılım
5 Eyl 2022
Mesajlar
804
Tepki puanı
137
Ödüller
2
Yaş
36
Sosyal
3 HİZMET YILI
an object is an element (or instance) of a class; objects have the behaviors of their class. The object is the actual component of programs
a method is an action which an object is able to perform.
 
Onaylı Üye
Katılım
5 Şub 2022
Mesajlar
50
Tepki puanı
2
Ödüller
2
Yaş
25
4 HİZMET YILI

Create Object​

Now we can use the class named MyClass to create objects:

Example​

Create an object named p1, and print the value of x:
p1 = MyClass()
print(p1.x)



Create a Class​

To create a class, use the keyword class:

Example
Bağlantıları görmek için lütfen Giriş Yap

Create a class named MyClass, with a property named x:
class MyClass:
x = 5
 
Durum
Üzgünüz bu konu cevaplar için kapatılmıştır...
Üst