package homework; public class text { public String toString() { return "Fruit toString."; } public static void main(String args[]) { text f=new text(); System.out.println("f="+f); // System.out.println("f="+f.toString()); } }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
此处的toString函数覆盖了Object中的toString函数
在子类中调用被覆盖的函数用super作为父类对象再调用函数即可
![复制代码](https://common.cnblogs.com/images/copycode.gif)
package homework; public class text { public static void main(String[] args){ child baby=new child(); baby.eat(); } } class father{ public father(){ System.out.println("father created"); } public void eat(){ System.out.println("2"); } } class child extends father{ public child(){ super(); System.out.println("child created"); } public void eat(){ System.out.println("1"); super.eat(); } }标签:关系,子类,System,toString,child,println,父类,public,out From: https://www.cnblogs.com/cqdycazs/p/17777544.html