create class python code example
Example 1: how to make a class in 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()
Example 2: declare class python
class Shape:
def __init__():
print("A new shape has been created!")
pass
def get_area(self):
pass
class Rectangle(Shape):
def __init__(self, height, width):
super.__init__()
self.height = height
self.width = width
def get_area(self):
return self.height * self.width
Example 3: python class
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("barry", 50)
p1.myfunc()
Example 4: class python
class MyClass(object):
def __init__(self, x):
self.x = x
Example 5: classes in python
class Foo:
def __init__(self):
self.definition = Foo!
def hi():
Example 6: python class
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()