to bdsc() 這是我試過的﹐你不信試試to 0flying0(j2ee的狂热分子)我說的同名方法不是虛函數的那種﹐是指在子類和父類中參數不同的那種同名方法
c++隱藏代碼//: C14:NameHiding.cpp // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Hiding overloaded names during inheritance #include <iostream> #include <string> using namespace std;class Base { public: int f() const { cout << "Base::f()\n"; return 1; } int f(string) const { return 1; } void g() {} };class Derived1 : public Base { public: void g() const {} };class Derived2 : public Base { public: // Redefinition: int f() const { cout << "Derived2::f()\n"; return 2; } };class Derived3 : public Base { public: // Change return type: void f() const { cout << "Derived3::f()\n"; } };class Derived4 : public Base { public: // Change argument list: int f(int) const { cout << "Derived4::f()\n"; return 4; } };int main() { string s("hello"); Derived1 d1; int x = d1.f(); d1.f(s); Derived2 d2; x = d2.f(); //! d2.f(s); // string version hidden Derived3 d3; //! x = d3.f(); // return int version hidden Derived4 d4; //! x = d4.f(); // f() version hidden x = d4.f(1); } ///:~ java 名字隱藏代碼 package e20040621; public class a{ public void method(){ System.out.println("a.method()"); } }
package e20040621; public class b extends a{ public void method(int i){ System.out.println("b.method()"); } }
package e20040621; public class methodtest{ public static void main(String args[]){ b bmethod=new b(); bmethod.method(); //不會隱藏父類的 method(); bmethod.method(3); } }
這是我試過的﹐你不信試試to 0flying0(j2ee的狂热分子)我說的同名方法不是虛函數的那種﹐是指在子類和父類中參數不同的那種同名方法
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Hiding overloaded names during inheritance
#include <iostream>
#include <string>
using namespace std;class Base {
public:
int f() const {
cout << "Base::f()\n";
return 1;
}
int f(string) const { return 1; }
void g() {}
};class Derived1 : public Base {
public:
void g() const {}
};class Derived2 : public Base {
public:
// Redefinition:
int f() const {
cout << "Derived2::f()\n";
return 2;
}
};class Derived3 : public Base {
public:
// Change return type:
void f() const { cout << "Derived3::f()\n"; }
};class Derived4 : public Base {
public:
// Change argument list:
int f(int) const {
cout << "Derived4::f()\n";
return 4;
}
};int main() {
string s("hello");
Derived1 d1;
int x = d1.f();
d1.f(s);
Derived2 d2;
x = d2.f();
//! d2.f(s); // string version hidden
Derived3 d3;
//! x = d3.f(); // return int version hidden
Derived4 d4;
//! x = d4.f(); // f() version hidden
x = d4.f(1);
} ///:~
java 名字隱藏代碼
package e20040621;
public class a{
public void method(){
System.out.println("a.method()");
}
}
package e20040621;
public class b extends a{
public void method(int i){
System.out.println("b.method()");
}
}
package e20040621;
public class methodtest{
public static void main(String args[]){
b bmethod=new b();
bmethod.method(); //不會隱藏父類的 method();
bmethod.method(3);
}
}
這個為什么不
因为java语言的设计不想让java像c++一样的复杂,而使得只有聪明的人才能掌握
-----------------------------------
在c++和java中子類如果有和父類相同的方法(指方法名和参数类型、个数都完全相同)那么它们都会會把父類的相同方法覆蓋掉
都是面向对象的语言,都能实现覆盖。