zoukankan      html  css  js  c++  java
  • 17. java面向对象

    一、定义

    JavaSE 5.0中提供了Varargs机制,允许直接定义能和多个实参相匹配的形参。从而,可以用一种更简单的方式,来传递个数可变的实参。
    
    格式:数据类型 ... 变量名
    
    注意点:
    1. 当调用可变格式形参的方法时,传入的参数个数可以是:0个,1个,2个...
    2. 可变个数形参的方法与本类中方法名相同,形参不同的方法之间构成重载
    3. 可变个数形参的方法与本类中方法名相同,形参类型也相同的数组之间不构成重载
    4. 可变个数形参的方法在形参中,必须声明在末尾
    

    二、实例

    public class MethodArgsTest {
    
        public static void main(String[] args) {
            MethodArgsTest mat = new MethodArgsTest();
            mat.argsTest("helw", "hen");
        }
    
        public void argsTest(String strs){
            System.out.println(strs);
        }
    
        public void argsTest(String ... strs){
            for (int i = 0; i < strs.length; i++) {
                System.out.println(strs[i]);
            }
        }
    
        public void argsTest(int a, String ... strs){
            System.out.println(1);
        }
    }
    
    

    三、方法参数的值传递机制

    1. Circle

    public class Circle {
        public double radius;
    
        public double findArea(){
            return Math.PI * radius * radius;
        }
    }
    
    

    2. MethodArgsTest

    public class MethodArgsTest {
    
        public static void main(String[] args) {
            MethodArgsTest mat = new MethodArgsTest();
            mat.argsTest("helw", "hen");
        }
    
        public void argsTest(String strs){
            System.out.println(strs);
        }
    
        public void argsTest(String ... strs){
            for (int i = 0; i < strs.length; i++) {
                System.out.println(strs[i]);
            }
        }
    
        public void argsTest(int a, String ... strs){
            System.out.println(1);
        }
    }
    
    
  • 相关阅读:
    迷宫 广搜
    steam 字符串hash or map
    Karen与测试 奇迹淫巧+快速幂
    puzzle 期望树形DP
    函数 贪心
    P1032 字串变换 字符串
    等效集合 图论(缩点)
    高斯消元
    loj2537. 「PKUWC2018」Minimax
    loj2538. 「PKUWC2018」Slay the Spire
  • 原文地址:https://www.cnblogs.com/hq82/p/12081384.html
Copyright © 2011-2022 走看看