class

the list updated within a class

帅比萌擦擦* 提交于 2019-12-25 05:23:14
问题 class A(object): aalist = [] ai = 0 def __init__(self): self.ablist = list() def add(self): self.aalist.append(["a","a"]) self.ablist.append(["b","b"]) self.ai = self.ai + 1 class B(A): def __init__(self): A.__init__(self) self.bblist = list() def badd(self): self.bblist.append(['c','c']) for i in range(1,4): c = B() c.add() c.badd() print c.aalist,c.ablist,c.bblist,c.ai run these codes , the result prompts: [['a', 'a']] [['b', 'b']] [['c', 'c']] 1 [['a', 'a'], ['a', 'a']] [['b', 'b']] [['c',

Call a method that requires a derived class instance typed as base class in VB.NET or C#

孤者浪人 提交于 2019-12-25 05:23:09
问题 I have two objects - "Spaceship" and "Planet" derived from a base "Obj". I have defined several classes - Circle, Triangle, Rectangle, etc. which all inherit from a "Shape" Class. For collision detection purposes, I want to give Obj a "shape": Dim MyShape as Shape So that in "Spaceship" I can: MyShape = new Triangle(blah,blah) and in "Planet" I can: MyShape = new Circle(blah,blah) I have a method (overloaded several times) which checks for collisions between different shapes, for example:

Python Issues with a Class

谁说我不能喝 提交于 2019-12-25 05:00:30
问题 I am having issues with my below class. I keep getting the below traceback, butI am not sure were I am going wrong. I am expecting to see a dictionary with photo tags. Any help would be great. Traceback: File "project.py", line 231, in <module> new_photo = Photo(result_get_photo_data) File "project.py", line 228, in __init__ for diction in photo_diction["photo"]["tags"]["tag"]: TypeError: list indices must be integers, not st My Code: class Photo(object): def __init__(self,photo_diction):

modify parent class's variable in extended class

泄露秘密 提交于 2019-12-25 04:57:43
问题 I wanna replace a variable in class A by calling function replace in class B. e.g. in code below I want replace 'hi' for 'hello' but output is 'hi' P.S : class B is some controller and must get instance in class A. i'm using php 5.2.9+ <?php $a = new A(); class A { protected $myvar; function __construct() { $this->myvar = "hi"; $B = new B(); echo $this->myvar; // expected value = 'hello', output = 'hi' } function replace($search, $replace) { $this->myvar = str_replace($search, $replace, $this

A field to count the amount instances of a class [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-25 04:55:16
问题 This question already has answers here : post increment operator java (3 answers) Closed 6 years ago . I need to create a field to count the number of instances made by a class public class Circle { private int diameter; private int xPosition; private int yPosition; private String color; private boolean isVisible; private static int count = 0; /** * Create a new circle at default position with default color. */ public Circle() { diameter = 30; xPosition = 20; yPosition = 60; color = "blue";

Calling out a function, not defined error [duplicate]

橙三吉。 提交于 2019-12-25 04:46:15
问题 This question already has answers here : Python call function within class (2 answers) Closed 4 months ago . I'm new to making classes and I'm trying to complete exercise 9-1 in my 'Python Crash Course' book where the last part of the question asks me to call back my method but I end up getting 'not defined error' for describe_restaurant() . Here is my code: class Restaurant(): def __init__(self, r_name, c_type): self.r_name = r_name self.c_type = c_type def describe_restaurant(): print(self

Understanding Object Inheritance

可紊 提交于 2019-12-25 04:39:27
问题 In a previous question (linked here), there were some unresolved issues about object inheritance which remain unclear. (my use of terminologies may be incorrect - please edit accordingly - thank you) Put simply, why does the superclass not inherit the properties of a subclass that has been expressly assigned to it. A code example: UIView *view = nil; UILabel *label = [[UILabel alloc] init]; label.text = @"some text..."; view = label; // view has not inherited any UILabel properties view.text

PHP and mysql connections [closed]

与世无争的帅哥 提交于 2019-12-25 04:38:37
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 10 years ago . Is it possible to create a connection in a PHP class file and use it in all of the different methods in the class? I am trying to open a connection in the constructor and i get an error when i get to the close connection method saying that the argument that I've provided in the mysql_close() statement isn't a

C# - Why can I not cast a List<MyObject> to a class that inherits from List<MyObject>?

有些话、适合烂在心里 提交于 2019-12-25 04:33:30
问题 I've got an object, which I'll call MyObject. It's a class that controls a particular data row. I've then got a collection class, called MyObjectCollection: public class MyObjectCollection : List<MyObject> {} Why can I not do the following: List<MyObject> list = this.DoSomethingHere(); MyObjectCollection collection = (MyObjectCollection)list; Thanks in advance. Edit: The error is InvalidCastException 回答1: My guess is that DoSomethingHere doesn't return an instance of MyObjectCollection . Let

Sort Items by parent relationship

99封情书 提交于 2019-12-25 04:26:47
问题 Coming from here I ask myself if there is a easier way to sort a class like public class ParentChild { int ID { get; set; } int ParentID { get; set; } public ParentChild(int id, int pid) { ID = id; ParentID = pid; } } depending on it's parent-relationship. e.g. List<ParentChild> pcItems = new List<ParentChild>() { new ParentChild(1,0), // 0 is the default value in case of no parent new ParentChild(2,1), new ParentChild(3,2), new ParentChild(4,2), new ParentChild(5,1), new ParentChild(6,4),