#!/usr/bin/python
# -*- coding: UTF-8 -*-
class Animal:
age = 0
weight = 0
def __init__(self, species, age, weight):
self.species = species
Animal.age = age
Animal.weight = weight
def displayAge(self):
print(self.species + "' age: " + str(Animal.age))
def displayWeight(self):
print(self.species + "' weight: " + str(Animal.weight))
def self_test(self):
print("Hello: " + str(self))
def cls_test(cls):
print("Hello: " + str(cls))
horse = Animal("horse", 10, 100)
horse.displayAge()
horse.displayWeight()
horse.self_test()
horse.cls_test()
#Animal.self_test() #TypeError: self_test() missing 1 required positional argument: 'self'
#Animal.cls_test() #TypeError: cls_test() missing 1 required positional argument: 'cls'
Animal.self_test(horse)
Animal.cls_test(Animal)
/home/zhenglf/PycharmProjects/TestPython/venv/bin/python /home/zhenglf/PycharmProjects/TestPython/Animal.py
horse' age: 10
horse' weight: 100
Hello: <__main__.Animal object at 0x7f3ed74cc908>
Hello: <__main__.Animal object at 0x7f3ed74cc908>
Hello: <__main__.Animal object at 0x7f3ed74cc908>
Hello: <class '__main__.Animal'>
Process finished with exit code 0