我得到了这种情况我无法理解阴影.例如以下代码
class Foo {
int a = 5;
void goFoo(int a) {
// No problem naming parameter as same as instance variable
for (int a = 0; a < 5; a++) { }
//Now the compiler complains about the variable a on the for loop
// i thought that the loop block had its own scope so i could shadow
// the parameter, why the compiler didnt throw an error when i named
// the parameter same as the instance …Run Code Online (Sandbox Code Playgroud) 我在ruby以及powershell编程中看到我们可以分配变量,如a,b = b,a.它实际上交换了变量.
如果这样可以在f#中使用,请引导我参考一下
C#中方法隐藏和阴影有什么区别?它们是相同还是不同?我们可以将它们称为多态(编译时或运行时)吗?
我希望在基类中有一个方法调用一个纯虚方法,该方法将在派生类中实现.但是,基类无参数方法似乎不会被派生类继承.我究竟做错了什么?编译器是MSVC12.
错误C2660:'Derived :: load':函数不带0个参数
这是一个完整的例子(由于错误而无法编译):
struct Base
{
void load() { load(42); }; // Making this virtual doesn't matter.
virtual void load(int i) = 0;
};
struct Derived : Base
{
virtual void load(int i) {};
};
int main()
{
Derived d;
d.load(); // error C2660: 'Derived::load' : function does not take 0 arguments
}
Run Code Online (Sandbox Code Playgroud) 我正在关注这个关于Java中的HashMap的视频.它有以下代码.
// Create the HashMap
HashMap<String,String> hm = new HashMap<String, String>();
// Put data
hm.put("Katie", "Android, WordPress");
hm.put("Magda", "Facebook");
hm.put("Vanessa", "Tools");
hm.put("Ania", "Java");
hm.put("Ania", "JEE"); // !! Put another data under the same key, old value is overridden
// HashMap iteration
for (String key: hm.keySet())
System.out.println(key+":"+hm.get(key));
Run Code Online (Sandbox Code Playgroud)
所以我写了下面的代码,用它来练习HashMap(几乎相同的代码)
package hashmap;
import java.util.*;
public class HashMap {
public static void main(String[] args) {
HashMap<String,String> hm = new HashMap<String, String>();
hm.put("Katie", "Android, WordPress");
hm.put("Magda", "Facebook");
hm.put("Vanessa", "Tools"); …Run Code Online (Sandbox Code Playgroud) 我是java的新手,我在下面的例子中感到困惑
public class Test {
int testOne(){ //member method
int x=5;
class inTest // local class in member method
{
void inTestOne(int x){
System.out.print("x is "+x);
// System.out.print("this.x is "+this.x);
}
}
inTest ins=new inTest(); // create an instance of inTest local class (inner class)
ins.inTestOne(10);
return 0;
}
public static void main(String[] args) {
Test obj = new Test();
obj.testOne();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我无法使用第8行中的"this"关键字访问inTestOne()方法中的shadowed变量
由于复制/粘贴,我在自己的代码中发现了一个错误.相同的值名称由同一范围内的复制/粘贴进行遮蔽.
let func() =
let a = 1
let a = something_else
....
Run Code Online (Sandbox Code Playgroud)
在C#中我不会通过编译.有没有办法禁用阴影?至少在相同的范围内?
谢谢
在阅读有关嵌套类的Oracle文档时,我发现这段代码的输出我无法理解.有人可以解释一下吗?
public class ShadowTest {
public int x = 0;
class FirstLevel {
public int x = 1;
void methodInFirstLevel(int x) {
System.out.println("x = " + x);
System.out.println("this.x = " + this.x);
System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
}
}
public static void main(String... args) {
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是此示例的输出:
x = 23
this.x = 1
ShadowTest.this.x = 0 //why is 0 printed here? why not 1 because "this" is the …Run Code Online (Sandbox Code Playgroud) 如果我编写以下F#代码,编译器会发出错误.
let a = 123
let a = 123
Run Code Online (Sandbox Code Playgroud)
产生的错误是:
错误FS0037:值'a'的重复定义
如果我在这样的函数中编写相同的代码:
let fctn =
let a =123
let a =123
a
Run Code Online (Sandbox Code Playgroud)
它不会产生任何错误.
我不明白其中的区别.有人可以解释一下吗?
编辑:我在模块级写的第一个代码.
这是有问题的代码片段:
package main
import (
"fmt"
)
var a string = "hello"
func main() {
b := "world"
fmt.Println(a, b)
a := "bye"
fmt.Println(a, b)
}
Run Code Online (Sandbox Code Playgroud)
输出:
hello world
bye world
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何解决“全局”和“本地”变量之间的名称冲突a?
更具体地说,我如何告诉 Goa使用哪个?
shadowing ×10
java ×4
f# ×3
polymorphism ×2
scope ×2
c# ×1
c++ ×1
go ×1
inheritance ×1
overloading ×1
variables ×1