class

Declaring a local variable within class scope with same name as a class attribute

自古美人都是妖i 提交于 2019-12-29 08:15:13
问题 While observing another person's code, i realized that within class A's method he declared a local int with the same name as an attribute of class A. For example: //classA.h class A{ int Data; void MethodA(); }; //classA.cpp #include "classA.h" using namespace std; void A::MethodA(){ int Data; //local variable has same name as class attribute Data = 4; //Rest of Code } I found it weird that the compiler would accept it without returning an error. In the above case, would the 4 be assigned to

继承与派生

夙愿已清 提交于 2019-12-29 08:05:14
什么是继承 继承是一种创建新类的方式, 新建的类可以继承一个或多个父类(python支持多继承),父类又可称为基类或超类,新建的类称为派生类或子类。 python中类的继承分为:单继承和多继承 class ParentClass1: #定义父类 pass class ParentClass2: #定义父类 pass class SubClass1(ParentClass1): #单继承,基类是ParentClass1,派生类是SubClass pass class SubClass2(ParentClass1,ParentClass2): #python支持多继承,用逗号分隔开多个继承的类 pass 继承的信息 class Sup: __num = 10 def __init__(self, name): self.__name = name @property def name(self): print(self) return self.__name @classmethod def __c_fn(cls): print(cls, 'c fn') def __o_fn(self): print(self.name, 'o fn') class Sub(Sup): def test(self): print(self) print(self.__name) pass 继承关系 1

How do I elegantly/efficiently write the __init__ function for a Python class that takes lots of instance variables?

こ雲淡風輕ζ 提交于 2019-12-29 08:00:08
问题 Let's say you have a class that takes many (keyword) arguments, most of which are meant to be stored as instance variables: class ManyInitVariables(): def __init__(a=0, b=2, c=1, d=0, e=-1, ... , x=100, y=0, z=9): How would you initialize them in __init__ ? You could do something like this: class ManyInitVariables(): def __init__(a=0, b=2, c=1, d=0, e=-1, ... , x=100, y=0, z=9): self.a = a self.b = b self.c = c ... self.z = z ...but it would take a lot of typing! How could I get __init__ to

how to access winform components from another class?

a 夏天 提交于 2019-12-29 07:49:06
问题 i have a form with one button and two labels and i have a separate class called myCounter i want the myCounter class to be able to access the labels in the form through a method called changeColor.. how can make the labels available in this class the form public partial class Form1 : Form { public Form1() { InitializeComponent(); } public Color colTurn { get { return lblp1Turn.BackColor; } set { lblp1Turn.BackColor = value; } } private void Form1_Load(object sender, EventArgs e) { } } the

What is the difference between UIView and UIViewController?

半世苍凉 提交于 2019-12-29 07:13:27
问题 I need a detailed explanation on the following: What do we use a UIViewController for? What is the use of it? I have a class that looks like the following: class one { UINavigationController *nav = ...; two *secondObject = ...; // By use of it, I have push the new view class two//ok } class two { ... } How can I use secondObject in the class one ? What is the class hierarchical start from the window? 回答1: The UIViewController is the controller part in the MVC design pattern. Model <------->

What is the difference between UIView and UIViewController?

余生颓废 提交于 2019-12-29 07:13:10
问题 I need a detailed explanation on the following: What do we use a UIViewController for? What is the use of it? I have a class that looks like the following: class one { UINavigationController *nav = ...; two *secondObject = ...; // By use of it, I have push the new view class two//ok } class two { ... } How can I use secondObject in the class one ? What is the class hierarchical start from the window? 回答1: The UIViewController is the controller part in the MVC design pattern. Model <------->

Objective-C Properties with or without instance variables [duplicate]

故事扮演 提交于 2019-12-29 07:10:47
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Properties and Instance Variables in Objective-C 2.0 I'm using properties only for ivars that don't require any real validation when getting or setting a value for a class that I'm authoring. Ex: @interface ClassB : NSObject { // No ivars } @property (nonatomic, retain) ClassA *obj; @property (nonatomic, retain) NSString *str; Whenever I call these methods within the class I'm always using self.obj/self.str or

NameError using execfile in python

萝らか妹 提交于 2019-12-29 07:08:30
问题 My application has a button to execute a python script dynamically using execfile . If I define a function inside the script (eg. spam() ) and try to use that function inside another function (eg. eggs() ), I get this error: NameError: global name 'spam' is not defined What is the correct way to call the spam() function from within eggs() ? #mainprogram.py class mainprogram(): def runme(self): execfile("myscript.py") >>> this = mainprogram() >>> this.runme() # myscript.py def spam(): print

Matlab equivalent to calling inside static class

我们两清 提交于 2019-12-29 07:04:52
问题 Confer the following code: classdef highLowGame methods(Static) function [wonAmount, noGuesses] = run(gambledAmount) noGuesses = 'something'; wonAmount = highLowGame.getPayout(gambledAmount, noGuesses); % <--- end function wonAmount = getPayout(gambledAmount, noGuesses) wonAmount = 'something'; end end end Is there a way to call a static method of the same class (inside a static) method without having to write the class name? Something like "self.getPayout(...)" - in case the class turns out

How to access class-scope variables without self?

落花浮王杯 提交于 2019-12-29 06:44:43
问题 So I have a class, which I'm using as a local namespace. I have some static functions in the class, but they can't access the class scope variables. Why is this? class Foo: foo_string = "I am a foo" @staticmethod def foo(): print foo_string >>> Foo.foo() [Stack Trace] NameError: global name 'foo_string' is not defined Any thoughts? 回答1: Python doesn't let class variables fall into scope this way, there are two ways to do this, the first is to use a class method: @classmethod def foo(cls):