美文网首页
【Java补充】Java方法参数

【Java补充】Java方法参数

作者: 椎椎隹木 | 来源:发表于2018-05-31 12:09 被阅读0次
    public class Main {
    
        public static void main(String[] args) {
            System.out.println("Hello World!");
    
            //Test1
            System.out.println("Test1");
            double percent = 10;
            System.out.println("befor percent=" + percent);
            tripleValue(percent);
            System.out.println("After percent" + percent);
    
            //Text2
            System.out.println("Text2");
            Employee harry = new Employee("Harry", 500);
            System.out.println("befor salary" + harry.getSalary());
            tripleSalary1(harry);
            System.out.println("After salary" + harry.getSalary());
    
            System.out.println("Text3");
            Employee harry2 = new Employee("Harry", 500);
            System.out.println("befor salary" + harry2.getSalary());
            tripleSalary2(harry2);
            System.out.println("After salary" + harry2.getSalary());
    
            //Text4
            System.out.println("Test4");
            System.out.println("\nTest4:");
            Employee a = new Employee("Alice", 70000);
            Employee b = new Employee("Bob", 60000);
            System.out.println("Before: a=" + a.getName());
            System.out.println("Before: b=" + b. getName());
            swap(a, b);
            System.out.println("After: a=" + a.getName());
            System.out.println("After: b=" + b.getName());
        }
    
        public static void tripleValue(double x) {
            x=x*3;
            System.out.println("End of method x=" + x);
        }
    
        public static void tripleSalary1(Employee x) {
            x.raiseSalary(200);
            System.out.println("End of method: salary=" + x.getSalary());
    
        }
        public static void tripleSalary2(Employee x) {
            double a=x.getSalary();
            a=a*3;
            x.setSalary(a);
            System.out.println("End of method: salary=" + x.getSalary());
    
        }
    
        public static void swap(Employee x, Employee y) {
            Employee temp=x;
            x=y;
            y=temp;
            System.out.println("End of method: x=" + x.getName());
            System.out.println("End of method: y=" + y.getName());
    
        }
    }
    class  Employee{
        private  String name;
        private  double salary;
    
        public Employee(String name, double salary) {
            this.name = name;
            this.salary = salary;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getSalary() {
            return salary;
        }
    
        public void setSalary(double salary) {
            this.salary = salary;
        }
    
        public void raiseSalary(double byPercent) {
            double raise=salary*byPercent/100;
            salary=raise;
        }
    }
    
    

    运行结果

    Hello World!
    Test1
    befor percent=10.0
    End of method x=30.0
    After percent10.0
    Text2
    befor salary500.0
    End of method: salary=1000.0
    After salary1000.0
    Text3
    befor salary500.0
    End of method: salary=1500.0
    After salary1500.0
    Test4
    
    Test4:
    Before: a=Alice
    Before: b=Bob
    End of method: x=Bob
    End of method: y=Alice
    After: a=Alice
    After: b=Bob
    

    在这个程序中, 首先试图将一个值参数的值
    提高两倍,但没有成功:

    Testing tripleValue:
    Before: percent=10.0
    End of method: x:30.0
    After: percent=10.0
    

    随后, 成功地将一个雇员的薪金提高了两倍:

    Testing tripleSalary:
    Before: salary=50000.0
    End of method: salary=150000.0
    After: salary=150000.0
    

    方法结束之后, harry 引用的对象状态发生了改变。这是因为这个方法可以通过对象引用
    的拷贝修改所引用的对象状态。
    最后,程序演示了 swap 方法的失败效果:

    Testing swap:
    Before: a=Alice
    Before: b=Bob
    End of method: x=Bob
    End of method: y=Alice
    After: a=Alice
    After: b=Bob
    

    可以看出, 参数变量 X 和 y 交换了, 但是变量 a 和 b 没有受到影响。


    图示.jpg

    相关文章

      网友评论

          本文标题:【Java补充】Java方法参数

          本文链接:https://www.haomeiwen.com/subject/bzkqsftx.html