Questions tagged [oop]

23292 questions
1

votes
0

answer
88

Views

C#, Get class properties

I have the following class structure: class structure The user can select to work with SystemA or SystemB. The following code is from the class that creates the main object: public class MainViewModel() { var project = new Project(); project.PropAList = new List(); project.PropAList.Add(new SystemB(...
gr1d3r
1

votes
0

answer
278

Views

How to remove annotations of a variable in a child class

Let us say that we have a class from API which has some fields, of which some are made mandatory using @NotNull annotation, like below: class SomeAPIClass { private String field1; private String field2; @NotNull public String getField1() { return field1; } public String getField2() { return field2;...
Rupesh
1

votes
1

answer
36

Views

If I extend a class in python, how to automatically return the result as the new class?

Here is a simple example: import pandas as pd class test_pd(pd.DataFrame): def __init__(self): super().__init__() def my_copy(self): return self.copy() if __name__=='__main__': a = test_pd() #a has a.my_copy() b = a.my_copy() #b does not have b.my_copy() I would like the test_pd.my_copy() function t...
fnosdy
1

votes
1

answer
51

Views

Class Decorators Excercise in Python

I'm learning Python from the video course Python Programming Language by David Beazley. I got stuck in this program where the author tried to decorate the Holding class by @typed. Unfortunately, although I believe I copied the code verbatim, it didn't seem to work according to plan. I duplicated the...
Jeremy Chen
1

votes
1

answer
147

Views

Using S3 Constructors in R

I was looking at some code earlier today (https://www.r-bloggers.com/building-your-own-blockchain-in-r/) where someone was defining S3 functions in a way that I'd never seen before and liked the visual style, but have never seen it anywhere else in R code and wanted to figure out why. They defined a...
Alex Gold
1

votes
1

answer
53

Views

Trying to get property of non-object using php oops concept

I am trying to retrieve the data from database using OO-PHP Here is my code user.php database.php public function query($sql){ /*$result = mysqli_query($this->connection,$sql);*/ $result = $this->connection->query($sql); $this->confirm_query($result); return $result; } index.php $users = User::find_...
Sanam Rohit
1

votes
2

answer
70

Views

How to call a sub-object property in PHP?

Let's say that I have two classes, 'Product' and 'Brand'. A Product has the following properties: -ProductID -ProductName -ProductBrandID -ProductCost And a Brand: -BrandID -BrandName -BrandLogo Let's say that in the Product class, i've: GetID() returning an Integer GetName() returning a String GetB...
Junior Zancan
1

votes
0

answer
39

Views

whats the difference between these patterns?

I use the following pattern all of the time: ;(function(ns){ var _str = 'hello'; ns.hello = function(){ console.log(str) } })(this.app = this.app || {}); app.hello(); // logs 'hello' to the console I'm trying to to do more OO type things and want to reuse this pattern. What I've come up with is the...
user1990962
1

votes
0

answer
68

Views

Symfony - where should be csrf validation done when not using symfony forms component?

From the documentation I see it is done in controller: https://symfony.com/doc/current/security/csrf.html public function delete(Request $request) { $submittedToken = $request->request->get('token'); // 'delete-item' is the same value used in the template to generate the token if ($this->isCsrfToken...
Darius.V
1

votes
0

answer
34

Views

Factory Pattern: createABC vs getType(ABC)

I am trying to write code that filters a list of objects based on certain properties they have. There are many ways to filter these objects, so I can have multiple filters. I am not sure if this matters for this question, but these filter classes have different constructors. To control the creation...
bchen
1

votes
3

answer
79

Views

How to reference this in closure

I am using PHP Version 7.1.1. In index.php of my MVC I create a route collection ($routes). To it I want to add a route group (with addGroup). This addition process relies on a closure (e.g. an anonymous function), passed as argument to addGroup. Inside of closure I am trying to reference the collec...
1

votes
0

answer
384

Views

Matlab error: constructor must preserve the class

connected question: Returning an object of a second class through the constructor of first class but the answer doesn't seem to be of much help for me. I'm having trouble with Matlab constructors. I want to create a class A and a subclass B, such that I can provide the constructor of B with either t...
Kenji
1

votes
1

answer
43

Views

How can I turn on the auto matching when invoking the template explicit instantiation?

I have two template class, and the Leaf is derived from the Base, then I developed a template function which uses the reference of the Base as its parameter. And set the definition and the instantiation of that function in its source file. The issue happened once I tried to pass the Leaf as the argu...
Calvin
1

votes
0

answer
40

Views

Drawing images to canvas with objects

I need to create a game with objects. I'm trying to draw my Link object to the canvas, but it won't work for some reason. I've tried methods described in other answers for this question. I don't understand why the image won't display on the canvas! Here is my code (edited to the relevant bits). HTML...
aiolian
1

votes
0

answer
58

Views

The Object-Serialization-Issue: Dealing with Inheritance while Mapping Objects

I am struggeling with the following Problem which is pretty common and I am not sure if properly solved by existing frameworks. I would call the problem object-mapping problem or object-serialization problem. I am writing a Java program where I have an abstract object wich has many implemenations. T...
Christof Tinnes
1

votes
2

answer
41

Views

Iterating over private data member

I have class Data, that holds different data structures related to common topic. public class Data { DataTypeA structureA; DataTypeB structureB; DataTypeC structureC; } public class DataTypeA extends ConcurrentHashMap { } I want to write code that iterates over structureA and performs some operat...
Victoriia
1

votes
1

answer
365

Views

static factory methods inside interface

This is a design question regarding using static factory methods inside of static interface methods. I am looking for improvements or disadvantages of using this pattern. public interface SampleService { // 1) do work void doWork(); static SampleService create(final String service) { // 2) Dispatch...
Greg
1

votes
0

answer
24

Views

Best way to pass data into plugin objects

I often have the problem that I have different plugins that all essentially perform the same task, but all in a different way. So each plugin needs different kinds of parameters, but how to pass them? IMySpecificTask plugin = PluginResolver.Resolve(typeof(IMySpecificTask)); Each implementation of IM...
codymanix
1

votes
0

answer
73

Views

Good OOP design of a Linear Solver in Fortran

I am programming a program in OOP Fortran for the solution of linear systems A x = b. The linear solver will require the use of the matrix-vector operation, thus I built a type containing the matrix (only a single number in the following example) and the matrix-vector multiplication procedure. modu...
enrico
1

votes
1

answer
66

Views

Redefine Class Instances in Python

I am migrating a project I have from being littered with globals variables to actually have a structure defined by classes defined in a separate module. This is my first time really using OOP so want to understand if it is safe to re-define an instance of a Class or if my code is missing something....
mattdonders
1

votes
2

answer
73

Views

Java: Should occasionally used objects be included as a class member?

I have a class CodeText as below: public class CodeText{ private char type; private int textCode; //Other members } Occasionally, if there is an error when a Symbol object refers to a CodeText object, I need to 'record' that Symbol so that the CodeText 'knows' who is referring it when error happens....
CodingNow
1

votes
1

answer
114

Views

Why often Laravel 5 uses method_exists instead of abstract methods?

FYI: I am using Laravel 5.5 One of the example: when you do: php artisan make:request TestRequest artisan generates TestRequest class which extends from FormRequest class and with generated 2 methods: rules() and authorize() Let's have a look to passesAuthorization() method in FormRequest class: pro...
Aleksandrs
1

votes
2

answer
75

Views

Create Class of Lists in Python

I have to extract patient information from a bunch of XML files for further data analysis. I have multiple Patients that can have multiple Diseases . For each Diseases there may or may not have been a Treatment or several. Each Treatment may or may not have TreatmentDetails. The TreatmentDetails are...
Roxanne
1

votes
1

answer
123

Views

Is there a way to abstract the setters/getters of a JS property to the Object's prototype?

I am trying to create a very simple class in Node. The class is called Student and it contains a few variables which I wish to keep private. Thus, I have not labeled them as this.field = ''; but rather let field = ''; I read that properties of an object could be created inside the constructor like...
ajax992
1

votes
1

answer
324

Views

inheritance vs composition - specific case

I have been reading about inheritance vs composition. I know there is already allot about this topic on this site, but i have a question about this specific example I hope will not be seen as duplicate. Some guidelines I have gathered so far: Prefer composition over inheritance. Has-a relationship w...
Roygbiv
1

votes
1

answer
40

Views

How to get JavaScript object to properly display rectangle on screen?

I'm doing an eCard for class and I have decided to create an animation using classes for the specific objects. The first thing I am doing is trying to get the background to draw a black rectangle across the whole canvas using Background.DrawBackgound. But nothing is working. I have tried even copy...
JGoss
1

votes
1

answer
28

Views

Loader factory from allowed list of classes

I'd want to be able to dispatch the instantiation of a class depending of a parameter. This has been asked here and the following answer provides a good solution, but the use of the globals()[name]() seems a bit ugly to me. Also, I understand how it works if the file is the main one, but not sure w...
1

votes
1

answer
70

Views

Interacting with the objects in a list as a whole [closed]

I was wondering if there was an easy way to make separate objects, say 3-5 of them, assign them to a list and have that list be able to access the methods from the class those objects came from. I can interact with each individual object and have made a way to interact with the objects as a single g...
Calva
1

votes
2

answer
58

Views

Multiple instances of my typescript class seem to be pointing to the same data, how do I remedy this?

So I've created a simple class: export class myClass { private static _instance: myClass; public name: string; public metrics: Array; private constructor() { this.set_metrics(); //... } public static get Instance() { // Do you need arguments? Make it a regular method instead. return (this._instance...
peter-stuart-turner
1

votes
1

answer
32

Views

subSetSum - powerSum JS . concatenating arrays into subset

With my very poor js knowledge, I am trying to solve the powerSum algorithm.Where I am supposed to return the count of the ways a number X can be expressed as the sum of the Nth powers of unique, natural numbers... I have -somehow- got to the point where I can see my subsets printed out on the cons...
Monique
1

votes
0

answer
30

Views

Spawning/instantiating projectiles in games: organisation of aggregation/composition/association

I'm trying to organised a good OO model for a game (being written in C#/XNA) but am uncertain of the 'best' way to deal with players, projectiles, enemies, and the environment. The main classes involved will be : level, player, tilemap, enemy, projectile. Which of the following would be, in general,...
sm284614
1

votes
1

answer
49

Views

Testing objects that reference other objects in minitest & ruby

I am having trouble testing methods similar to this using minitest where objects have references to other objects: def drive num old_place = @current_place.name if num == 0 road = @current_place.first_place_road @current_place = @current_place.first_place else road = @current_place.second_place_road...
scumlyfe
1

votes
1

answer
802

Views

Understanding a Linked List implementation in Python

I have found an implementation of a Linked List in Python, online, but it doesn't have any explanation or comments. I understand the underlying concepts of a Linked List, but there is one key part of the code I don't understand: class Node: def __init__(self, data): self.data = data self.next = None...
user9352220
1

votes
3

answer
39

Views

Is it best practice to avoid 'self' instances?

If I were to implement a class, such as: class LinkedList: def __init__(self): self.head = None def size(self): self.count = 0 self.current = self.head while self.current != None: self.current = self.current.get_next() self.count += 1 return self.count The size method would work perfectly fine witho...
user9352220
1

votes
0

answer
38

Views

Converting an object in string type into Python object

I have a tree structure stored in a text file. I want to read the file into Python and convert it into a Node object. The standard way to build the tree is as follows: from zss import Node A = ( Node('+') .addkid(Node('+') .addkid(Node('2')) .addkid(Node('4'))) .addkid(Node('*') .addkid(Node('3...
SriniShine
1

votes
2

answer
39

Views

call child class method without call parent class

I want to call this function without using its parent name: Square square = new Square(Color.Blue, Pattern.DOTTED, 4); but its error and said use this: Square square = new Square(Shape.Color.Blue, Shape.Pattern.DOTTED, 4); I want do this without import Shape calss and its method in main The class is...
narges
1

votes
1

answer
41

Views

Is the decorator pattern the correct pattern to be used on this situation

I would like to ask if the decorator pattern suits my needs and is another way to make my software design much better? Previously I have a device which is always on all the time. On the code below, that is the Device class. Now, to conserve some battery life, I need to turn it off then On again. I c...
newbie programmerz
1

votes
2

answer
980

Views

Error with Parcelable object: Unmarshalling unknown type code *** at offset ***

I'm trying to finish this project for school and I keep getting this error in the 'read' portion of my Parcelable code. This has eaten up many hours of my day and I've read countless StackOverflow pages with similar problems and none of those solutions worked for me. Here are things I've read about...
joelc1225
1

votes
0

answer
102

Views

Java access JTextField from another class

I want to access JTextField but I can't because its in another class. My GUI and my ActionListener class are separated but I want to access the JTextField from another class and I have problem doing so. Sorry that the code is quite lenghty, when I run this it gives me nullpointer exception. How shou...
BadAssPanda
1

votes
2

answer
71

Views

Class design - manual cast alternatives

Question is related to classes below. Zookeeper1 and Zookeeper2 are 2 alternatives that I could use. I can potentially store in future different types of animals inside Zookeeper. I should be able to get those classes. In 1st case I store all animals in list (meaning in future I can add other new An...
Bojan Vukasovic

View additional questions