java通过子类对象访问父类方法
一、示列一:
package com.looper.java;
public class InheritDemo {
public static void main(String[] args) {
Son son = new Son();
son.fatherMethod();
son.name();
}
}
class Son extends Father{
public void name() {
System.out.println("son...name");
}
}
class Father {
public void fatherMethod(){
System.out.println("fatherMethod...");
}
public void name() {
System.out.println("father...name");
}
}控制台输出:
fatherMethod...
son...name
本文介绍Java中如何通过子类对象访问父类的方法。通过一个具体的例子展示,当子类继承父类后,可以在子类中重写父类的方法,并通过子类对象调用父类方法。
461

被折叠的 条评论
为什么被折叠?



