“Método de classe em Python” Respostas de código

Como fazer uma aula em 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

Classe Python

class Person:#set name of class to call it 
  def __init__(self, name, age):#func set ver
    self.name = name#set name
    self.age = age#set age
   

    def myfunc(self):#func inside of class 
      print("Hello my name is " + self.name)# code that the func dose

p1 = Person("barry", 50)# setting a ver fo rthe class 
p1.myfunc() #call the func and whitch ver you want it to be with 
rej

Classe Python

# Standard way of writing a simple class
class Person1:
# Type hinting not required
    def __init__(self, name: str, age: int, num_children=0):
        self.name = name
        self.age = age
        self.num_children = num_children

    def __repr__(self):
        return f'My name is {self.name}, I am {self.age} years old, and I have {self.num_children} children'

      
from dataclasses import dataclass


# A class using data classes. Dataclasses are simpler but can't support operations during initialization
@dataclass()
class Person2:
    """ This class handles the values related to a person. """
    name: str  # Indicating types is required
    age: int
    num_children = 0  # Default values don't require an indication of a type

    def __repr__(self):
        return f'My name is {self.name}, I am {self.age} years old, and I have {self.num_children} children'

# Both classes (Person1 and Person2) achieve the same thing but require different code to do it

person1 = Person1('Joe', 28, 2)
print(person1)
# Result: My name is Joe, I am 28 years old, and I have 2 children

person2 = Person2('Emma', 19)
print(person2)
# Result: My name is Emma, I am 19 years old, and I have 0 children
YEP Python

Classe Python

class Human():
    def __init__(self, _name, _age):
        self.name = _name
        self.age = _age

    def walk(self):
        print("walking...")

Person = Human('John', 32)
Person.walk()
The Cat Coder

Classe 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

Método de classe em Python

# Python program to demonstrate
# use of a class method and static method.
from datetime import date
  
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
  
    # a class method to create a
    # Person object by birth year.
    @classmethod
    def fromBirthYear(cls, name, year):
        return cls(name, date.today().year - year)
  
    # a static method to check if a
    # Person is adult or not.
    @staticmethod
    def isAdult(age):
        return age > 18
  
person1 = Person('mayank', 21)
person2 = Person.fromBirthYear('mayank', 1996)
  
print(person1.age)
print(person2.age)
  
# print the result
print(Person.isAdult(22))
Someone

Respostas semelhantes a “Método de classe em Python”

Perguntas semelhantes a “Método de classe em Python”

Mais respostas relacionadas para “Método de classe em Python” em Python

Procure respostas de código populares por idioma

Procurar outros idiomas de código