Java学习----你可以告诉对象该怎么做(方法中传参)

对象根据参数传递来的条件执行相应的功能。

package org.demo.app2;

public class App2 {
    public void print(String msg, int num) {
        for (int i = 0; i < num; i++) {
            System.out.println(msg);
        }
    }
    
    public void b(String[] strs) {
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
    }
    
    public static void main(String[] args) {        
        String s[] = {"aaa","bbb"};
        
        App2 obj = new App2();
        String info = "hahaha";
        obj.print("hello world", 5);
        obj.print(info, 3);
        obj.b(s);
    }
}

输出结果:

hello world
hello world
hello world
hello world
hello world
hahaha
hahaha
hahaha
aaa
bbb
原文地址:https://www.cnblogs.com/dragon1013/p/5051269.html