如示例所示,传递接口引用或类引用之间有什么区别;
Interface MyInterface
{
void foo();
}
public class MyClass implements MyInterface
{
public void foo()
{
doJob();
}
}
....
//in another class or etc..
public void case1(MyClass mc)
{
mc.foo();
}
public void case2(MyInterface mi)
{
mi.foo();
}
....
//In somewhere
MyClass mc = new MyClass();
case1(mc);
case2(mc);
Run Code Online (Sandbox Code Playgroud)
case1和case2之间的主要区别是什么?它们在性能,可见性,保护对象免受非法使用方面是否具有任何优势?像这样使用它有什么缺点吗?
在cstring.h文件中存在一个函数:
int strcmp ( const char *s1, const char *s2 ),但为什么只有数据是常量,使指针和数据都不变更安全.在我看来,正确的函数版本应该是这样的:
int strcmp ( const char * const s1, const char * const s2 )
Run Code Online (Sandbox Code Playgroud)