我是一名编码的初学者,我一直在互联网上寻找并没有真正的结果,并给我一个如何使用extend方法以及如何正确使用 super的示例( )方法。我有点坚持这一点,并想知道是否有人可以展示或给我一个很好的例子。我的项目是定义一个班级文凭及其子类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对于风格故障感到抱歉,我对复制粘贴搞砸了。
答案 0 :(得分:2)
super
不是一种方法; it's a keyword.在某些情况下,它可能被称为或使用作为方法,但它实际上是一个关键字。上面的链接将为您提供大量有用的信息,以及继承的一些花絮。
除此之外,它听起来是两个实体之间的直接关系:
Diploma
应该是您正在使用的基础实体;它包含收件人姓名,GPA以及他们收到的学位等信息。
对于不同的GPA,DiplomaWithHonors
听起来是不同类型的荣誉。
非常原始结构可能看起来像这样。假设字段已定义。
public class Diploma {
public Diploma(String name, double GPA, String degreeField, DiplomaType diplomaType) {
this.name = name;
this.GPA = GPA;
this.degreeField = degreeField;
this.diplomaType = diplomaType;
}
}
public class DiplomaWithHonors extends Diploma {
public DiplomaWithHonors(String name, double GPA, String degreeField, DiplomaType diplomaType) {
super(name, GPA, degreeField, diplomaType);
calculateHonors();
}
}
请注意,构造函数上下文中的super
必须是该构造函数中引用的第一个事物。
答案 1 :(得分:0)
查看此示例的toString()方法
import java.util.List;
public class Diploma {
private String student;
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
@Override
public String toString() {
return "Diploma [student=" + student + "]";
}
public static void main(String[] args){
DiplomaWithHonors d1 = new DiplomaWithHonors();
d1.setStudent("John");
String[] achievements = {"Golden Medal","Silver Medal"};
d1.setAchievements(achievements);
System.out.println(d1);
DiplomaWithoutAnyHonors d2 = new DiplomaWithoutAnyHonors();
d2.setStudent("Doe");
String[] homeworksDoneByStackOverflow = {"How to use super"};
d2.setHomeworksDoneByStackOverflow(homeworksDoneByStackOverflow );
System.out.println(d2);
}
}
和
import java.util.Arrays;
public class DiplomaWithHonors extends Diploma {
private String[] achievements;
public String[] getAchievements() {
return achievements;
}
public void setAchievements(String[] achievements) {
this.achievements = achievements;
}
@Override
public String toString() {
return "DiplomaWithHonors [achievements=" + Arrays.asList(achievements)
+ ", toString()=" + super.toString() + "]";
}
}
和
import java.util.Arrays;
public class DiplomaWithoutAnyHonors extends Diploma{
private String[] homeworksDoneByStackOverflow;
public String[] getHomeworksDoneByStackOverflow() {
return homeworksDoneByStackOverflow;
}
public void setHomeworksDoneByStackOverflow(
String[] homeworksDoneByStackOverflow) {
this.homeworksDoneByStackOverflow = homeworksDoneByStackOverflow;
}
@Override
public String toString() {
return "DiplomaWithoutAnyHonors [homeworksDoneByStackOverflow="
+ Arrays.asList(homeworksDoneByStackOverflow) + ", toString()="
+ super.toString() + "]";
}
}