通过supper()有参构造器,完成子类对象调用父类属性的方法,并完成赋值
package com.Summer_0426.cn; /** * @author Summer * 通过supper()有参构造器,完成子类对象调用父类属性的方法,并完成赋值 * */ public class Test01 { public static void main(String[] args) { JavaTeacher guoxiang = new JavaTeacher("郭襄");//子类实例化,并传入参数 guoxiang.giveLesson();//输出结果,讲Java课 } } class Teacher{ private String name; public Teacher(String name) {//父类构造方法将传入的name赋值给本类name this.name = name; } public String getName() { return name; } public void giveLesson() { System.out.println("讲数学课"); } } class JavaTeacher extends Teacher{ private String tatoo; public JavaTeacher(String name){//子类通过super()调用父类的构造方法 super(name); } public void sing(){ System.out.println("唱歌"); } public void giveLesson() { System.out.println(getName()+"讲Java课"+"还有一个纹身"+tatoo); } }