| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 463 人关注过本帖
标题:[求助]编译时提示找不到方法
只看楼主 加入收藏
jmasm
Rank: 1
等 级:新手上路
威 望:1
帖 子:368
专家分:0
注 册:2006-9-24
收藏
 问题点数:0 回复次数:2 
[求助]编译时提示找不到方法

class Shoes{
void walk(Shoes shoes){ //行走
if (shoes instanceof Leather){
shoes=(Leather)shoes;
shoes.land();
System.out.println("111");
}
if(shoes instanceof Sandal){
//shoes=(Sandal)shoes;
//shoes.water();
System.out.println("2222");
}
}
}

class Leather extends Shoes{ //皮鞋
void land(){
System.out.println("我可以在陆地行走.");
}
}
class Sandal extends Shoes{ //凉鞋
void water(){
System.out.println("我可以在水中行走.");
}
}

class ShoesTest{
public static void main(String[] args){
Shoes shoes=new Shoes();
Leather l=new Leather();
Sandal s=new Sandal();
shoes.walk(l);
shoes.walk(s);
}
}

编译时提示找不到land方法?
怎么回事?

搜索更多相关主题的帖子: 陆地 shoes Leather Shoes Sandal 
2007-06-14 19:57
狂放不羁
Rank: 4
等 级:贵宾
威 望:12
帖 子:925
专家分:0
注 册:2007-1-24
收藏
得分:0 

因为你的Shoes类中没有定义land()方法。。要想把子类向上转型为基类的引用,那么那个方法应该是被子类覆盖的。。建议LZ少使用instanceof,因为这是与多态相违背的,完全可以用重载来实现你所想要的功能。。只是一点小建议。不对的地方请兄弟们包含。。

class Shoes{
void walk(Leather shoes){ //行走


shoes.land();
System.out.println("111");

}

void walk(Sandal shoes){
shoes.water();

}
}

class Leather extends Shoes{ //皮鞋
void land(){
System.out.println("我可以在陆地行走.");
}
}
class Sandal extends Shoes{ //凉鞋
void water(){
System.out.println("我可以在水中行走.");
}
}

class ShoesTest{
public static void main(String[] args){
Shoes shoes=new Shoes();
Leather l=new Leather();
Sandal s=new Sandal();
shoes.walk(l);
shoes.walk(s);
}
}


2007-06-14 20:27
jmasm
Rank: 1
等 级:新手上路
威 望:1
帖 子:368
专家分:0
注 册:2006-9-24
收藏
得分:0 
谢谢大哥
受教了
2007-06-14 21:11
快速回复:[求助]编译时提示找不到方法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011728 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved