xxxxxxxxxx
# Example of defining a class in Python
class MyClass:
def __init__(self, attribute):
self.attribute = attribute
def my_method(self):
print("Hello, world!")
# Creating an instance of the class
my_object = MyClass("example")
# Accessing the attribute and calling the method
print(my_object.attribute)
my_object.my_method()
xxxxxxxxxx
A class is a block of code that holds various functions. Because they
are located inside a class they are named methods but mean the same
thing. In addition variables that are stored inside a class are named
attributes. The point of a class is to call the class later allowing you
to access as many functions or (methods) as you would like with the same
class name. These methods are grouped together under one class name due
to them working in association with eachother in some way.
xxxxxxxxxx
class Planet:
def __init__(self, name, radius, gravity, system):
self.name = name
self.radius = radius
self.gravity = gravity
self.system = system
def orbit(self):
return f'{self.name} is orbiting in the {self.system}'
alphax123 = Planet('alphax123', 300000, 8, 'Naboo System')
print(f'Name: {alphax123.name}')
print(f'Radius: {alphax123.radius}')
print(f'Gravity: {alphax123.gravity}')
print(f'System: {alphax123.orbit}')
xxxxxxxxxx
# class
class Enum:
Tim, Bill, Joe = range(1,4)
print(Enum.Tim == 1) # return True
xxxxxxxxxx
class Person:
def __init__(self, name, sex, profession):
# data members (instance variables)
self.name = name
self.sex = sex
self.profession = profession
# Behavior (instance methods)
def show(self):
print('Name:', self.name, 'Sex:', self.sex, 'Profession:', self.profession)
# Behavior (instance methods)
def work(self):
print(self.name, 'working as a', self.profession)
xxxxxxxxxx
<div class="alert" role="alert">Warning! I'm missing something</div>
xxxxxxxxxx
/* Any Element With Class Title */
.title {
}
/* Any element with id nav */
#nav {
}
/* any div element */
div {
}
/* any element h2 */
h2 {
}