【发布时间】:2014-01-27 03:18:39
【问题描述】:
我是编码的初学者,我一直在寻找整个互联网,并没有真正出来,并给我一个关于如何使用扩展方法以及如何正确使用 super( ) 方法。我有点坚持这一点,想知道是否有人可以展示或给我一个很好的例子。我的项目是定义一个类 Diploma 及其子类 DiplomaWithHonors,以便一些陈述显示他们的学术成就。谢谢你的帮助!好吧,既然我对这个问题进行了“刺伤”,那么如果有人可以帮助解决这个问题,我的 return 语句就会出错......
public class Diploma
{
public String fullName;
public String course;
int output = Diploma();
public Diploma(String name, String info)
{
fullName = name;
course = info;
}
public String toString()
{
return "This certifies that \n" + fullName + " \n has completed a course in " + course;
}
}
public class DiplomaWithHonors extends Diploma
{
public DiplomaWithHonors( String name, String info )
{
super( name, info );
}
public String toString()
{
return "This certifies that \n" + fullName + " \n has completed a course in " + course+ "\n*** with honors ***";
}
}
public String toString()
{
Diploma[] diplomas = new Diploma[2];
diplomas[ 0 ] = new Diploma("Murray Smith", "Gardening");
diplomas[ 1 ] = new DiplomaWithHonors("Lisa Smith", "Evolutionary Psychology");
for( int i = 0; i< diplomas.length; i++)
{
System.out.println( diplomas[ i ] );
}
}
return output;
}
P.S 很抱歉样式故障,我有点搞砸了复制粘贴。
【问题讨论】:
-
如果编码适合您,也许您应该尝试编写一些代码。尝试解决问题,然后在遇到实际问题时再回来。
-
class B extends A { ... }。如果要调用父构造函数,super()必须是子构造函数中的第一条语句。如果父级具有参数化构造函数,您可以在super(param1, param2, ...);statement 中传递这些参数。如果要调用父级的特定方法,请使用super.methodOfParentToInvoke(...),其中methodOfParentToInvoke(...)是您要调用的父级方法 -
感谢@Radiodef 和 Roman 提供的示例,他们让我们更好地理解超类和子类。 Nathaniel 如果我有任何想法,我会的,但如果出现问题,我会确保发布它。
标签: java class subclass superclass super