Questions tagged [class]

A template for declaring a type of object.

Filter by
Sorted by
Tagged with
3
votes
4answers
101 views

Structure when I need multiple “copy” instances of a common base class?

So I'm building a card game where each card has cost, damage, and durability, as well as name and description. Obviously one would think of creating a Card class: class Card { int cost int ...
2
votes
2answers
85 views

Creating an Instance of a Complicated Class

How do I create instances of classes which require a huge amount of components and attributes? Take for example a car. A car has hundreds of sub components each with their own specific properties. ...
11
votes
5answers
958 views

What is a good design practice to avoid asking a subclass type?

I've read that when your program needs to know what class an object is, usually indicates a design flaw so I want to know what is a good practice to handle this. I'm implementing a class Shape with ...
4
votes
5answers
505 views

Should I encapsulate an object inside another object as methods or just access it directly?

Suppose I have a class A: public class A{ public void a(){ } } and class B use A: public class B{ private A a; } Should I encapsulate A in B: public class B{ private A a; ...
1
vote
0answers
122 views

Should all classes and interfaces ultimately derive from one Class, and how does this affect type safety?

If I have "Object" as the top level class, then I have various interfaces, ISomething, ISomethingB, ISomethingC, then I have Class x,y,z, all ultimately deriving from the class Object (these ...
2
votes
2answers
107 views

Keep consistency when items can be in several classes

I'm having a difficulty to figure out relating an item to classes properly. For example, I have a business directory website that shows profiles of different companies according to the products/...
-1
votes
1answer
125 views

How to quickly work on a single class of a big GUI C# project?

So here's the problem. I have a massive C# project in Visual Studio. It has a big GUI that manages a large number of classes. In one of the form windows, there is a bunch of textboxes. The user fills ...
-2
votes
1answer
221 views

How does accessing class level variables in java work?

I'm doing my third online course in learning java because I couldn't get over how to even start a project from scratch. There was a lesson in this new course that made me ask this question of the code ...
0
votes
2answers
114 views

Using a class for a collection element, with methods to access other collections

I'm hoping for a sanity check in my design thinking. I'm working with a small team on a website based on a MongoDB database. There are several collections in the DB -- for example, one representing ...
-1
votes
1answer
131 views

Memory override using classes when using lists in python

I am trying to create a new variable with class Lamb (called hold) using a variable (main), which also has class Lamb. Lamb has two parameters (x and y). I create a variable called main with class ...
1
vote
0answers
4k views

ASP.NET: Creating an interface for a controller

Would creating a controller's interface a bad idea? I have a controller that derived from ApiController, so knowing that you can only derived one class to a class but able to derived one or more ...
0
votes
1answer
178 views

Emulating classes in Javascript

Lets take the following example of inheritance in javascript: var Employee = function(name, salary) { Person.call(this, name); this.name = name; this.salary = salary; } Employee.prototype = ...
17
votes
1answer
36k views

Classes vs. modules in Python

Python has many modules (such as re) that perform a specific set of actions. You can call the functions of this module and get results, and the module as a whole has an idea behind it (in this case, ...
1
vote
1answer
454 views

Class vs Module - When classes are needed to be in module?

I am developing an application that has a lot of options like local database CRUD operations, file IO, web APIs calling, notifications, user preference settings, services, widgets etc. At start it ...
1
vote
1answer
431 views

Creating widgets, setting/getting data from them - Pattern problem

Lets say I have a Widget class. I also have TextWidget, ComboWidget, ChoiceWidget classes that inherited from Widget class. I create this widgets based on the situation. After creating the widget, I ...
2
votes
2answers
101 views

Modifying an existing class: should I extend it to a new class?

I'm working with a library (from an online source, not authored in-house) providing an interface as well as an implementation of it: interface FooInterface { // ... } class Foo implements ...
0
votes
3answers
530 views

When NOT to use a class / member variable?

I am trying to learn WHEN NOT to use: classes member variables HERE IS THE CODE access_point_detection_classes.py from scapy.all import * class Handler : def __init__(self) : self....
0
votes
1answer
124 views

Does passing this as argument potentially cause exposure of invalid object state?

This question is based on two premises: First: an object must always have valid state. It is discussed in various posts, too. https://stackoverflow.com/questions/22408804/should-a-c-object-always-be-...
0
votes
6answers
646 views

why can't class directly take arguments instead of using a constructor?

I referred this page for understanding about constructors in Java: http://www.javatpoint.com/constructor So, I pretty much got it as in my own words: constructor has no return value and is used to ...
0
votes
2answers
193 views

Which pattern to use when I want to encapsulate large blocks of code?

I have a data structure, a large dictionary, that is mutated by several functions. Each function does a database call, some calculations and then changes the value of a key in the data structure. Each ...
11
votes
3answers
5k views

How should an `Employee` class be designed?

I am trying to create a program for managing employees. I cannot, however, figure out how to design the Employee class. My goal is to be able to create and manipulate employee data on the database ...
1
vote
1answer
112 views

Removing constraints of Java array

Java arrays only allow subscripts to range between 0 and N-1 for an array of size N. The class below aims to remove that constraint by allowing the class user to specify the valid subscripts ...
4
votes
3answers
3k views

Why instantiate and initialize an object without saving it into a variable?

Sorry if I got the wording wrong in my title, but it would be easiest to provide an example in JavaFX primaryStage.setScene(new Scene(grid, 300, 275)); Now from what I understand, this code is ...
10
votes
3answers
556 views

What does it mean to inject data (vs behaviour) in a class constructor, and why is that considered bad practice?

I am reading the book "Learning TypeScript" by Remo Jansen. In one section the author describes how to create a very simple proof-of-concept MVC framework including how to create the Model class and ...
3
votes
3answers
6k views

Define C++ class in one or more files

When creating a C++ class what is best practice> Put the entire class definition and member functions in a header file Put the class definition and function declarations in the header file and put ...
5
votes
1answer
10k views

Dictionary of dictionaries design in C#

My question: is there a canonical way of creating a dictionary of dictionaries and providing an outer/inner key pair? Is there a NugetPackage out there with an implementation? In my code, I have now ...
0
votes
3answers
103 views

What do you call the two types of classes on a instance declaration

When you declare a new instance of a class, you do so in a lot of languages as: MyClass obj = new MyClass(); The two MyClass mean subtly different things, often you will see: IMyClass obj = new ...
0
votes
1answer
116 views

Fundamental difference between a static class and namespaced functions

I have been using "static" classes as a method to group functions with relating purposes under a common name that provides readability and maintainability to the code at the cost of performance and ...
2
votes
1answer
3k views

Using ninject in a class library

Looking for some help getting my head around ninject and DI. Using the simple examples I've found online everything works nicely but trying to do something more complex is causing headaches. I have ...
3
votes
1answer
10k views

Is calling the superclass constructor in a subclass really important?

The following piece of Python code uses a superclass solely as the repository of functions that one of more subclasses may draw from: class Class(object): ''' A trivial repository for functions ...
-1
votes
1answer
165 views

What is the faster way to save and browse objects using vectors +/or maps? (c++)

I'm doing a object Garbage class using c++: #include "Object.h" class ObjectGarbage { public: ObjectGarbage(); ~ObjectGarbage(); void Call_All_Objects() { //(...
0
votes
1answer
506 views

How to call an unknown member function through an instance of a related class?

I have two classes. The first, called Game_Events, controls the objects and manages the general tasks. The second, called Button, is for one of those object instances. The example below is wrong but ...
1
vote
2answers
56 views

Are Data defined into derived class saved when up-casting?

Let's say i have two class named Object (base class) and an actor class (derived class). The actor class uses physics so i need to implement it on the Actor class. But there are other derived class of ...
12
votes
5answers
5k views

Inheritance vs additional property with null value

For classes with optional fields, is it better to use inheritance or a nullable property? Consider this example: class Book { private String name; } class BookWithColor extends Book { private ...
7
votes
4answers
271 views

When in your language classes are objects too, does the Liskov substitution principle apply to their interfaces?

According to Wikipedia the Liskov substitution principle states that objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program ...
2
votes
3answers
274 views

Adding a function to class member

I'm not sure of the proper terminology here so I'm having trouble finding the answer to my question but here is what I want to achieve. say I have class ABC { public: int number; }; and I want ...
5
votes
8answers
1k views

Rule of thumb for deciding which class a method belongs to

For example, imagine a website which stores results about a certain sport or game, which has a typical "season" structure, such that there's both a Player and a Season class. To retrieve a player's ...
4
votes
1answer
558 views

Avoiding tightly coupled class definitions in Python for has-a relationships

I have the following code: class Car(object): def __init__(self, my_id): self.my_id = my_id self.color = color self.brand = brand self.get_color() self....
9
votes
4answers
1k views

How to argue against this “completely public” mindset of business object class design

We're doing a lot of unit testing and refactoring of our business objects, and I seem to have very different opinions on class design than other peers. An example class that I am not a fan of: ...
1
vote
1answer
593 views

Manual reload of Java classes static block

I have a static block which fetches data from database. This data is then being used by instance method. Since static block will be loaded once I fear that the data won't change in its lifetime. How ...
1
vote
1answer
123 views

Is this the right way to use classes on this project?

Before any down voting or blaming, please bear in mind that this is my first "serious" Python project. Before this I have only coded a program that uses several Google API's to do stuff, and not much ...
2
votes
1answer
716 views

Function pointers vs. Derived classes

So I have a Signal class and a client class which manages a list of Signal objects. The class provides a bunch of interfaces, one of them is an update()-function. The update()-code is completely ...
7
votes
2answers
691 views

C++ class design with invariant

I've been pondering a really basic question about how far to take enforcing a class's invariant. Maybe that's worded badly, so as an example, let's say that I want to write a class which stores a ...
6
votes
2answers
259 views

Idiomatic way to write JavaScript class that maintains state and tells you when that state has changed

I am an experienced C++ developer but new to JavaScript. I want to write an ES6 JavaScript class that maintains state. How do I tell when state has changed? I can think of two ways to do this. One ...
2
votes
2answers
1k views

Where is the class itself stored at runtime to be used as a reference

In C# or Java we find that the objects are stored on heap and their reference vars are stored on stack. But at run time where is the class definition stored to be used as a template for creating ...
3
votes
6answers
766 views

If Class is to define attributes and methods, and Interface is to define (a set of) methods, then how to think of interface needing new attributes?

I thought a class is supposed to define, or give a blueprint, of attributes and methods for an object. And then, an interface is to provide a set of methods, as a contract for its clients. (and so a ...
2
votes
2answers
832 views

python - differences between reusable code vs. code for solving specific tasks

Reusable code (ex. libraries and frameworks) and code written to solve a specific task and not meant to be reused as a general tool (for example, code being used only by my 6 person team in a private ...
6
votes
4answers
978 views

Class design dilemma: Encapsulation vs Single Responsibility/Separation of Concerns

I'm working on a class that represents an object with multiple representations - one is an XML type representation used by an automatic ordering system, the other is a POJO-based representation used ...
0
votes
3answers
39k views

Using super keyword to access super class members

I have following questions about the super keyword in java: I have a code below with classes: A Extends no class contains integer variable i B Extends class A contains integer variable i C Extends ...
-1
votes
3answers
163 views

Python — Class and Object [closed]

I got a question that quite disturbs me a lot and I think it might help a lot if I had an answer to it. So I got this: class Klasse1: variable1 = "haha" class Klasse2: variable2 = "hoho" ...