- package cn.itcast.duotai;
- public class Test{
- public static void main(String[] args) {
- Father f = new Son();
- int x = f.counter(3); //非静态方法,编译看左边,运行看右边
- System.out.println(x);
-
- int y = f.counter(3,3);
- System.out.println(y); //静态方法,编译和运行都看左边
-
- String s = ((Son) f).sayHello();
- System.out.println(s); //若是要用子类自己的方法,的强转成子类
- }
- }
- class Father {
-
- public int counter(int n){
- return n*n;
-
- }
- public static int counter(int m,int n){
- return m*n;
-
- }
- }
- class Son extends Father{
- @Override
- public int counter(int n) {
-
- return n*n+1;
- }
-
- public static int counter(int m,int n){
- return m*n+1;
-
- }
-
- public String sayHello(){
- return "Hello,World!!!";
- }
- }
复制代码 |