Jak jsem schopen opravit atribut chyby při použití třídy

0

Otázka

Napsat třídu s názvem "Osoba" s atributy dat pro osobu, jméno, adresu a telefonní číslo.

class Person:
    def __init__(self, n, a, p):
        self.name = n
        self.address = a
        self.phone = p
    
    def set_name(self, n):
        self.name = n
    
    def set_address(self, a):
        self.address = a
    
    def set_phone(self, p):
        self.phone = p
    
    def get_name(self):
        return 'Customer Name:'
    
    def get_address(self):
        return 'Customer Address:'
    
    def get_phone(self):
        return 'Customer Phone Number:'
    
def main():
    n = input('Enter Customer Name: ')
    a = input('Enter the Customer Address: ')
    p = input('Enter the Customer Phone Number: ')
    
    print(n.get_person())
    print(a.get_address())
    print(p.get_phone())
main()

Jak jsem schopen opravit svůj atribut chyba?

attributeerror class python python-3.x
2021-11-22 03:27:59
2
0

První, své chyby, opravdu by měla být TypeError,, protože se volá metoda instance ze třídy:

>>> class Foo:
...     def __init__(self):
...         self.bar = 1
...     def thing(self):
...         return self.bar
...
>>>
>>>
>>> Foo.thing()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: thing() missing 1 required positional argument: 'self'

Nicméně, opravit je stejný bez ohledu na to.

Musíte vytvořit instanci třídy, aby přístup k jeho atributy:

class Foo:
    def __init__(self):
        self.bar = 1

# Foo is a class, but it doesn't have a 
# bar attribute. An *instance* of the class
# does

# this raises an attributeError
Foo.bar
AttributeError

# this doesn't, because we created an instance
foo = Foo()
foo.bar
1

Musíte vytvořit člověk stupně, pak se můžete dostat své atributy:

class Person:
    def __init__(self, name, address, phone):
        self.name = name
        self.address = address
        self.phone = phone


Person.phone 
# AttributeError

# Create an instance of Person here
tim = Person(name='Tim', address='1234 Main St', phone='555-5555')

# Then you can get Tim's information
tim.phone
'555-5555'
2021-11-22 03:47:20
0

Musíte vytvořit instanci třídy Osoba pomocí něčeho jako person = Person(n, a, p). Udělej to po zadání příkazů z vašeho main() funkce.

Za druhé, při tisku z atributy, které potřebujete k odkazu na instanci třídy Osoba, kterou jste vytvořili (tj. osoba.get_person(), person.get_address(), atd.).

Konečně, při volání funkce, ujistěte se, že tyto funkce vracejí hodnotu, kterou hledáte. Funkce get_address() by se měla vrátit já.adresa.

Tady je můj návrh:

class Person:
    def __init__(self, n, a, p):
        self.name = n
        self.address = a
        self.phone = p
    
    def set_name(self, n):
        self.name = n
    
    def set_address(self, a):
        self.address = a
    
    def set_phone(self, p):
        self.phone = p
    
    def get_name(self):
        return self.name
    
    def get_address(self):
        return self.address
    
    def get_phone(self):
        return self.phone

def main():
    n = input('Enter Customer Name: ')
    a = input('Enter the Customer Address: ')
    p = input('Enter the Customer Phone Number: ')
    
    person = Person(n, a, p) # create the instance

    # get attributes of instantiated object
    print(person.get_person())
    print(person.get_address())
    print(person.get_phone())

main()
2021-11-22 05:53:22

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................