zoukankan      html  css  js  c++  java
  • 方法的语法(未总结完)

    定义方法的语法:

    //Public  []括号表示可选,可用可不用

     [访问修饰符][static]返回值类型  方法名([参数])

    {

      方法体

    }

     注意:

    1. 方法一般要定义在类中
    2. 如果方法没有返回值,则返回值类型写void
    3. 如果方法没有参数,()不能省略

    方法的调用:如果是静态方法(由static修饰的)则使用  类名.方法名();

    在类中调用本类的方法,可以只写  方法名();

    例子:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication6
     8 {
     9     class Class1
    10     {
    11         private static int member;
    12         //实例方法访问静态字段
    13         public void Way(int num)
    14         {
    15             member = num;
    16         }
    17         //静态方法访问静态字段
    18         public static void Show()
    19         {
    20             Console.WriteLine("Value of member:" + member);
    21         }
    22 
    23     }
    24 
    25     class Program1
    26     {
    27         static void CeShi()
    28         {
    29             Class1 test = new Class1();
    30             //对象调用实例方法
    31             test.Way(40);
    32             Class1.Show();//如果在类中调用本类的方法,可以只写 方法名(); 即 Show();
    33         }
    34     }
    35 }

    参考:http://jingyan.baidu.com/article/f0062228c2e998fbd3f0c8a9.html

  • 相关阅读:
    Iptables 之二扩展模块 nat
    sudo 命令
    7、CentOS6 编译安装
    MySQL5.7 基础之二 DCL DML
    SQL Server 2008R2安装
    6、httpd2.4 编译安装LAMP
    MySQL 基础之一
    gulp
    msbuild
    inno setup
  • 原文地址:https://www.cnblogs.com/start-from-scratch/p/5053663.html
Copyright © 2011-2022 走看看