scope

Block scope variables

狂风中的少年 提交于 2019-12-17 19:19:21
问题 This will compile class X { public static void main(String args[]) { { int a = 2; } { int a = 3; } } } This won't class X { public static void main(String args[]) { int a = 2; { int a = 3; } } } I expected both to compile (maybe it is the way C works?). What is the reason because it is not possible to declare a variable in a block with the same name of one in the outer block? 回答1: The short answer is: Because this is the way the Java language is defined in JLS §6.4. You might be used from

Access List from another class

房东的猫 提交于 2019-12-17 19:03:33
问题 can anyone tell me how to create a list in one class and access it from another? 回答1: public class MyClass { private List<string> myList = new List<string>(); public List<string> GetList() { return myList; } } You can have any anything there instead of string. Now you can make an object of MyClass and can access the public method where you have implemented to return myList . public class CallingClass { MyClass myClass = new MyClass(); public void GetList() { List<string> calledList = myClass

Java: Anonymous inner class using a local variable

六眼飞鱼酱① 提交于 2019-12-17 18:59:55
问题 How can I get the value of userId passed to this method in my anonymous inner subclass here? public void doStuff(String userID) { doOtherStuff(userID, new SuccessDelegate() { @Override public void onSuccess() { Log.e(TAG, "Called delegate!!!! "+ userID); } }); } I get this error: Cannot refer to a non-final variable userID inside an inner class defined in a different method I'm pretty sure I can't assign it as final since it's a variable with an unknown value. I had heard that this syntax

how refer to a local variable share same name of a global variable in C? [duplicate]

家住魔仙堡 提交于 2019-12-17 18:56:40
问题 This question already has answers here : How to print value of global variable and local variable having same name? (4 answers) Closed 2 years ago . for example #include<stdio.h> int foo = 100; int bar() { int foo; /* local foo = global foo, how to implemented? */ return 0; } int main() { int result = bar(); return 0; } I think in the function bar, calling foo directly will just get the global foo. How can I refer the local foo? I know in C++, there is this pointer. However, does C has

c++ using declaration, scope and access control

ぐ巨炮叔叔 提交于 2019-12-17 18:56:36
问题 Typically the 'using' declaration is used to bring into scope some member functions of base classes that would otherwise be hidden. From that point of view it is only a mechanism for making accessible information more convenient to use. However: the 'using' declaration can also be used to change access constraints (not only for functions but also for attributes). For example: class C{ public: int a; void g(){ cout << "C:g()\n"; } C() : a(0){} }; class D : public C{ private: using C::a; using

Objective-C: instance variables out of scope in debugger

和自甴很熟 提交于 2019-12-17 18:44:04
问题 I have a superclass and a subclass, both of which define instance variables. Rough outline of superclass: /* GenericClass.h */ @interface GenericClass : NSObject { /* some variables */ } @end /* GenericClass.m */ @implementation GenericClass /* ... */ @end Outline of subclass: /* SpecificClass.h */ #import "GenericClass.h" @interface SpecificClass : GenericClass { NSMutableString *str; } /* SpecificClass.m */ #import "SpecificClass.h" @implementation SpecificClass - (void)aMethod { //Debugger

function arguments

好久不见. 提交于 2019-12-17 18:41:56
问题 function Foo(f) { var f = f; } Here inside the function, variable f is local to the Foo (it has a function scope), but why is the variable f in the argument list not in conflict? Maybe because it is bound inside the Foo.arguments object? In other languages we cannot declare an argument variable with the same name as a local variable. How is this name ambiguity resolved? Or, How do you reference each of the two distinct f variables later in the method? 回答1: JavaScript does a couple of things

Nested subroutines and Scoping in Perl

半腔热情 提交于 2019-12-17 18:29:15
问题 I'm writing Perl for quite some time now and always discovering new things, and I just ran into something interesting that I don't have the explanation to it, nor found it over the web. sub a { sub b { print "In B\n"; } } b(); how come I can call b() from outside its scope and it works? I know its a bad practice to do it, and I dont do it, I use closured and such for these cases, but just saw that. 回答1: Subroutines are stored in a global namespace at compile time. In your example b(); is

AngularJS: How to pass arguments/functions to a directive?

大憨熊 提交于 2019-12-17 17:50:08
问题 Look at this Fiddle, what do I have to change, that the expressions in the template get evaluated using the arguments I defined in the HTML? The SAVE-button should call the blabla() -function of the controller, since I pass it? var myApp = angular.module('MyApp',[]) myApp.directive('editkeyvalue', function() { return { restrict: 'E', replace: true, scope: { accept: "expression" }, template : '<div><label class="control-label">{{key}}</label>' + '<label class="control-label">{{key}}</label>' +

Javascript scope addEventListener and this

℡╲_俬逩灬. 提交于 2019-12-17 17:34:56
问题 I am a C# developer experimenting with JavaScript and I'm trying to get my head around the scope :) I have the following code which contains an addEventListener in which I want to use a field from my object: (function(window) { function Keyboard() { this.keys = {}; } Keyboard.prototype.handle_keydown = function(args) { this.keys[args.keyCode] = true; } Keyboard.prototype.listen = function() { window.addEventListener('keydown', this.handle_keydown); } app.util.keyboard = new Keyboard(); })