“objek dan kelas dalam python” Kode Jawaban

Cara Membuat Kelas di Python

class Person:
  def __init__(self, _name, _age):
    self.name = _name
    self.age = _age
   
  def sayHi(self):
    print('Hello, my name is ' + self.name + ' and I am ' + self.age + ' years old!')
    
p1 = Person('Bob', 25)
p1.sayHi() # Prints: Hello, my name is Bob and I am 25 years old!
Red Tailed Cockatoo

Kelas Python

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age
  def myfunc(self):
    print("Hello my name is " + self.name +".")

p1 = Person("Victor", 24)
p1.myfunc()
Blushing Badger

Python kelas

class Employee(Object)
	def __init__(self, name, age, salary):
    	self.name = name
      	self.age = age
        self.salary = salary
        
        
  
  	def __str__(self)
    return f"Employee {name} \nhes age {age} \nand make {salary}"
Evil Earthworm

Kelas Python

class Student:
  def __init__(self, id, name, age):
    self.name = name
    self.id = id
    self.age = age
  
  def greet(self):
    print(f"Hello there.\nMy name is {self.name}")
    
  def get_age(self):
    print(f"I am {self.age}")
    
  def __add__(self, other)
  	return Student(
      self.name+" "+other.name,
      self.id + " "+ other.id,
      str(self.age) +" "+str(other.age))
    
p1 = Student(1, "Jay", 19)
p2 = Student(2, "Jean", 22)
p3 = Student(3, "Shanna", 32)
p4 = Student(4, "Kayla", 23)


result = p1+p3
White Lord

objek dan kelas dalam python

class IntellipaatClass:
	a = 5
	def function1(self):
		print(‘Welcome to Intellipaat’)
#accessing attributes using the class object of same name
IntellipaatClass.function(1)
print(IntellipaatClass.a)
Fancy Flamingo

cara menggunakan kelas dalam python

class awwab(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def speak(self):
        print("Hello, my name is",self.name,"and I am",self.age,"years old!")
        
awwabasad = awwab("Awwab Asad", 11)
print(awwabasad.speak())
Cheerful Cassowary

Jawaban yang mirip dengan “objek dan kelas dalam python”

Pertanyaan yang mirip dengan “objek dan kelas dalam python”

Lebih banyak jawaban terkait untuk “objek dan kelas dalam python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya