Java入门之基础详解

2019-08-06  本文已影响0人  南在南方i

Java面向对象,C面向过程

数组

数组一旦被定义,大小就确定了,无法更改

int[] score = {1, 2, 3};
String[] names = {"jack", "merry"};
//内容动态添加
float[] height = new float[5];
        height[0] = 165.5f;
        height[1] = 170f;

尽量不要扩大变量的作用域

for (int i = 0; i < 10; i++) {
        System.out.print(count[i] + " ");
}
        System.out.println();
for (int temp : count) {
        System.out.print(temp + " ");
 }
        System.out.println();
System.out.println(Arrays.toString(count));

类***

类就是封装

人类:概念 抽象 不能完成具体的事情
小王:对象 具体存在 可以使用的

车子:牌子 四个轮子 发动机 5个座位 不可以加 类
宝马:BMW 四个轮子 发动机 5个座位 可以开 对象

对象***

些许注意事项


Person xw = new Person(); Person zs = new Person();



参数与返回值

 public void test2(){
        //在自己的类里面可以调用这个类里面的所有资源
        test();//隐藏this.
    }

    //没有返回值 接收一个参数
    public void test3(String name){

    }

    //有一个参数有一个返回值
    public int test4(String name){
        return 0;
    }

    //有多个参数有一个返回值
    public int test5(String name,int age){
        return 0;
    }

    //可变参数 相当于数组
    public int test6(int ... counts){
        int sum = 0;
        for (int i = 0;i < counts.length;i++){
            sum += counts[i];
        }
        return sum;
    }

PS.

上一篇 下一篇

猜你喜欢

热点阅读