免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2413 | 回复: 12
打印 上一主题 下一主题

找错,一个简单java程序,讲下原因吧 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-23 18:26 |只看该作者 |倒序浏览
class TestP{
    public void t1(){
       System.out.println("method from F's t1()");
    }
}
class TestC extends TestP{
    public void t1(){
        System.out.println("method from C's t1()");
    }

    public void ct1(){
         System.out.println("method from C's ct1()");
    }

    public static void main(String args[]){
        TestP tp = new TestC();
        tp.t1();
        tp.ct1();
    }
}

[ 本帖最后由 uneting 于 2005-11-23 18:28 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2005-11-23 18:44 |只看该作者
这样编译能通过么?

论坛徽章:
0
3 [报告]
发表于 2005-11-23 19:37 |只看该作者
通不过,怎么改捏,为何?

论坛徽章:
0
4 [报告]
发表于 2005-11-23 21:35 |只看该作者
居然比我还菜。。。

论坛徽章:
0
5 [报告]
发表于 2005-11-23 21:37 |只看该作者
内部类不能有静态声明!===》public static void main(String args[]){
实例化一个对象写错      ===》TestP tp = new TestC();
还有为main方法声明一个类。修改如下:

class TestP{
    public void t1(){
       System.out.println("method from F's t1()");
    }
}
class TestC extends TestP{
    public void t1(){
        System.out.println("method from C's t1()");
    }


    public void ct1(){
         System.out.println("method from C's ct1()");
    }
}
public class Test {
    public static void main(String args[]){
        TestC tp = new TestC();
        tp.t1();
        tp.ct1();
    }
}

输出结果为:
method from C's t1()
method from C's ct1()
lpnas 该用户已被删除
6 [报告]
发表于 2005-11-24 09:20 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
7 [报告]
发表于 2005-11-24 15:25 |只看该作者
原帖由 lpnas 于 2005-11-24 09:20 发表
在p类中没有定义ct1的方法
在之后把tp定义成p类型后就不可能调用ct1

同意,可以调用在p里面声明的方法,没有声明的不能调用!

论坛徽章:
0
8 [报告]
发表于 2005-11-24 15:36 |只看该作者
原帖由 wsyngj 于 2005-11-23 21:37 发表
内部类不能有静态声明!===》public static void main(String args[]){
实例化一个对象写错      ===》TestP tp = new TestC();
还有为main方法声明一个类。修改如下:

你的修改试乎把楼主程序的本意改变了

class testC是testP的一个子类, 不是内部类;
testP tp =new TestC();这种声明也没错,多态
只要把tp.ct1()注释掉就可以了

论坛徽章:
0
9 [报告]
发表于 2005-11-24 23:30 |只看该作者
原帖由 madebynt 于 2005-11-24 15:36 发表

你的修改试乎把楼主程序的本意改变了

class testC是testP的一个子类, 不是内部类;
testP tp =new TestC();这种声明也没错,多态
只要把tp.ct1()注释掉就可以了


怎么没注意多态呢。。。

[ 本帖最后由 wsyngj 于 2005-11-24 23:40 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2005-11-25 00:09 |只看该作者
那个不是接口..= =继承的没法这个样子搞.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP