javz 노트: 재미있는 정적 방법의 사용

6126 단어 java정적 방법

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("
Testing 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("
Testing 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("
Testing 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("
Testing 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;
}

좋은 웹페이지 즐겨찾기