subclassing

What to consider before subclassing list?

这一生的挚爱 提交于 2019-11-26 16:59:01
问题 I was recently going over a coding problem I was having and someone looking at the code said that subclassing list was bad (my problem was unrelated to that class). He said that you shouldn't do it and that it came with a bunch of bad side effects. Is this true? I'm asking if list is generally bad to subclass and if so, what are the reasons. Alternately, what should I consider before subclassing list in Python? 回答1: There are no benefits to subclassing list . None of the methods will use any

Why do all backgrounds disappear on UITableViewCell select?

末鹿安然 提交于 2019-11-26 15:45:24
问题 My current project's UITableViewCell behavior is baffling me. I have a fairly straightforward subclass of UITableViewCell. It adds a few extra elements to the base view (via [self.contentView addSubview:...] and sets background colors on the elements to have them look like black and grey rectangular boxes. Because the background of the entire table has this concrete-like texture image, each cell's background needs to be transparent, even when selected, but in that case it should darken a bit.

If you overwrite a field in a subclass of a class, the subclass has two fields with the same name(and different type)?

好久不见. 提交于 2019-11-26 14:28:36
I have 3 classes: public class Alpha { public Number number; } public class Beta extends Alpha { public String number; } public class Gama extends Beta { public int number; } Why does the following code compile? And, why does the test pass without any runtime errors? @Test public void test() { final Beta a = new Gama(); a.number = "its a string"; ((Alpha) a).number = 13; ((Gama) a).number = 42; assertEquals("its a string", a.number); assertEquals(13, ((Alpha) a).number); assertEquals(42, ((Gama) a).number); } Member variables cannot be overridden like methods. The number variables in your

How to subclass pandas DataFrame?

老子叫甜甜 提交于 2019-11-26 12:55:31
问题 Subclassing pandas classes seems a common need but I could not find references on the subject. (It seems that pandas developers are still working on it: https://github.com/pydata/pandas/issues/60). There are some SO threads on the subject, but I am hoping that someone here can provide a more systematic account on currently the best way to subclass pandas.DataFrame that satisfies two, I think, general requirements: import numpy as np import pandas as pd class MyDF(pd.DataFrame): # how to

Why can't you reduce the visibility of a method in a Java subclass?

喜你入骨 提交于 2019-11-26 06:45:01
问题 Why does the compiler give an error message when you reduce the visibility of a method while overriding it in the subclass? 回答1: Because every instance of the subclass still needs to be a valid instance of the base class (see Liskov substitution principle). If the subclass suddenly has lost one property of the base class (namely a public method for example) then it would no longer be a valid substitute for the base class. 回答2: Because if this was allowed, the following situation would be

If you overwrite a field in a subclass of a class, the subclass has two fields with the same name(and different type)?

烂漫一生 提交于 2019-11-26 05:56:28
问题 I have 3 classes: public class Alpha { public Number number; } public class Beta extends Alpha { public String number; } public class Gama extends Beta { public int number; } Why does the following code compile? And, why does the test pass without any runtime errors? @Test public void test() { final Beta a = new Gama(); a.number = \"its a string\"; ((Alpha) a).number = 13; ((Gama) a).number = 42; assertEquals(\"its a string\", a.number); assertEquals(13, ((Alpha) a).number); assertEquals(42,

Protocol func returning Self

时光毁灭记忆、已成空白 提交于 2019-11-25 23:44:10
问题 I have a protocol P that returns a copy of the object: protocol P { func copy() -> Self } and a class C that implements P: class C : P { func copy() -> Self { return C() } } However, whether I put the return value as Self I get the following error: Cannot convert return expression of type \'C\' to return type \'Self\' I also tried returning C . class C : P { func copy() -> C { return C() } } That resulted in the following error: Method \'copy()\' in non-final class \'C\' must return Self to