zoukankan      html  css  js  c++  java
  • 静态类(static)与java值传递、引用传递小测

    java中都是值传递。直接上代码了:

     1 class TestStaticA {
     2 
     3     static {
     4         System.out.println("b");
     5     }
     6 
     7     public TestStaticA() {
     8         System.out.println("TestStaticA construction method");
     9     }
    10 
    11     protected void say() {
    12         System.out.println("TestStaticA say hello ");
    13     }
    14 
    15     static {
    16         System.out.println("bb");
    17     }
    18 
    19 }

    继承类:

     1 public class TestStatic extends TestStaticA {
     2 
     3     public static int i = 1;
     4     static {
     5         System.out.println("i" + i);
     6         i=2;
     7     }
     8 
     9     public TestStatic() {
    10         System.out.println("TestStatic construction method"+i);
    11     }
    12 
    13     public void say() {
    14         System.out.println("TestStatic say hello");
    15     }
    16     
    17 
    18     
    19     public void appendex(StringBuffer a, StringBuffer b) {
    20         a.append("a");
    21         b=a;
    22     }
    23 
    24     public static void main(String[] args) {
    25         TestStaticA test = new TestStatic();
    26         new TestStatic();
    27         test.say();
    28         StringBuffer a = new StringBuffer("a");
    29         StringBuffer b = new StringBuffer("b");
    30         new TestStatic().appendex(a,b);
    31         System.out.println(a.toString()+":"+b.toString());
    32     }
    33 
    34 }

    结果:

     1 b
     2 bb
     3 i1
     4 TestStaticA construction method
     5 TestStatic construction method2
     6 TestStaticA construction method
     7 TestStatic construction method2
     8 TestStatic say hello
     9 TestStaticA construction method
    10 TestStatic construction method2
    11 aa:b

    注意红色答案部分,虽然是一个值传递(引用副本),但是引用副本所指向的内容发生改变,当方法结束时,引用副本消亡,但是已经改变了原来的内容。

  • 相关阅读:
    为什么富人越来越富,穷人越来越穷?
    计算几何基础_点_向量_极角排序
    滑窗模板_双向队列
    后缀数组
    AC自动机
    RMQ_ST表
    二叉树求逆序对(伪AC 23333)
    分块
    莫队
    树状数组_二维
  • 原文地址:https://www.cnblogs.com/hoojjack/p/9245159.html
Copyright © 2011-2022 走看看