01/10/2018, 12:14

Tính kế thừa của Python sai ở đâu?

Mọi người cho mình hỏi mình sai chỗ nào ạ? Mình dùng python2

class Cha:
	def __init__(self, ten, tuoi):
		self.ten = ten
		self.tuoi = tuoi
	def daihoc(self):
		pass

class Con(Cha):
	def __init__(self,ten, tuoi, daihoc):
		Super().__init__(ten, tuoi)
		self.daihoc = daihoc

con2 = Con('Tien',22 , 'BK')
print(con2.ten)
Henry viết 14:16 ngày 01/10/2018

Mình không rành Python cho lắm, nhưng mình thấy có 3 cái chưa đúng. Thứ nhất, hàm super bạn phải cho nó ít nhất một argument. Trong khi bạn không đưa cái nào.
Thứ 2, super not Super
Phải sửa thành

super(Cha, self).__init__(ten, tuoi)
# Cha ở đây để giúp super biết, gọi hàm __init__ của lớp kế thừa nào (vì Python có thể multi kế thừa.
# self ở đây truyền vào sẽ là đối tượng thuộc lớp Con, để vào đó self con được gắn  thay vì self cha.

Tiếp cái thứ 3
Trong Python 3.X thì không phân biệt giữa

class A:
    pass

class A(object):
    pass

Nhưng trong Python 2.X, cái đầu tiên được gọi là old style class. Thêm (object) gọi là new style class. Và hàm super chỉ chịu nhận một cái new style class.
Suy ra bạn phải sửa class Cha: thành class Cha(object): và tuyệt đối không được class Cha():
Nhưng mà, mình thấy cách này nó tù lắm, nên chả mấy khi dùng.

class Cha(object): # ở đây, có object hay không cũng được, vì ở dưới không dùng hàm super.
  def __init__(self, ten, tuoi):
    self.ten = ten
    self.tuoi = tuoi
  def daihoc(self):
    pass

class Con(Cha):
  def __init__(self,ten, tuoi, daihoc):
    Cha.__init__(self, ten, tuoi)
    self.daihoc = daihoc

con2 = Con('Tien',22 , 'BK')
print con2.ten

Mình có link cho bạn tham khảo thêm:

stackoverflow.com
CiaranWelsh

Example using 'super' In Python2.7

python, inheritance, super
asked by CiaranWelsh on 12:22PM - 06 Sep 15

stackoverflow.com
Geo

Python super() raises TypeError

python, inheritance, python-2.x, super
asked by Geo on 08:47PM - 28 Jan 09

Tiến Nguyễn viết 14:16 ngày 01/10/2018

Cảm ơn bạn nhiều nhé, Mình hiểu được rồi ^^

Tiến Nguyễn viết 14:24 ngày 01/10/2018

bạn có thể xem dùm mình thêm đoạn code này sai ở đâu nữa không?

Tiến Nguyễn viết 14:31 ngày 01/10/2018

from abc import ABC, abstractmethod

class Mayin():
# @abstractmethod
# “”“docstring for Mayin”""
def init(self, ID, name, thuonghieu, giathanh):
self.__ID = ID
self.__name = name
self.__thuonghieu = thuonghieu
self.__giathanh = giathanh
# @abstractmethod
def display(self):
print “ID: %r” %self.__ID
print “name: %r” %self.__name
print “thuong hieu: %r” %self.__thuonghieu
print “gia thanh: %r” %self.__giathanh

class MayinKim(Mayin):
“”“docstring for MayinKim”""
def init(self, id, name, thuonghieu, giathanh, sokim):
Mayin.init(ID, name, thuonghieu, giathanh)
self.__sokim = sokim
def display(self):
Mayin.display()
print “so kim: %r” %self.__sokim

class MayinMuc(Mayin):
def init(self, ID, name, thuonghieu, giathanh, loaimuc):
Mayin.init(self, ID, name, thuonghieu, giathanh)
self.__loaimuc = loaimuc
def display(self):
Mayin.display()
print “loai muc: %r” %self.__loaimuc

may1 =MayinMuc(‘1’, ‘print’, ‘Sony’, ‘2000$’, ‘Black’)
may1.display()

Henry viết 14:15 ngày 01/10/2018

Bạn nên nắm rõ con trỏ self.
Bản chất của nó cũng là một argument thôi.
Đây là code mình sửa, bạn tham khảo.

class Mayin:
  def __init__(self, ID, name, thuonghieu, giathanh):
    self.__ID = ID
    self.__name = name
    self.__thuonghieu = thuonghieu
    self.__giathanh = giathanh
  def display(self):
    print "ID: %r" %self.__ID
    print "name: %r" %self.__name
    print "thuong hieu: %r" %self.__thuonghieu
    print "gia thanh: %r" %self.__giathanh

class MayinKim(Mayin):
  def init(self, id, name, thuonghieu, giathanh, sokim):
    Mayin.__init__(self, ID, name, thuonghieu, giathanh)
    self.__sokim = sokim
  def display(self):
    Mayin.display(self)
    print "so kim: %r" %self.__sokim

class MayinMuc(Mayin):
  def __init__(self, ID, name, thuonghieu, giathanh, loaimuc):
    Mayin.__init__(self, ID, name, thuonghieu, giathanh)
    self.__loaimuc = loaimuc
  def display(self):
    Mayin.display(self)
    print "loai muc: %r" %self.__loaimuc

may1 =MayinMuc('1', 'print', 'Sony', '2000$', 'Black')
may1.display()
Bài liên quan
0