From: ttate@... Date: 1998-04-15T20:40:22+09:00 Subject: [ruby-list:7511] Re: クラスの変更 立石です。 ogino@coop.nagoya-u.ac.jpさん(04月15日20時): ogino>>荻野といいます。すみませんが、ご指導ください。プログラミング初心者です。 ogino>> ogino>>大変馬鹿なことを考えているような気がしますが、あるオブジェクトが、自分自 ogino>>身のクラスを変更することというのは可能なのでしょうか? refrectionかと思った、、、、、(^^; ogino>># Java や C++ など、変数が型を持っていれば明らかにだめだと分かりますが。 スーパークラスが同じならJava,C++でも下記の ようなことは可能です。 class Dog def barg printf("うぅ〜、、、\n") end end class LargeDog < Dog def barg super printf("ばうばう!\n") end end class Littledog < Dog def barg super printf("きゃん!\n") end end dog = LittleDog.new dog.barg dog = LargeDog.new dog.barg これで御希望の処理ができると思いますが、、、、 ;; 見当違いなことだったらすいません。 ちなみにC++だと class Dog { public: Dog(){}; void barg() { .....; }; }; class LargeDog : Dog { public: LargeDog():Dog(){}; void barg() { Dog::barg(); .....; }; }; class LittleDog : Dog { .....(同様)..... }; で Dog dog = new Dog; dog.barg(); dog = new LargeDog; dog.barg(); dog = new LittleDog; dog.barg(); とすれば似たようなことが可能です。 (メモリ管理が必要ですが、、、) ---------------------------------------------- Name Takaaki Tateishi E-Mail ttate@jaist.ac.jp URL http://www.pluto.dti.ne.jp/~tateishi/ Org School of Information Science Japan Advanced Institute of Science and Technology (JAIST)