| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1953 人关注过本帖
标题:子类的子类能不能继承父类的父类的方法和字段?
取消只看楼主 加入收藏
chengsencs
Rank: 1
等 级:新手上路
帖 子:109
专家分:0
注 册:2006-7-24
收藏
 问题点数:0 回复次数:0 
子类的子类能不能继承父类的父类的方法和字段?


public class S
{
public static void main(String[] args)
{

ClassManage monitor = new ClassManage("Tom", "20031009", "班长");
ClassPE player= new ClassPE("CS","333","组长","选手");
Student[] student = new Student[4];


student[0] = monitor;
student[1] = player;
student[2] = new Student("Jack", "2003100901");
student[3] = new Student("Rose", "2002091002");

for(int i = 0; i <student.length; i++)
{
System.out.println(student[i].toString());
}
}
}

class Student
{
private String strName = "";
private String strNumber = "";
private String strSex = "";
private String strBirthday = "";
private String strSpeciality = "";
private String strAddress = "";

public Student(String name, String number)
{
strName = name;
strNumber = number;
}

public String getStudentName()
{
return strName;
}

public String getStudentNumber()
{
return strNumber;
}

public void setStudentSex(String sex)
{
strSex = sex;
}

public String getStudentSex()
{
return strSex;
}

public String getStudentBirthday()
{
return strBirthday;
}

public void setStudentBirthday(String birthday)
{
strBirthday = birthday;
}

public String getStudentSpeciality()
{
return strSpeciality;
}

public void setStudentSpeciality(String speciality)
{
strSpeciality = speciality;
}

public String getStudentAddress()
{
return strAddress;
}

public void setStudentAddress(String address)
{
strAddress = address;
}

public String toString()
{
String information = "学生姓名=" + strName + ", 学号=" + strNumber;
if( !strSex.equals("") )
information += ", 性别=" + strSex;
if( !strBirthday.equals(""))
information += ", 出生年月=" + strBirthday;
if( !strSpeciality.equals("") )
information += ", 专业=" + strSpeciality;
if( !strAddress.equals("") )
information += ", 籍贯=" + strAddress;
return information;
}
}


class ClassManage extends Student
{
private String strDuty = "";

public ClassManage(String name, String number, String strDuty)
{
super(name, number);
this.strDuty = strDuty;
}

public String getDuty()
{
return strDuty;
}


public String toString()
{
String str = super.toString();
if(!strDuty.equals(""))
{
str += ", 职务=" + strDuty;
}

return str;
}
}

class ClassPE extends ClassManage
{
private String strPE ="";
public ClassPE(String name,String number,String strDuty, String strPE);
{
super(name,number,strDuty);
this.strPE=strPE;
}
public String getPE()
{
return strPE;
}
public String toString()
{
String str= super.toString();
if(!strPE.equals(""))
{
str +=",体=" + strPE;
}
return str;
}
}

最后一个类是我完全按照上一个类写的,为什么可编译,无法运行?
ClassPE可以继承Student的方法吗?

D:\>javac S.java
S.java:143: missing method body, or declare abstract
public ClassPE(String name,String number,String strDuty, String strPE);
^
S.java:145: call to super must be first statement in constructor
super(name,number,strDuty);
^
2 errors

谢谢。

搜索更多相关主题的帖子: 子类 父类 字段 继承 
2007-02-03 22:09
快速回复:子类的子类能不能继承父类的父类的方法和字段?
数据加载中...
 
   



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

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