面向对象三大特征之 【 封装性 】

封装性在Java当中的体现:

1.方法就是一种封装;[如下代码示例所示,获取最大数的getMax方法就是一种封装]

2.关键字private也是一种封装。

封装就是将一些细节信息隐藏起来,对于外界不可见。

public class Demo02Method
{
    public static void main(String[] args)
    {
        int[] array = {5,15,25,20,100};
        int max = getMax(array);
        System.out.println("最大值"+ max);
    }
//给我一个数组,我换你一个最大值
    public static int getMax(int[] array)
    {
        int max = array[0];
        for (int i = 0; i < array.length; i++)
        {
            if (array[i]>max)
            {
                max = array[i];
            }
        }
        return  max;

    }
}

  

猜你喜欢

转载自www.cnblogs.com/sdrbg/p/11221413.html