zoukankan      html  css  js  c++  java
  • 不错的quiz

    using System;
    using System.Collections.Generic;

    namespace abcd
    {
        
    class Program
        
    {
            S s;
            
    struct S
            
    {
                
    public int x; //public int x = 0 编译错误?解释为什么
            }


            C c 
    = new C();
            
    class C
            
    {
                
    public int x = 0;
            }
            
            
            
    void print()
            
    {
                Console.WriteLine(
    "{0}\t{1}", s.x, c.x);
            }


            
    void Foo1(S s, C c)
            
    {
                s.x
    ++;
                c.x
    ++;
                print();
            }


            
    void Foo2(S s, C c)
            
    {
                s 
    = new S();
                c 
    = new C();
                print();
            }


            
    void Foo3(ref S s, ref C c)
            
    {
                s.x
    ++;
                c.x
    ++;
                print();
            }


            
    void Foo4(ref S s, ref C c)
            
    {
                s 
    = new S();
                c 
    = new C();
                print();
            }
           

            
    void Test()
            
    {
                Foo1(s, c);
                Foo2(s, c);
                Foo3(
    ref s, ref c);
                Foo4(
    ref s, ref c);
                
    // 输出结果,解释为什么:
                
    // 0    1
                
    // 0    1
                
    // 1    2
                
    // 0    0

                List
    <S> l1 = new List<S>();
                l1.Add(s);
                
    //l1[0].x++; 编译错误?解释为什么

                S[] l 
    = new S[1];
                l[
    0= new S();
                l[
    0].x++;
                
    // 数组OK,解释为什么

                List
    <C> l2 = new List<C>();
                l2.Add(c);
                l2[
    0].x++;
                
    // OK,解释为什么
            }


            
    static void Main()
            
    {
                
    new Program().Test();
            }
           
        }

    }


    解释每一个为什么,并且画出对象在内存中的布局变化图!
    欢迎大家阅读我的极客时间专栏《Java业务开发常见错误100例》【全面避坑+最佳实践=健壮代码】
  • 相关阅读:
    thinkphp3.1.3验证码优化
    php导出数据为CSV文件DEMO
    python学习笔记十七:base64及md5编码
    linux运维笔记
    [转]如何像Python高手(Pythonista)一样编程
    用gulp清除、移动、压缩、合并、替换代码
    [蓝桥杯][2017年第八届真题]小计算器(模拟)
    [蓝桥杯][2017年第八届真题]发现环(基环树输出环)
    [蓝桥杯][2017年第八届真题]合根植物(并查集)
    省赛训练5-3(四个签到题)
  • 原文地址:https://www.cnblogs.com/lovecherry/p/1136624.html
Copyright © 2011-2022 走看看