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

本文展示了一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值