oop

Is it possible to initialize the argument of a method with a self.variable value from __init__ in python?

ぃ、小莉子 提交于 2021-02-05 08:25:10
问题 I have a class with an __init__ method where I define some variables, as well as a method AddSignalSpectrum : class SpectrumGraph: # Properties def __init__(self): self.PlotHeight= 300 self.PlotWidth = 800 self.xRangeMin = -10 self.xRangeMax = 10 self.yRangeMin = -0.1*(self.xRangeMax - self.xRangeMin) self.yRangeMax = 0.9*(self.xRangeMax -self.xRangeMin) def AddSignalSpectrum( self, name, Type, CenterPosition, Bandwidth=2, Height = self.yRangeMax*0.8, Color = "#0000FF", LineWidth = 2,

accessing fields of a class in Java

对着背影说爱祢 提交于 2021-02-05 08:00:54
问题 I am completely new to Java. I was practicing a code about a person eating some fruit. I have 3 classes Fruit Class: public class Fruit { String fruitname = "grapes"; } Person Class: public class Person { void eat(Fruit f) { System.out.println("person is eating " + f.fruitname); // how can I do f.fruitname } } Test Class: public class TestFruit { public static void main(String[] args) { Person p = new Person(); // person object Fruit f = new Fruit(); // fruit object p.eat(f); } // eat method

When are static variables initialized in Python?

那年仲夏 提交于 2021-02-05 07:48:45
问题 Consider the following code class Foo: i = 1 # initialization def __init__(self): self.i += 1 t = Foo() print(t.i) When exactly does the initialization of i take place? Before the execution of the init method or after it? 回答1: Before. The __init__ method isn't run until Foo is instantiated. i=1 is run whenever the class definition is encountered in the code You can see this by adding a print statements: print('Before Foo') class Foo: i = 1 print(f'Foo.i is now {i}') def __init__(self): print(

Assign a class instance variable to a local variable within a method in Python

安稳与你 提交于 2021-02-05 07:48:06
问题 It is possible to assign a class instance variable to a local variable within a method, such as: class Foo(object): def __init__(self): self.bar = 'bar' def baz(self): # assign instance variable to local variable with a method bar = self.bar # do work with local variable bar = "qux" # update value of instance variable self.bar = bar return self By doing this, one is able to refer to bar instead of self.bar within the scope of Foo.baz() . Is it wrong, or Unpythonic, to do this? 回答1: Doing that

How to create List of Action Delegates when different number and types of parameters are needed

别等时光非礼了梦想. 提交于 2021-02-05 06:41:45
问题 We have a set of about two dozen classes that inherit from a base class which has an abstract Validate method. Of course each class has different validation needs but the rules are needed in different combinations between all of them so, as you can imagine, this resulted in a lot of code duplication, as an example: Class A needs rules 1, 3, 6, and 9 Class B needs rules 3, 4, and 8 Class C needs rules 1, 8, and 9 .... you get the picture. So I was thinking of doing a simple refactoring and

If a class might be inherited, should every function be virtual?

半城伤御伤魂 提交于 2021-02-05 05:00:30
问题 In C++, a coder doesn't know whether other coders will inherit his class. Should he make every function in that class virtual? Are there any drawbacks? Or is it just not acceptable at all? 回答1: In C++, you should only make a class inheritable from if you intend for it to be used polymorphically. The way that you treat polymorphic objects in C++ is very different from how you treat other objects. You don't tend to put polymorphic classes on the stack, or pass them by or return them from

Integrity constraint violation: 1048 Column 'name' cannot be null error

江枫思渺然 提交于 2021-02-04 19:54:30
问题 there were a lot of answers related to this, but I couldn't find useful information. I'm trying to connect to the database and insert user's entered values into it, but I got this error and I seriously don't know what I am doing wrong. I've created 2 different classes in 2 different files, one is connection.php and the other is users.php (for insterting the users into the database) Could someone help me to solve this? Here is my connection.php file: <?php class Connection { public $dbh; //

Integrity constraint violation: 1048 Column 'name' cannot be null error

女生的网名这么多〃 提交于 2021-02-04 19:53:52
问题 there were a lot of answers related to this, but I couldn't find useful information. I'm trying to connect to the database and insert user's entered values into it, but I got this error and I seriously don't know what I am doing wrong. I've created 2 different classes in 2 different files, one is connection.php and the other is users.php (for insterting the users into the database) Could someone help me to solve this? Here is my connection.php file: <?php class Connection { public $dbh; //

difference between function hiding and overloading

久未见 提交于 2021-02-04 18:08:52
问题 I can't find any difference between function hiding and overloading. As the function hiding is the function that is present in derived class and hides the function of a base class. Having same name of the function in both of them. Overloading: having same name but different signature in both derived and base class. class A { void print(int); }; class B: public A { void print(float); }; does it hide function or overload ? 回答1: The function B::print hides the parent function A::print . If you

difference between function hiding and overloading

偶尔善良 提交于 2021-02-04 18:07:33
问题 I can't find any difference between function hiding and overloading. As the function hiding is the function that is present in derived class and hides the function of a base class. Having same name of the function in both of them. Overloading: having same name but different signature in both derived and base class. class A { void print(int); }; class B: public A { void print(float); }; does it hide function or overload ? 回答1: The function B::print hides the parent function A::print . If you