Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 新人求证继承基本问题一例,望高人指点
gumuyang





发贴: 21
积分: 0
于 2007-01-31 09:40 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
class Father{
int x=0,y=0;
Father(){
System.out.println("now is in Father's constructor");
}
Father(int x,int y){
this.x = x;
this.y = y;
}
public void print(){
System.out.println("x="+x+";y="+y);
}
}
class Son extends Father{
Son(){
System.out.println("now is in Son's constructor");
}
Son(int x,int y){
super(x,y);
//this(); //调用自己的构造器
}
public void print(){
super.print();
System.out.println("My name is son!");
}
}
public class Example4_13{
public static void main(String[] args){
//通过默认构造器创建实例
Son s = new Son();
s.print();
//通过其他构造器创建实例
Son f = new Son(10,20);
f.print();
}
}

结果显示如下:
now is in Father's constructor
now is in Son's constructor
x=0;y=0
My name is son!
x=10;y=20
My name is son!

我对程序运作的认识如下:
从public class Example4_13{这一行开始是主程序
令我困惑的主要有两个问题:

一、子类继承父类时,为什么要把父类的所有程序段都执行一遍,而不是在s.print()只执行*.print()里的程序?我知道这是规范,但我很想知道为什么会这样规范,如果有人能描述一遍规范,我会非常感激!

二、从结果看来,好像父类和子类的内容是交替实现的,为什么不是先把父类的所有结果都实现一遍并显示,然后再显示子类的结果呢?

我知道这里新人很多,我也是其中一个,免不了又要给高手添麻烦。之前我也在这里静静地看过很多帖子,也下载了thinking in java,但作为只有薄弱的c语言基础的新人,第一章就让我看得云山雾罩,真的好希望身边有个高手,能边看边问,那该多好啊,我知道这很奢侈,只是说说心里话而已。




话题树型展开
人气 标题 作者 字数 发贴时间
7386 新人求证继承基本问题一例,望高人指点 gumuyang 1200 2007-01-31 09:40
5680 Re:新人求证继承基本问题一例,望高人指点 嚼花仔 98 2007-01-31 10:39
5637 Re:新人求证继承基本问题一例,望高人指点 cxp108 198 2007-01-31 13:50
5835 Re:新人求证继承基本问题一例,望高人指点 kyowu 36 2007-01-31 14:29
5694 Re:新人求证继承基本问题一例,望高人指点 嚼花仔 117 2007-01-31 14:54
6086 Re:新人求证继承基本问题一例,望高人指点 shinegmd 162 2007-01-31 17:24

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923