oop

Any disadvantage to creating a class which uses methods which return a Task<T> but are synchronous methods over methods returning T

泪湿孤枕 提交于 2021-01-29 04:40:48
问题 Let's say we have this class - MyClass MyClass has methods that convert different types of lists to XDocuments As an example we'll just look at one method, Convert I could write this method as: public XDocument Convert<T>(IList<T> myList) { XDocument doc = new XDocument("root"); //Do some synchronous work return doc; } Or I could write it like the following: public Task<XDocument> Convert<T>(IList<T> myList) { XDocument doc = new XDocument("root"); //Do some synchronous work return Task

Best practice for OOP style loading of nested objects from a YAML file

旧时模样 提交于 2021-01-29 03:39:41
问题 I have a YAML file that describes the content of a text game. It contains the data required to populate many nested objects. These are scenes, actions, outcomes and state updates. A scene contains many actions, an action has many possible outcomes and an outcome can cause many states to update. I have defined classes for each of these entities, but I am not sure of the best way to create them from an object oriented perspective. Should I be creating everything in one big nested set of for

When chaining methods in PHP, Is it possible to determine which method was called first?

你离开我真会死。 提交于 2021-01-29 01:56:59
问题 So I am writing a PHP class at the moment to help make construction of SQL statements easier. I am writing them so that each method returns an instance of the object ( return $this ) in order to enable method chaining. Is it possible to test whether a method being called is a part of a method chain and which methods have been executed already in the method chain? For example, in the below code, I would like the 'from' method to test whether it was called as part of a chain with the select()

When chaining methods in PHP, Is it possible to determine which method was called first?

北城以北 提交于 2021-01-29 01:55:34
问题 So I am writing a PHP class at the moment to help make construction of SQL statements easier. I am writing them so that each method returns an instance of the object ( return $this ) in order to enable method chaining. Is it possible to test whether a method being called is a part of a method chain and which methods have been executed already in the method chain? For example, in the below code, I would like the 'from' method to test whether it was called as part of a chain with the select()

All objects made through constructor have the same vectors

房东的猫 提交于 2021-01-28 23:55:30
问题 I'm new to C++ and I am trying to create a basic genetic algorithm. I created a Chromosome class and want to create a Society class that generates a vector of these Chromosomes with randomly generated "genes". Genes being the vector in the Chromosome that holds values of 0 or 1. I was testing out the Chromosome constructor, and all of the objects have the same gene vectors. How can I make the constructor generate random values? I have included code below. Any other coding practice or

All objects made through constructor have the same vectors

↘锁芯ラ 提交于 2021-01-28 23:43:35
问题 I'm new to C++ and I am trying to create a basic genetic algorithm. I created a Chromosome class and want to create a Society class that generates a vector of these Chromosomes with randomly generated "genes". Genes being the vector in the Chromosome that holds values of 0 or 1. I was testing out the Chromosome constructor, and all of the objects have the same gene vectors. How can I make the constructor generate random values? I have included code below. Any other coding practice or

How do I call a class function inside the class definition?

一笑奈何 提交于 2021-01-28 21:29:53
问题 class MetaData(): maxSize = 2**10 # class definition code if not os.path.exists('sample.data'): SSD = open('sample.data', 'wb+') data = { 0: [], 1: {'.': None,} } data[1]['~'] = data[1] MetaData.save() # i want to call the save function here # class function @classmethod def save(cls): cls.SSD.seek(0) cls.SSD.write(b' ' * cls.maxSize) cls.SSD.seek(0) cls.SSD.write(pickle.dumps(cls.data)) I want to use the save() function inside the class block. I've tried MetaDate.save() and simply save()

What is the difference between the CoR and the Decorator? Why is CoR a behavioral pattern? Why is Decorator a structural pattern?

耗尽温柔 提交于 2021-01-28 19:27:20
问题 This answer almost describes the first half of the question. It says: After reading the Gang of Four definitions, I'm not convinced there's a real difference. (included for convenience) Decorator: Allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviours Chain of Responsibility: Gives more than one object an opportunity to handle a request by linking receiving objects together Wikipedia fleshes them out a little, but some of it's kinda

Most efficient and most Pythonic way to deep copy class instance and perform additional manipulation

允我心安 提交于 2021-01-28 13:51:11
问题 Say I have a Python class instance, here's a really simple example: class Book: def __init__(self, genre): self.genre = genre self.author = self.title = None return def assign_author(self, author_name): self.author = author_name return def assign_title(self, title_name): self.title = title_name return western_book_template = Book("Western") For various reasons, I want each class instance to be able to generate a deep copy of itself, and then perform some additional manipulation on the new

Calling methods of a virtual member class

北城以北 提交于 2021-01-28 12:31:14
问题 I know how virtual works in the context of member functions, but I saw an article online about virtual member classes that confuses me. The example I found is this: class Machine { void run () {} virtual class Parts { }; }; // The inner class "Parts" of the class "Machine" may return the number of wheels the machine has. class Car: public Machine { void run() { cout << "The car is running." << endl; } class Parts { int get_Wheels () { cout << "A car has 4 wheels." << endl; return 4; } string