4,171 questions
0
votes
1
answer
72
views
How does super() initialize properties on a subclass instance in JavaScript?
I’m learning Object-Oriented Programming in JavaScript and got confused by the following code:
class Animal {
constructor(lg) {
this.legs = lg;
}
}
class Dog extends Animal {
constructor() {...
0
votes
1
answer
106
views
Subclass of Generic fails with AttributeError: object has no attribute '__parameters__' - using Generic when superclass does forward __init_subclass__
I have a setup like the following
from typing import Generic, TypeVar
T = TypeVar("T")
class ThirdParty:
def __init_subclass__(cls):
... # does not call super()
class Mine(...
0
votes
0
answers
15
views
How can I subclass a JComobBox and handle events there but, have access to the top level class?
How can I move then event handling into the subclass of a JComboBox? I can make it work if everything is in TopLevel but, I want to move the event handling (actionPerformed(ActionEvent e)) into a ...
2
votes
1
answer
181
views
Is it possible to limit attributes in a Python sub class using __slots__?
One use of __slots__ in Python is to disallow new attributes:
class Thing:
__slots__ = 'a', 'b'
thing = Thing()
thing.c = 'hello' # error
However, this doesn’t work if a class inherits from ...
-2
votes
2
answers
93
views
How do I call the method from a class that creates other objects? [closed]
I have five classes. My Main class, a form class, two subclasses that extend the form class and a createForm class I am forced to use to create new instances of the subclass.
public class FormenFabrik ...
0
votes
0
answers
31
views
Constructor and datatype not matching in java [duplicate]
I'm relatively new to Java and object oriented programming, and I don't understand why sometimes the constructor used in declaring and object will be a subclass of the objects type instead of the ...
1
vote
2
answers
110
views
How do you extend a superclass property object in a subclass in JavaScript and get TypeScript to not complain?
Given this code:
class Foo {
data = { x: 1 };
}
class Bar extends Foo {
override data = { ...super.data, y: 1 };
}
TypeScript complains on the super.data part:
Class field 'data' defined by the ...
1
vote
1
answer
70
views
Subclass that throws custom error if modified
What's the best way in python to create a subclass of an existing class, in such a way that a custom error is raised whenever you attempt to modify the object?
The code below shows what I want.
class ...
1
vote
0
answers
80
views
How to tell Python which constructor to use to instantiate subclasses? [duplicate]
I am in the following situation: I have a type A, two subtypes B and C of A, a function f of type A -> A that is such that
for any B object x, f(x) is of type B;
for any C object x, f(x) is of ...
0
votes
1
answer
29
views
what is subclass tf.keras.layers.Layer instead of using a Lambda in this code?
An example of replacing a lambda with a subclass in code is as follows:
scale = tf.Variable(1.)
scale_layer = tf.keras.layers.Lambda(lambda x: x * scale)
Because scale_layer does not directly track ...
1
vote
1
answer
114
views
Error with custom exception in C++ module (conflicting definition of __cxa_throw)
I get the following error when creating a custom exception in a C++ module:
C:/Sync/Engine/Chorus/Base/win_9x.cpp:600:59: error: conflicting declaration of C function 'void __cxa_throw(void*, void*, ...
0
votes
1
answer
144
views
How to randomize ship positions in a Battleships game?
I'm writing a battleships game for my college project and I want to randomize the positions of the ships in the beginning of the game. My program has an abstract superclass of Ship which branches out ...
1
vote
2
answers
89
views
Is there an elegant way to subclass a Python list which keeps its subclass when adding and slicing, etc
I want to extend the functionality of a standard list, subclassing seems like the obvious way. Actually, I'm using this to store a mathematical expression in Reverse Polish Notation (RPN) for a ...
2
votes
2
answers
264
views
In Java, avoiding null dereferences when calling super(...) in constructor
I am using Java 21.
I have two classes:
abstract class MySuperClass {
private final Object mySuperField;
MySuperClass(Object myField) {
this.mySuperField = myField;
}
public ...
-1
votes
2
answers
55
views
Error while building a program which should output assigned domino tiles for each player in Java
So, I am a total beginner in Java. I need a program that takes a number of players as an input. Then assuming there are 91 domino tiles each with two squares with dots ranging from 0 to 12 in each ...