How to return new instance of subclass while initializing parent class?
Tag : ruby , By : xie renhui
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You're breaking a fundamental OO principle here -- classes should know nothing about their subclasses. Of course, sometimes principles should be broken, but there's no apparent reason to do it here. A far better solution is to shift the instantiation logic to a factory method in a separate class. The factory method takes the same arguments as the A's initializer above, and returns an instance of the appropriate class.
|
Python subclass not recognizing inherited parent class
Tag : python , By : Jason Haar
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , I am struggling with my code, specifically subclasses. I have a parent class that when initialized will call its subclass to be used as an attribute, in the form of a list of objects. I have seen numerous post where a person forgets to call the __init__() of the parent. My issue is different in that the parent is calling the child class and I do not want to call it. , When I run your code, I got the error in the following line: class subbundle(bundle):
class bundle(object):
def __init__(self, size,nzone):
self.size = size
[ . . .]
class subbundle(bundle):
""" Defines geometry to represent subbundle"""
def __init__(self, row, col, rows,cols):
[ . . . ]
for i in range (1,self.size):
subbundle_instance = subbundle(r, c, self.rows, self.cols)
subchans.append(subbundle_instance)
|
Are subclass properties preserved if I assign a subclass instance to a property typed as a parent class?
Date : March 29 2020, 07:55 AM
hop of those help? If you intend to use methods that are defined in the Status class but not PhotoStatus, you need to check the class of the returned object. To do this, you would normally do something along these lines: Status *status = viewCommentsViewController.status;
if ([[status class] isSubclassOfClass: [PhotoStatus class]])
{
PhotoStatus *photoStatus = (PhotoStatus *) status;
// Handle photoStatus.
}
else
{
// Handle the other case.
}
@implementation Status
- (PhotoStatus *) asPhotoStatus
{
return nil;
}
@end
@implementation PhotoStatus
- (PhotoStatus *) asPhotoStatus
{
return self;
}
@end
[[viewCommentsViewController.status asPhotoStatus] doSomething];
|
Python class inheritance: dynamic subclass initialization with own and parent defaults and init methods
Date : March 29 2020, 07:55 AM
hop of those help? You can use the new classmethod added to object() in Python 3.6: __init_subclass__This method is called when the class is subclassed and allows you to customize the subclasses' creation without using a metaclass. class Base:
def __init_subclass__(cls, **kwargs):
super().__init_subclass__()
for key, value in kwargs.items():
setattr(cls, key, value)
def __init__(self, *args, **kwargs):
super().__init__()
for key, value in kwargs.items():
setattr(self, key, value)
class Parent(Base, p_var_1='my_name', p_var_2=2):
pass
class Gen1(Parent, G1_var_1='Generation 1', G1_var_2=[]):
pass
class Gen2(Gen1, G2_var_1=1337, G2_var_2=(10,4)):
pass
|
Creating a new instance of an inherited class through the inherited static methods
Date : March 29 2020, 07:55 AM
I hope this helps you . I've got a method in my class to create a new 'self' on execution. , Use return new static($id); instead of return new self($id);
|