• 4-7再谈方法之方法参数的值传递(练习)


    练习1

    public class TransferTest3{
        public static void main(String args[]){
            TransferTest3 test=new TransferTest3();
            test.first();
        }
    
        public void first(){
            int i=5;
            Value v=new Value();
            v.i=25;
            second(v,i);
            System.out.println(v.i);
        }
    
        public void second(Value v,int i){
            i=0;
            v.i=20;
            Value val=new Value();
            v=val;
            System.out.println(v.i+" "+i);
    
        }
    }
    class Value {
        int i= 15;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    练习2

    定义一个int型的数组:int[] arr = new int[]{12,3,3,34,56,77,432};
    让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的新值。遍历新的数组。

    public class Test3 {
        public static void main(String[] args) {
            /*
             * 微软:
             * 定义一个int型的数组:int[] arr = new int[]{12,3,3,34,56,77,432};
             * 让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的新值。遍历新的数组。
             */
    
            int[] arr = new int[]{12,3,3,34,56,77,432};
    
    //错误写法
            for(int i= 0;i < arr.length;i++){
                arr[i] = arr[i] / arr[0];
            }
    
    //正确写法1
            for(int i = arr.length-1;i >= 0;i--){
                arr[i] = arr[i] / arr[0];
            }
    
    //正确写法2
            int temp = arr[0];
            for(int i= 0;i < arr.length;i++){
                arr[i] = arr[i] / temp;
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    练习3

    • int[] arr = new int[10];
    • System.out.println(arr);//地址值?

    • char[] arr1 = new char[10];
    • System.out.println(arr1);//地址值?
    /*
     * int[] arr = new int[10];
     * System.out.println(arr);//地址值?
     *
     * char[] arr1 = new char[10];
     * System.out.println(arr1);//地址值?
     */
    public class ArrayPrint {
    
        public static void main(String[] args) {
            int[] arr = new int[]{1,2,3};
            //传进去的是一个Object的对象
            System.out.println(arr);//地址值
    
            char[] arr1 = new char[]{'a','b','c'};
            //传进去的是一个数组,里面遍历数据了
            System.out.println(arr1);//abc
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    练习4

    练习4:将对象作为参数传递给方法

    • (1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积。

    • (2)定义一个类PassObject,在类中定义一个方法printAreas(),该方法的定义如下:

    • public void printAreas(Circle c,int time)

    • 在printAreas方法中打印输出1到time之间的每个整数半径值,以及对应的面积。

    • 例如,times为5,则输出半径1,2,3,4,5,以及对应的圆面积。

    • (3)在main方法中调用printAreas()方法,调用完毕后输出当前半径值。

    public class Circle {
    
        double radius;	//半径
    
        //返回圆的面积
        public double findArea(){
            return radius * radius * Math.PI;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    public class PassObject {
    
        public static void main(String[] args) {
            PassObject test = new PassObject();
    
            Circle c = new Circle();
    
            test.printAreas(c, 5);
    
            System.out.println("no radius is:" + c.radius);
        }
    
        public void printAreas(Circle c,int time){
    
            System.out.println("Radius\t\tAreas");
    
            //设置圆的半径
            for(int i = 1;i <= time ;i++){
                c.radius = i;
                System.out.println(c.radius + "\t\t" + c.findArea());
            }
    
            //重新赋值
            c.radius = time + 1;
        }
    }```
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    两个方法解决NC6元数据升级无效问题
    Delphi XE2 新控件 布局Panel TGridPanel TFlowPanel
    Unity_热更方案
    Mysql备份工具innobackupex简单操作及参数说明
    NIO和BIO
    ARM编程模型-内存空间和数据
    AutoSAR基础:Port与Dio
    工作比读研简单多了
    学习vue3踩坑分享( 1 )- 使用Element Plus <script lang=“ts“ setup> 加上lang=“ts“后编译错误
    Data Guard 和 GoldenGate的区别
  • 原文地址:https://blog.csdn.net/qq_44774198/article/details/125513281