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

您没有登录

» Java开发网 » Java SE 综合讨论区 » 学习心得/方法/资源  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教一下继承中的构造方法。
enen000





发贴: 1
积分: 0
于 2008-09-15 23:05 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
正在学习JAVA,对继承中的构造方法的概念和使用有一点模糊。希望高手能够实例指教一下,辛苦大家了。非常感谢。


作者 Re:请教一下继承中的构造方法。 [Re:enen000]
xuxiaolei

^_^



发贴: 169
积分: 23
于 2008-09-16 07:38 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
不知道是不是指的是这个意思,看下面的代码,在子类的构造函数中通过super(name)来调用父类的构造函数

public class People {

public People() {
}

public People(String name) {
this.name = name;
}

private String name;

}

public class Person extends People {

public Person() {
}

public Person(String name, int age) {
super(name);
this.age = age;
}

private int age;
}


xuxiaolei edited on 2008-09-16 07:41

http://xuxiaolei.javaeye.com
作者 Re:请教一下继承中的构造方法。 [Re:enen000]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-09-18 18:19 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
To xuxiaolei,

People and Person are meant to be the same meaning in English, I think. Maybe a better example will be People ----> Animal . Smile

Jiafan



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:请教一下继承中的构造方法。 [Re:enen000]
xuxiaolei

^_^



发贴: 169
积分: 23
于 2008-09-19 21:19 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
sorry, my english is poor.


http://xuxiaolei.javaeye.com
作者 Re:请教一下继承中的构造方法。 [Re:enen000]
690716494





发贴: 4
积分: 0
于 2008-09-26 23:06 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
sorry ,my english is very poor.


作者 Re:请教一下继承中的构造方法。 [Re:enen000]
andy_wang_5





发贴: 101
积分: 14
于 2008-09-27 09:23 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
my english is poorest, Cry


鹰行折翼 再盼凌云
作者 Re:请教一下继承中的构造方法。 [Re:enen000]
primoc





发贴: 2
积分: 0
于 2008-10-07 16:35 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
谈一点自己的拙见和理解
首先需要搞清楚初始化的顺序
再理解继承中父类与子类的关系就容易一些了
---------
从正常的概念上来讲,一定是先有父再有子,没有父类也就不存在子类
继承也是这个道理,在初始化的过程中,一定是先调用父类的构造方法创建出父类的对象,子类才有东西去继承,子类才会存在
所以当一个类B继承一个类A的时候 class B extends A{}
再执行类B的构造方法时,首先调用的是其父类A的构造方法,在没有显式的调用父类构造方法的时候,程序会默认调用父类的无参构造方法
public B(){
super() ;
}
super关键字是表示一个指向父类对象的引用,通过这个引用调用父类的无参构造方法创建出一个父类的对象,然后再执行子类的构造方法创建出子类对象,这里写不写super()调用效果都是一样的,没有写的情况下,程序会自动调用父类的无参构造方法

还有一点要注意的是如果父类中重载了构造方法,也就是没有显式的提供无参构造函数,则在子类中不能由程序默认去调用无参的父类构造函数,解决的方法是给父类手动添加一个无参的构造函数,或者在子类中显式的调用父类有参的构造函数

自己写一个简单的小程序就明白了!!

class Person{
  String name ;
  int age ;

  public Person(){
    System.out.println("父类无参构造") ;
  }

  public Person(String name){
    this.name = name ;
    System.out.println("父类有参构造") ;
  }
}

class Student extends Person{
  public Student(){
    System.out.println("子类无参构造") ;
  }
}

public class JavaTest01{
  public static void main(String[] args){
    Student s = new Student() ;
  }
}




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