欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javz笔记之:有趣的静态方法的使用

程序员文章站 2023-12-04 16:28:40
复制代码 代码如下:import java.util.*; public class welcome {     public stati...

复制代码 代码如下:

import java.util.*;

public class welcome {

    public static void main(string[] args)
       {
          /*
           * test 1: methods can't modify numeric parameters
           */
          system.out.println("testing triplevalue:");
          double percent = 10;
          system.out.println("before: percent =" + percent);
          percent = triplevalue(percent);
          system.out.println("after: percent =" + percent);  //这里输出为30了!正常的结果

          /*
           * test 2: methods can change the state of object parameters
           */
          system.out.println("\ntesting triplesalary:");
          employee harry = new employee("harry", 50000);
          system.out.println("before: salary =" + harry.getsalary());
          triplesalary(harry);
          system.out.println("after: salary =" + harry.getsalary());

          /*
           * test 3: methods can't attach new objects to object parameters
           */
          system.out.println("\ntesting swap:");
          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 double triplevalue(double x) // doesn't work
       {
          return x = 3 * x;
          //system.out.println("end of method: x=" + x);
       }

       public static void triplesalary(employee x) // works
       {
          x.raisesalary(200);
          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 // simplified employee class
    {
       public employee(string n, double s)
       {
          name = n;
          salary = s;
       }

       public string getname()
       {
          return name;
       }

       public double getsalary()
       {
          return salary;
       }

       public void raisesalary(double bypercent)
       {
          double raise = salary * bypercent / 100;
          salary += raise;
       }

       private string name;
       private double salary;
    }


如果是以下代码:system.out.println("after: percent =" + percent);  //这里输出为10了!因为静态方法达不成你要的效果

这是因为静态方法不能对对象产生效果,和静态域一样,它属于类,不属于任何对象

复制代码 代码如下:

/**
 * this program demonstrates parameter passing in java.
 * @version 1.00 2000-01-27
 * @author cay horstmann
 */
public class paramtest
{
   public static void main(string[] args)
   {
      /*
       * test 1: methods can't modify numeric parameters
       */
      system.out.println("testing triplevalue:");
      double percent = 10;
      system.out.println("before: percent=" + percent);
      triplevalue(percent);
      system.out.println("after: percent=" + percent);

      /*
       * test 2: methods can change the state of object parameters
       */
      system.out.println("\ntesting triplesalary:");
      employee harry = new employee("harry", 50000);
      system.out.println("before: salary=" + harry.getsalary());
      triplesalary(harry);
      system.out.println("after: salary=" + harry.getsalary());

      /*
       * test 3: methods can't attach new objects to object parameters
       */
      system.out.println("\ntesting swap:");
      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) // doesn't work
   {
      x = 3 * x;
      system.out.println("end of method: x=" + x);
   }

   public static void triplesalary(employee x) // works
   {
      x.raisesalary(200);
      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 // simplified employee class
{
   public employee(string n, double s)
   {
      name = n;
      salary = s;
   }

   public string getname()
   {
      return name;
   }

   public double getsalary()
   {
      return salary;
   }

   public void raisesalary(double bypercent)
   {
      double raise = salary * bypercent / 100;
      salary += raise;
   }

   private string name;
   private double salary;
}