zoukankan      html  css  js  c++  java
  • 第三章 对象和类型

    MathTest

    using System;
    
    namespace Wrox
    {
       class MainEntryPoint
       {
          static void Main()
          {
             // Try calling some static functions
             Console.WriteLine("Pi is " + MathTest.GetPi());
             int x = MathTest.GetSquareOf(5);
             Console.WriteLine("Square of 5 is " + x);
    
             // Instantiate at MathTest object
             MathTest math = new MathTest();   // this is C#'s way of
                                               // instantiating a reference type
             
             // Call non-static methods
             math.Value = 30;
             Console.WriteLine(
                "Value field of math variable contains " + math.Value);
             Console.WriteLine("Square of 30 is " + math.GetSquare());
          }
       }
    
       // Define a class named MathTest on which we will call a method
       class MathTest
       {
          public int Value;
    
          public int GetSquare()
          {
             return Value*Value;
          }
    
          public static int GetSquareOf(int x)
          {
             return x*x;
          }
    
          public static double GetPi()
          {
             return 3.14159;
          }
       }
    }
    View Code

    MathTestWeakReference

    using System;
    
    namespace Wrox
    {
       class MainEntryPoint
       {
          static void Main()
          {
             // Instantiate a weak reference to MathTest object
             WeakReference mathReference = new WeakReference(new MathTest());   
             MathTest math;
             if(mathReference.IsAlive)
             {
                math = mathReference.Target as MathTest;
                math.Value = 30;
                Console.WriteLine(
                   "Value field of math variable contains " + math.Value);
                Console.WriteLine("Square of 30 is " + math.GetSquare());
    
             }
             else
             {
                Console.WriteLine("Reference is not available.");
             }
    
             GC.Collect();
             
             if(mathReference.IsAlive)
             {
                math = mathReference.Target as MathTest;
             }
             else
             {
                Console.WriteLine("Reference is not available.");
             }
          }
       }
    
       // Define a class named MathTest on which we will call a method
       class MathTest
       {
          public int Value;
    
          public int GetSquare()
          {
             return Value*Value;
          }
    
          public static int GetSquareOf(int x)
          {
             return x*x;
          }
    
          public static double GetPi()
          {
             return 3.14159;
          }
       }
    }
    View Code

    ParameterTest

    using System;
    
    namespace Wrox
    {
       class ParameterTest
       {
          static void SomeFunction(int[] ints, int i)
          {
             ints[0] = 100;
             i = 100;
          }
       
          public static int Main()
          {
             int i = 0;
             int[] ints = { 0, 1, 2, 4, 8 };
             // Display the original values
             Console.WriteLine("i = " + i);
             Console.WriteLine("ints[0] = " + ints[0]);
             Console.WriteLine("Calling SomeFunction...");
    
             // After this method returns, ints will be changed,
             // but i will not
             SomeFunction(ints, i);
             Console.WriteLine("i = " + i);
             Console.WriteLine("ints[0] = " + ints[0]);
             return 0;
          }
       }
    }
    View Code
  • 相关阅读:
    css样式初始化代码总结
    linux LVM逻辑卷的创建,扩容,缩减和删除
    MAC Jenkins安装 + Xcode + 蒲公英 + Testflight
    Linux rsyslog工具
    linux 中 Vi 和 Vim 的使用
    Zabbix实战--监控Nginx、MySQL与VM esxi主机、vSphere Client、JAVA应用
    Linux下netstat命令详解
    Debian 10上使用UFW设置防火墙
    开源网络安全检测工具——伏羲 Fuxi-Scanner
    CentOS8的web终端-cockpit,通过Cockpit管理KVM虚拟机
  • 原文地址:https://www.cnblogs.com/liuslayer/p/7016994.html
Copyright © 2011-2022 走看看