简体   繁体   中英

Is it possible to create a loop inside a child class?

I'm trying to see if it's possible to create a loop in a child class with for loop ?

Anyway, here's what I made and trying to figure out how to achieve this (line 22-23) but unfortunately can't find a way. I'm new to python btw and exploring things

class Valkyrie:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

class Character(Valkyrie):
        def __init__(self, fname, lname, age):
            super().__init__(fname, lname)
            self.edad = age         

        def intro(self):
            print(self.firstname, self.lastname, self.edad)

        def introd(self):
            print(self.firstname, self.lastname,",", self.edad)    
            

kk = Character("Kiana", "Kaslana", 16)
for x in kk:
    print(x) 

rm = Character("Mei", "Raiden", 18)
bz = Character("Bronya", "Zaychick", 14)
kk.intro()               
kk.introd()
rm.intro()
bz.intro() 

I don't understand your example but summarizing, yes, you can create a loop inside a child class.

Let's consider this example:

class Parent_class:
    def __init_(self,name,last_name,age):
        self.name = name
        self.last_name = last_name
        selg.age = age
    def print_name(self):
        print('The name is ',self.name,' ',self.last_name,'\n')


class Child_class(list(PARENT_CLASSES)):
    def __init__(self, name, last_name, age):
        super().__init__(name, last_name, age)   
    def introduce(self):
        for person in PARENT_CLASSES:
            person.print_name()

If you pass a list of PARENT_CLASSES to the child class and, then, invoke its introduce function, it will give you the details of every person listed in PARENT_CLASSES

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM