zoukankan      html  css  js  c++  java
  • 方法的重载并不一定是在一个类中

    方法的覆盖(overriding)

    方法的重载并不一定是在一个类中:子类可以从父类继承一个方法,也可以定义一个同名异参的方法,也称为overloading。

    当子类从父类继承一个无参方法,而又定义了一个同样的无参方法,则子类新写的方法覆盖父类的方法,称为覆盖。(注意返回值类型也必须相同,否则编译出错。)

    如果方法不同,则成重载。

     1 package TomText;
     2 
     3 public class TomText_15 {
     4     
     5          public static void main(String[] args) {
     6           int[] i = new int[args.length];
     7           
     8           copyArrayTo(args,i);
     9           calculateForPrinting(i);
    10          }
    11          
    12          /*
    13           * 将String类型的数组的值,赋值给int类型的数组
    14           */
    15          public static void copyArrayTo(String[] s,int[] i) {
    16           for(int n=0;n<s.length;n++) {
    17            i[n] =Integer.parseInt(s[n]);  //将String类型强制转换成int类型,再赋值给数组元素
    18           }
    19          }
    20          /*
    21           * 计算打印张数
    22           * 如果一张打9页,那么n份,总共多少张
    23           */
    24          public static void calculateForPrinting(int[] i) {
    25           int sum =0;
    26           
    27           for(int m=0; m<i.length; m++) {
    28            if(i[m]%9 == 0) sum = sum + i[m]/9;
    29            else sum = sum + (i[m]/9 + 1);
    30           }
    31           
    32           System.out.println(sum);
    33          }
    34     
    35 
    36 }
  • 相关阅读:
    P1486 [NOI2004]郁闷的出纳员
    P1966 火柴排队
    P2627 修剪草坪
    P1621 集合
    P1025 数的划分
    中国剩余定理
    P2043 质因子分解
    P1075 质因数分解
    C#之引用类型参数
    C#之方法的定义及调用学习案例
  • 原文地址:https://www.cnblogs.com/borter/p/9418574.html
Copyright © 2011-2022 走看看