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

2016-02-19 10:13 10 1 收藏

给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的javz笔记之:有趣的静态方法的使用,过去的都会过去,迎接崭新的开始,释放更美好的自己。

【 tulaoshi.com - 编程语言 】

代码如下:

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());

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

      /*
       * 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;
   }

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

   private String name;
   private double salary;
}

来源:http://www.tulaoshi.com/n/20160219/1593841.html

延伸阅读
1.光绘 光是创造出一张照片的基本元素。也许我们经常会被四散的光线所困扰。把相机在三脚架上固定好,调慢快门速度,你就可以亲手控制光线在你设计的场景中的表现。试试使用不同光源:闪光灯、一串灯绳、蜡烛或者手头上的任何东西。在这张照片中,我让一个朋友站在一间全黑的屋子中,把快门速度设在估计差不多够我举着蜡烛绕他走一圈的时间...
标签: Android
安卓手机静态IP的设置方法   1、打开WIFI。 2、点击你需要连接的WIFI接入点名称。 3、输入密码以后,把输入法隐藏一下就会发现显示密码和显示高级选项。 4、勾选--显示高级www.Tulaoshi.com选项。 5、下拉一下会出现IP设置。 6、将选项中的DHCP改为--静态。 7、改为静态以后,下面继续会出现几项,I...
++ Swing读书笔记JInternalFrame的使用(窗口与面板) 5-1-1:JInternalFrame的使用 类层次结构图: java.lang.Object --java.awt.Component --java.awt.Container --javax.swing.JComponent --javax.swing.JInternalFrame JinternalFrame的使用跟JFrame几乎一样,可以最大化、最小化、...
//请参考下面的例子 void Page_Load() { . Response.Charset=""; this.EnableViewState =false; StringWriter tw=new StringWriter(); HtmlTextWriter wt =new HtmlTextWriter(tw); string path=@"c:\test.htm"; //FileInfo info =new FileInfo(@"c:\test.txt"); ...
标签: ASP
  方法一 <% '常用函数 '1、输入url目标网页地址,返回值getHTTPPage是目标网页的html代码 function getHTTPPage(url) dim Http set Http=server.createobject("MSXML2.XMLHTTP") Http.open "GET",url,false Http.send() if Http.readystate<4 then exit function end if getHTTPPage=bytesToBSTR(Http.responseBody,"GB2312") ...

经验教程

588

收藏

26
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部