子類對introduce進行復寫
public class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; System.out.println("Person 二參構造"); } void introduce() { System.out.println("我的名字是" + name + ", 我的年齡是" + age); } }
public class Student extends Person { String address; public Student(String name, int age, String address) { super(name, age); this.address = address; } void introduce() { System.out.println("我的名字是" + name + ", 我的年齡是" + age + ", 我的地址是" + address); } }
public class Test { public Test() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub Student student = new Student("furong", 12, "BeiJing"); student.introduce(); } }
運行結果
Person 二參構造
我的名字是furong, 我的年齡是12, 我的地址是BeiJing子類改進
public class Student extends Person { String address; public Student(String name, int age, String address) { super(name, age); this.address = address; } void introduce() { super.introduce(); System.out.println("我的地址是" + address); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/27403.html
標籤:Java
上一篇:第一個Java程式的撰寫程序
