这是我要问的一个例子
超类名称.java
public class Name{
protected String first;
protected String last;
public Name(String firstName, String lastName){
this.first = firstName;
this.last = lastName;
}
public String initials(){
String theInitials =
first.substring(0, 1) + ". " +
last.substring(0, 1) + ".";
return theInitials;
}
然后子类是 ThreeNames.java
public class ThreeNames extends Name{
private String middle;
public ThreeNames(String aFirst, String aMiddle, String aLast){
super(aFirst, aLast);
this.middle = aMiddle;
}
public String initials(){
String theInitials =
super.first.substring(0, 1) + ". " +
middle.substring(0, 1) + ". " +
super.last.substring(0, 1) + ".";
return theInitials;
}
所以如果我创建一个 Threename 对象ThreeNames example1 = new ThreeNames("Bobby", "Sue" "Smith")
然后调用System.out.println(example1.initials());
我会得到B.S.S.
我得到的。
我的问题是有没有办法调用 Name 类中的 initials 方法,这样我的输出就是B.S.