尚硅谷视频课程java第九、十章

java第九、十章

1. 大概目录

image-20201221160039909

2. 对象的内存机制

JVM的简要内存解析

image-20201221160620633

同一个类不同对象的内存解析

image-20201221161320373

对象数组的内存解析

image-20201221164958473

3. 匿名对象

public class HelloWorld {

    public static class a{
        String str = "test";

        public void print()
        {
            System.out.println(str);
        }
    }

    public static void main(String[] args) {
//        匿名对象
        new a().print();// test
    }
}
public class HelloWorld {

    public static class a{
        String str = "test";

        public void print()
        {
            System.out.println(str);
        }

    }

    public static class aa
    {
        public void show(a x)
        {
            x.print();
        }
    }

    public static void main(String[] args) {
        aa xx = new aa();
//        匿名对象的常见用法
        xx.show(new a()); //test
    }
}
image-20201221170833594

4. 方法的重载

image-20201221171407242image-20201221171518405

image-20201221171538517

5. 可变个数形参

public class HelloWorld {

    public static void print(String ... strs)
    {
        System.out.println("method print (String ... strs)");
        for (int i = 0; i < strs.length; i ++)
        {
            System.out.print(strs[i] + " ");
        }
    }

    public static void main(String[] args) {
//        可变类型参数
        print("Hello", "World", "Exchange");
    }
}

可变个数形参类型只能放在最后

public static void print(String ... strs, int x)
    //Vararg 参数必须为列表中的最后一项

等价于

public static void print(String[] strs)
    //已在 'com.example.helloworld.HelloWorld' 中定义 'print(String[])'

但是后者这种写法,可以放在任意的位置,而不是

public class HelloWorld {

    public static void print(String[] strs, int x)
    {
        System.out.println("method print (String ... strs)");
        for (int i = 0; i < strs.length; i ++)
        {
            System.out.print(strs[i] + " ");
        }
    }

    public static void main(String[] args) {
//        String[]作为形参类型,可以放在任何地方,但是只支持如下的写法,得要new String[]{}
//        new出来的String[]{}也是支持String ...形参的
        print(new String[]{"Hello", "World", "Exchange"}, 3);
    }
}

可变个数形参可以有多个,但是等到调用的时候只能有一个传入的值是不用new出数组的形式,一般选最后一个地方使用不new的形式

image-20201221174227959

6. 形参的值传递机制

image-20201221205315922

挺不错的练习题,答案是15,0,20

image-20201221220202112
posted @ 2020-12-27 22:19  alexemey  阅读(35)  评论(0)    收藏  举报