假设我有家长班P
class P(object):
def __init__(self, a, b, c):
self._a = a
self._b = b
self._c = c
如果创建另一个类P(它是P的子类),则可以继承P的某些参数,但不是全部(假设我只是希望将P类的参数a,c传递给C)。我知道这是一个很奇怪的问题,而且我不知道是否有针对此的应用程序,但是我似乎找不到答案。预先谢谢你!
答案 0 :(得分:1)
据我所知,唯一的好方法是执行以下操作:
db = DatabaseAccess.getInstance(this);
db.open();
ArrayList<Product> products = db.getMiavit();
final String[] suggestions = new String[products.size()];
for (int i = 0; i < products.size(); i++) {
suggestions[i] = products.get(i).getPRODUCTS();
}
searchView = (MaterialSearchView) findViewById(R.id.search_view);
searchView.setVoiceSearch(false);
searchView.setCursorDrawable(R.drawable.custom_cursor);
searchView.setSuggestions(suggestions);
searchView.setEllipsize(true);
db.close();
本质上,您调用超类构造函数来定义所有值,但是稍后在子类构造函数中覆盖该值(在这种情况下,对于
class P:
def __init__(self, a, b, c):
self._a = a
self._b = b
self._c = c
class C(P):
def __init__(self, a, b, c):
super(C, self).__init__(a, b, c)
self._b = b
来说是这样)。
或者,如果您不希望self._b
成为类self._b
的东西,也可以在C
调用后执行del self._b
来将其删除完全。
但是,通常来说,让子类不拥有父类具有的某些字段似乎是一种不好的做法,因为代码的其他部分可能依赖于那里的该字段。