zoukankan      html  css  js  c++  java
  • java--多态


    public class duotaui_1 {
    /*
    * 多态运行规则
    * 成员变量(int num =10;)
    * 编译看左边(父类),运行看右边(父类)
    * 成员方法print()
    * 编译看左边(父类),运行看右边(子类)
    * 静态方法:
    * 编译看左边(父类),运行看右边(父类)
    * 总结:只有非静态的成员方法,编译看左边,运行看右边
    */

    /*
    * 多态好处:
    * a 提高代码的维护性(继承保证)
    * b 提高了代码的扩展性(由多态保证)
    * c 可以当形式参数,可以接受任意子类对象
    * 弊端:
    * 不能使用子类的特有属性和行为
    */



    public static void main(String[] args) {
    // TODO Auto-generated method stub
    father f =new son();
    System.out.println(f.num);
    f.print();
    f.method();
    son s =new son();
    System.out.println(s.num);
    s.print();
    s.method();
    father a =new father();
    System.out.println(a.num);
    a.print();

    }

    }

    class father{
    int num =10;
    public void print() {
    System.out.println("father");
    }
    public static void method() {
    System.out.println("father static father");
    }

    }
    class son extends father {
    int num =20;
    public void print() {
    System.out.println("son");
    }
    public static void method() {
    System.out.println("son static father");
    }
    }

    ******************************人因为有理想、梦想而变得伟大,而真正伟大就是不断努力实现理想、梦想*****************************
  • 相关阅读:
    所谓的日常 #8
    所谓的日常 #7
    所谓的日常 #6
    所谓的日常 #5
    所谓的日常 #4
    所谓的日常 #3
    DFS序+线段树 hihoCoder 1381 Little Y's Tree(树的连通块的直径和)
    Codeforces Round #366 (Div. 2)
    2016 Multi-University Training Contests
    DP套DP HDOJ 4899 Hero meet devil(国王的子民的DNA)
  • 原文地址:https://www.cnblogs.com/cloudLi/p/12916976.html
Copyright © 2011-2022 走看看