我仍然对Objective-C感到困惑.有时您会声明一个变量:
NSRect rect;
Run Code Online (Sandbox Code Playgroud)
有时是这样的:
NSValue *value;
Run Code Online (Sandbox Code Playgroud)
我永远不知道何时添加*,到目前为止我总是在Apple的文档中查找它.我知道值和指向对象的指针之间的区别.
但是,当我声明一个值并且当我声明一个指针时,是否有任何硬性规定?什么让它更容易记住?或者我必须知道(例如,查找)哪个NSSomething是值,哪个是对象?
谢谢!
我最近在Java中做了一些sax解析.我注意到命名空间映射的初始化
private Map<String,String> namespaceMappings = new HashMap<String,String>();
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么我们在这里创建一个Map变量而不是HashMap?
此外,如果我要使用getNamespaceMappings字段,我应该返回什么类型?
public Map<String,String> getNamespaceMappings() {
return namespaceMappings;
}
Run Code Online (Sandbox Code Playgroud)
要么
public HashMap<String,String> getNamespaceMappings() {
return (HashMap<String,String>) namespaceMappings;
}
Run Code Online (Sandbox Code Playgroud)
要么
public Map<String,String> getNamespaceMappings() {
return (HashMap<String,String>) namespaceMappings;
}
Run Code Online (Sandbox Code Playgroud) 我一直在研究GCC源代码,我注意到大多数(如果不是全部)特定于目标的源代码文件(例如arm.c)使用旧的K&R函数样式.
这有什么特别的原因吗?向后兼容性?可移植性?
在我的方法中,我声明了一些变量,包括int blockCount;.我不止一次称这种方法.使用Xcode调试器,我发现在第二次调用该方法之后,将值blockCount设置为364265,而将其设置为2,仅提前几毫秒.
这不是一个真正的问题,因为我可以将它设置为0或我想要的任何其他数字,但是一次又一次地声明某个变量是不好的编程习惯?我对编程很陌生,我想确保我正确地做事.:)
在下面的代码中,为什么全局变量"x"的定义显示警告"数据定义没有类型或存储类",但同样的事情适用于局部变量"y"?我正在为每个变量做什么首先在一个语句中声明它们,然后在另一个语句中定义它们.它对一个语句有效但是对另一个语句显示警告的区别是什么?
#include<stdio.h>
int x;
x=303;
int main(void)
{
int y;
y=776 ;
printf("The value of x is %d,and of y is %d",x,y);
}
Run Code Online (Sandbox Code Playgroud) 我试图理解无点风格的概念.我做了一个函数尝试使用添加两个值uncurry.
add = (+) . uncurry
Run Code Online (Sandbox Code Playgroud)
并且结果抱怨:
No instance for (Num ((a0, b0) -> c0))
arising from a use of `+'
Possible fix:
add an instance declaration for (Num ((a0, b0) -> c0))
In the first argument of `(.)', namely `(+)'
In the expression: (+) . uncurry
In an equation for `add': add = (+) . uncurry
Run Code Online (Sandbox Code Playgroud)
这是一个声明问题吗?我试过add :: (Int, Int) -> Int,它不起作用.
我上课NumberArray了NumberArray.h
class NumberArray
{
private:
double *aPtr;
int arraySize;
public:
NumberArray(int size, double value);
// ~NumberArray() { if (arraySize > 0) delete [ ] aPtr;}
//commented out to avoid problems with the
//default copy constructor
void print() const;
void setValue(double value);
};
Run Code Online (Sandbox Code Playgroud)
当我去NumberArray.cpp中编写print函数时
void NumberArray::print()
{
for (int index = 0; index < arraySize; index++)
cout << aPtr[index] << " ";
}
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误
声明与"void NumberArray :: print()const不兼容"
有什么想法,我可能会出错吗?其余的构造函数和类函数工作正常.
问题是标题所示.O得到错误:期望的类型标识符,(我认为)来自在类型声明之前读取的函数声明.
type
TForm1 = class(TForm)
//other declarations
function ItemThere(x: TItem): boolean; // here it says TItem is unknown
end;
type
TItem = class
Name : String;
Description : String;
constructor Create;
end;
Run Code Online (Sandbox Code Playgroud)
另外,我想让你知道我是一个相当缺乏经验的程序员.我怎样才能解决这个问题?我应该将TItem的声明移到TForm1之上吗?谢谢您的帮助.
我已经知道如何HashMap使用以下两种方法之一来初始化Java
// way 1: apply generic type saftey
HashMap<String, Integer> hashMap1 = new HashMap<String, Integer>();
// way 2: general without apply generic type saftey
HashMap<String, Integer> hashMap2 = new HashMap();
Run Code Online (Sandbox Code Playgroud)
我的问题
什么是最佳做法
根据Eclipse Marker
类型安全:HashMap类型的表达式需要未经检查的转换以符合HashMap
new HashMap<String, Integer>();
Run Code Online (Sandbox Code Playgroud)
但根据Sonar Linter的说法
使用菱形运算符("<>")替换此构造函数调用中的类型规范.
new HashMap();
Run Code Online (Sandbox Code Playgroud)
哪一个是最好的?为什么?
我正在尝试以不同的方式在C中声明字符串以准备考试。我们知道C中的字符串是'\0'结尾处的字符数组。然后我发现,即使我声明了一个5个字符的数组并将5个字符放入其中,例如“ abcde”,它也被接受。那么,空字符存储在哪里?
我以以下方式声明了字符串
char str[] = {'a','b','c','d','e'};
char str2[] = "abcde";
char str3[5] = "abcde";
Run Code Online (Sandbox Code Playgroud)
现在,在第3种情况下,我要分配5个字节的空间,并且数组中恰好有5个字符,那么如果string的末尾应将空字符存储在哪里?还是不附加null的情况?第一种情况和第二种情况如何,那里是否附加了null?
strlen()在所有3种情况下均返回5。
declaration ×10
c ×2
java ×2
objective-c ×2
c++ ×1
class ×1
cocoa ×1
collections ×1
delphi ×1
expression ×1
function ×1
gcc ×1
hashmap ×1
haskell ×1
interface ×1
lazarus ×1
methods ×1
object ×1
pointfree ×1
return-type ×1
sonarlint ×1
string ×1
types ×1
variables ×1
xcode ×1