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

 更新时间:2013年04月26日 16:49:56   作者:  
本篇文章介绍了,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("\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;
}

相关文章

  • SpringSecurity在单机环境下使用方法详解

    SpringSecurity在单机环境下使用方法详解

    本文详细介绍了SpringSecurity和SpringBoot的整合过程,包括配置用户认证、JSP页面的使用、数据库认证以及授权功能的实现,感兴趣的朋友一起看看吧
    2025-02-02
  • idea配置maven环境时maven下载速度慢的解决方法

    idea配置maven环境时maven下载速度慢的解决方法

    我们在idea配置maven环境的时候会发现maven更新慢的现象,解决办法就是下载国内的镜像包,完美解决下载速度慢的问题,文中有详细的具体操作方法,并通过图文介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • Java中常用数据类型的输入输出详解

    Java中常用数据类型的输入输出详解

    本文主要介绍了Java中几个常用的数据类型是如何输入和输出的,例如:Char型、int型、double型、数组、字符串等,对我们学习java有一定的帮助,感兴趣的小伙伴可以跟随小编一起学习学习
    2021-12-12
  • idea实现类快捷生成接口方法示例

    idea实现类快捷生成接口方法示例

    这篇文章主要介绍了idea实现类快捷生成接口方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • tomcat connection-timeout连接超时源码解析

    tomcat connection-timeout连接超时源码解析

    这篇文章主要为大家介绍了tomcat connection-timeout连接超时源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • Spring Security如何在Servlet中执行

    Spring Security如何在Servlet中执行

    这篇文章主要介绍了Spring Security如何在Servlet中执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • 详解如何将已有项目改造为Spring Boot项目

    详解如何将已有项目改造为Spring Boot项目

    本篇文章主要介绍了如何将已有项目改造为Spring Boot项目,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • Spring MVC获取查询参数及路径参数代码实例

    Spring MVC获取查询参数及路径参数代码实例

    这篇文章主要介绍了Spring MVC获取查询参数及路径参数代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Java线程生命周期的终止与复位

    Java线程生命周期的终止与复位

    这篇文章主要介绍了Java线程生命周期的终止与复位,Java的线程状态描述放在Thread类里面的枚举类State中.总共包含了6中状态,具体详情需要的小伙伴可以参考一下文章描述
    2022-07-07
  • 一文弄懂fastjson

    一文弄懂fastjson

    fastjson 是一个java语言编写的高性能且功能完善的JSON库,本文主要介绍了fastjson的使用,具有一定的参考价值,感兴趣的可以了解一下
    2023-05-05

最新评论