class

ソースコード
        
            #coding:utf-8
            #class 2025/02/12
            
            #Defining a class: A class corresponds to a template for an object.
            #The name of a class must be in uppercase, with the rest in lowercase.
            
            class Car:
                def __init__(self,brand,color,type,price,picture): #initialize 初期化
                #self means created instance itself automatically do not need pass anything to self
                
                #attributes 属性,特徴の定義   
                    self.brand = brand
                    self.color = color
                    self.type = type
                    self.price = price
                    self.picture = picture
               
                #methods 方法の定義
                def info(self):  
                    print(f"The car of {self.brand} is {self.color}, type is {self.type}.")
                    print(f" {self.picture} 価格= {self.price}")
            
            #instance,object オブジェクトの実例
            Tesla = Car("Tesla","Black","Model Y,200000,")
            Tesla.info()
            
            #例2
            class Person:
            #初期化
                def __init__(self,f_name,l_name):
                #attributes 属性,特徴の定義     
                    self.firstname = f_name
                    self.lastname = l_name
            
            #method 
                def print_name_eng(self):
                    print(self.firstname,self.lastname)
            
            #method 
                def print_name_jp(self):
                    print(self.lastname,self.firstname)
            
            #instance,object
            x = Person("咲","桜原")
            x.print_name_jp()
            
            y = Person("ashan","prabodha")
            y.print_name_eng()
            
            #例3
            class Rectangle:
                #初期化
                def __init__(self,length,width):
                    #attributes 特徴量、属性
                    self.rect_length = length
                    self.rect_width = width
            
                #methods
                def print_area(self):
                    print(f"面積 = {self.rect_length * self.rect_width}")
            
            #instances
            for i in range(3):
                 x = int(input("長さを入力してください"))
                 y = int(input("幅を入力してください"))
                 shape = Rectangle(10,5)
                 shape.print_area()
                
               

実行結果