oop

Why use staticmethod instead of no decorator at all

徘徊边缘 提交于 2021-02-19 04:34:04
问题 There are several good explanations on SO about why/when you should use a class method vs a static method, but I've not been able to find an answer for when you would use a static method over no decoration at all. Consider this class Foo(object): @staticmethod def f_static(x): print("static version of f, x={0}".format(x)) def f_standalone(x): print("standalone verion of f, x={0}".format(x)) And some output: >>> F = Foo >>> f = F() >>> F.f_static(5) static version of f, x=5 >>> F.f_standalone

Is this precondition a violation of the Liskov Substitution Principle

二次信任 提交于 2021-02-19 01:16:13
问题 I have 3 classes, Account , CappedAccount , UserAccount , CappedAccount , and UserAccount both extend Account . Account contains the following: abstract class Account { ... /** * Attempts to add money to account. */ public void add(double amount) { balance += amount; } } CappedAccount overrides this behavior: public class CappedAccount extends Account { ... @Override public void add(double amount) { if (balance + amount > cap) { // New Precondition return; } balance += amount; } } UserAccount

Initializing variables outside of PHP constructor

删除回忆录丶 提交于 2021-02-18 22:10:41
问题 I was looking through the PHP documentation and saw several comments where a variable was initialized outside of a class's constructor, similar to the following: classMyClass { private $count = 0; public function __construct() { //Do stuff } } In PHP Objects, Patterns, and Practice , the author recommends using constructs only for the initialization of properties, deferring any heavy lifting or complex logic to specialized methods. This tutorial (a quick example that I found on Google) also

Initializing variables outside of PHP constructor

强颜欢笑 提交于 2021-02-18 22:10:04
问题 I was looking through the PHP documentation and saw several comments where a variable was initialized outside of a class's constructor, similar to the following: classMyClass { private $count = 0; public function __construct() { //Do stuff } } In PHP Objects, Patterns, and Practice , the author recommends using constructs only for the initialization of properties, deferring any heavy lifting or complex logic to specialized methods. This tutorial (a quick example that I found on Google) also

Initializing variables outside of PHP constructor

丶灬走出姿态 提交于 2021-02-18 22:09:47
问题 I was looking through the PHP documentation and saw several comments where a variable was initialized outside of a class's constructor, similar to the following: classMyClass { private $count = 0; public function __construct() { //Do stuff } } In PHP Objects, Patterns, and Practice , the author recommends using constructs only for the initialization of properties, deferring any heavy lifting or complex logic to specialized methods. This tutorial (a quick example that I found on Google) also

Initializing variables outside of PHP constructor

你离开我真会死。 提交于 2021-02-18 22:08:17
问题 I was looking through the PHP documentation and saw several comments where a variable was initialized outside of a class's constructor, similar to the following: classMyClass { private $count = 0; public function __construct() { //Do stuff } } In PHP Objects, Patterns, and Practice , the author recommends using constructs only for the initialization of properties, deferring any heavy lifting or complex logic to specialized methods. This tutorial (a quick example that I found on Google) also

What is meant by “classes themselves are objects”?

本秂侑毒 提交于 2021-02-18 20:30:11
问题 I was just reading the Python documentation about classes; it says, in Python "classes themselves are objects". How is that different from classes in C#, Java, Ruby, or Smalltalk? What advantages and disadvantages does this type of classes have compared with those other languages? 回答1: In Python, classes are objects in the sense that you can assign them to variables, pass them to functions, etc. just like any other objects. For example >>> t = type(10) >>> t <type 'int'> >>> len(t.__dict__)

Find gap between multiple dates in C#

♀尐吖头ヾ 提交于 2021-02-18 19:47:18
问题 I'm currently programming an appointment finder that automatically syncs with MS Exchange Server. The program should lookup multiple users' calendar when they are available. I have coded some demo data with multiple users and their appointments. The appointments consist of two DateTimes. So my question is: How can I find a gap between multiple users appointments, where everyone is available. This is the demo data: var appointment1ForPerson1 = new Appointment(1, new List<Appointment.Time>() {

C# - Should an object be responsible for creating a history object when it changes something like status?

不羁的心 提交于 2021-02-18 15:25:23
问题 This is more of an architecture/best practices question than anything else, so please feel free to add your two cents. I know i stated status in the title, but this goes for any basic property of an object. I think the account example below will help demonstrate my question a little better than status. Here is a sample Account object: public class Account { private IList<Transaction> _transactions; public AddTransaction(trans as Transaction) { _transaction.add(trans) } } Now lets say I want

R: Use active binding in object generator to conditionally add new class to R6 objects

半腔热情 提交于 2021-02-18 14:42:48
问题 I have a simple R6 object generator: thing <- R6Class("youngThing", private = list( ..age = 0), active = list( age = function(){ private$..age <- private$..age + 1 private$..age } ) ) That gives me a simple R6 object, where ..age increases by 1 every time the active age field is called: a_thing <- thing$new() a_thing$age # [1] 1 I want the object class of a_thing to change given a threshold value of the private field ..age , like this: class(a_thing) # [1] "youngThing" "R6" for(timestep in 1