1、静态方法与实例方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 静态方法与实例方法
{
class Program
{
int exampleVar = 0;//静态成员
static int staticVar = 0;//静态成员
static void staticMethod(){
staticVar = 1;
//exampleVar = 1;不能调用实例成员
} //只能访问静态成员
void exampleMethod() {
//实例成员方法可以调用静态和实例任何成员
staticVar = 1;
exampleVar = 1;
//this.staticVar = 2;
//在实例方法中可以使用this
}
static void Main(string[] args)
{
staticMethod();//直接调用静态方法;同Program.staticMethod();
//调用实例方法时,将类进行实例化
staticVar = 2;//直接调用静态成员,也等价于 Program.staticVar = 2;
Program p = new Program();
p.exampleMethod();
p.exampleVar = 1;
//Program.exampleMethod();不能通过访问静态方法的途径访问实例方法,要通过对象去访问
}
}
}

2、虚方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 虚方法等知识
{
class class1{
//指定方法public;默认方法是私有的,只能在当前类中进行访问
//需要在其他类中进行访问,就要指定public;访问权限是最高的在项目内部都可以访问
public virtual void virtualMethid()//虚方法可以在派生类中重写
{
Console.WriteLine("这是一个虚方法!");
}
public void nonVirtualMethod() {
Console.WriteLine("这是一个非虚方法");
}
}
class class2:class1 { //class2继承class1
public new void nonVirtualMethod() {
Console.WriteLine("这是一个新方法!");
}
public override void virtualMethid()//重写虚方法
{
//base.virtualMethid();
Console.WriteLine("这是新写的虚方法!");
}
}
class Program
{
static void Main(string[] args)
{
class1 c1 = new class1();
c1.virtualMethid();
c1.nonVirtualMethod();
class2 c2 = new class2();
c2.virtualMethid();
c2.nonVirtualMethod();
c1 = c2;
c1.virtualMethid();
c1.nonVirtualMethod();
//调用了c2的虚方法
//虚方法的实现不是一成不变的,而非虚方法是一成不变的
Console.ReadKey();
}
}
}

3、重写方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 重写方法
{
//方法名称和参数列表不能改变
class class1
{
public virtual void Write() {
Console.WriteLine("这是一个虚方法,可以被重写!");
}
}
class class2:class1
{
public override sealed void Write()//重写方法,不能更改权限修饰符
{
Console.WriteLine("这是一个重写方法,被称为一重写的方法!");
}
}
class class3:class2
{
}
//如果不想让继承class2的类再去重写Write()方法
//那就采用关键字sealed
class Program
{
static void Main(string[] args)
{
class1 c1 = new class1();
c1.Write();
class2 c2 = new class2();
c2.Write();
//override和virtual配合
Console.ReadKey();
}
}
}

4、外部方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace 外部方法
{
//方法位置:通常放在类当中,并且与其他方法保持平级关系
class Program
{//使用之前应该引用命名空间:
[DllImport("User32.dll")]//调用库文件(alt+shift+f10)
//声明外部方法 使用关键字extern由于配合DllImpor,需要static
public static extern int MessageBox(int h, string m, string c, int type);
static int Main(string[] args)
{
Console.WriteLine("请输入您的名字:");
String name = Console.ReadLine();
//利用return进行弹出对话框, 所以需要精main方法改为in他类型
return MessageBox(0, "您好:" + name + "\n" + "欢迎来到奇客艺术博客","欢迎提示", 0);
}
}
}

5、分部方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 分布方法
{
public partial class Programe
{
//声明与定义一个分部类
//生命分部方法
//方法默认为私有,也可以加上
partial void Write();//声明
partial void Write() {
Console.WriteLine("这是一个分部方法");
}
}
public partial class Programe
{
static void Main(string[] args)
{
Programe p = new Programe();
p.Write();
Console.ReadKey();
}
}
}

6、方法重载

决定方法是否构成重载有三个条件

1)在同一个类中

2)方法名相同

3)参数列表不同

利用重载分别求圆、矩形、三角形的面积

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 方法重载
{
class Program
{
static void WriteArea(double radius) {
double Area = System.Math.PI*radius*radius;
Console.WriteLine("您所求圆的面积是:{0}",Area);
}
static void WriteArea(double width,double height) {
double Area = width * height;
Console.WriteLine("矩形面积为:{0}", Area);
}
//三角形面积
static void WriteArea(double a,double b,double c) {
double p = (a + b + c) / 2;
double Area = System.Math.Sqrt(p * (p - a) * (p - b) * (p - c));
Console.WriteLine("三角形的面积是{0}",Area);
}
static void Main(string[] args)
{
WriteArea(3);
WriteArea(20,23);
WriteArea(3,4,5);
Console.ReadKey();
}
}
}

7、Main方法

main方法有四种表现形式
public static void Main()
public static int Main()
public static void Main(string[] args)
public static int Main(string[] args)

指定命令行参数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Main方法
{
class Program
{
static void Main(string[] args)
{
//查看命令行参数数组长度
//指定命令行参数
Console.WriteLine("有{0}个命令行参数", args.Length);
foreach(string str in args)
Console.WriteLine(str);
Console.ReadKey();
//项目->右击->属性->调试
}
}
}

C#方法有关内容的总结--C#基础的更多相关文章

  1. XCode中的单元测试:编写测试类和方法(内容意译自苹果官方文档)

    当你在工程中通过测试导航栏添加了一个测试target之后, xcode会在测试导航栏中显示该target所属的测试类和方法. 这一章演示了怎么创建测试类,以及如何编写测试方法. 测试targets, ...

  2. JS之BOM和DOM(来源、方法、内容、应用)

    1.Javascript组成(此文为转载) JavaScript的实现包括以下3个部分: 1)核心(ECMAScript):描述了JS的语法和基本对象. 2)文档对象模型 (DOM):处理网页内容的方 ...

  3. Java学习-026-类名或方法名应用之二 -- 统计分析基础

    前文讲述了类名或方法的应用之一调试源码,具体请参阅:Java学习-025-类名或方法名应用之一 -- 调试源码 此文主要讲述类名或方法应用之二统计分析,通过在各个方法中插桩(调用桩方法),获取方法的调 ...

  4. 魔法方法:构造和析构 - 零基础入门学习Python041

    魔法方法:构造和析构 让编程改变世界 Change the world by program 构造和析构 什么是魔法方法呢?我们来系统总结下: - 魔法方法总是被双下划线包围,例如__init__ - ...

  5. WordPress 无法使用the_content()方法输出内容

    在使用WordPress里在一个页面里我使用the_content()方法来输出当前页面的内容,但却显示为空,而标题,url等都没有问题 在网络上好像遇到这种情况的人很少只找到了一个说是可能是func ...

  6. python 列表排序方法reverse、sort、sorted基础篇

    python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...

  7. dom&bom的起源,方法,内容,应用

    Document Object Model的历史可以追溯至1990年代后期微软与Netscape的"浏览器大战"(browser wars),双方为了在JavaScript与JSc ...

  8. git内容补充-Git零基础快速入门-苏玲

    https://git-scm.com/book/zh/v2 git历史 集中式版本控制管理:cvs.svn 分布式版本控制管理:git 基本命令 git config --list --global ...

  9. JAVA中String类的方法(函数)总结--JAVA基础

    1.concat()方法,当参数为两字符串时,可实现字符串的连接: package cn.nxl123.www; public class Test { public static void main ...

随机推荐

  1. MySQL中四种常用存储引擎的介绍

    MySQL常用的四种引擎的介绍 (1):MyISAM存储引擎: 不支持事务.也不支持外键,优势是访问速度快,对事务完整性没有 要求或者以select,insert为主的应用基本上可以用这个引擎来创建表 ...

  2. RTLabel 的简单使用

    RTLabel 基于富文本的格式,适用于iOS,类似HTML的标记. RTLabel 基于UILabel类的拓展,能够支持Html标记的富文本显示,它是基于Core Text,因此也支持Core Te ...

  3. 对html语义化的理解

    所有人都知道html即超文本标记语言或超文本链接标示语言,是目前网络上应用最为广泛的语言,也是构成网页文档的主要语言. html标签中的大部分都是由"语义化"标签所担任 那么,它有 ...

  4. 深入分析Java ClassLoader的原理(转)

    一.什么是ClassLoader? 大家都知道,当我们写好一个Java程序之后,不是管是CS还是BS应用,都是由若干个.class文件组织而成的一个完整的Java应用程序,当程序在运行时,即会调用该程 ...

  5. Centos环境下给PHP7.0安装yaf扩展

    首先要知道PHP的安装目录在哪里,以我当前的路径为例,在/usr/local/php目录下. 下一步需要下载扩展包,进入http://pecl.php.net/package/yaf寻找符合版本要求的 ...

  6. Luogu P1877 [HAOI2012]音量调节

    题目描述 一个吉他手准备参加一场演出.他不喜欢在演出时始终使用同一个音量,所以他决定每一首歌之前他都需要改变一次音量.在演出开始之前,他已经做好一个列表,里面写着每首歌开始之前他想要改变的音量是多少. ...

  7. 将FTP映射至Windows

    在经常使用ftp传输文件的环境中,每次上传和下载文件都需要重新连接然后登录是非常繁琐的一件事情.我们可以将FTP空间映射到本地磁盘空间,免去输入地址以及账号.密码.方便我们日常中文件的上传和下载. 1 ...

  8. com.alibaba.druid.sql.parser.ParserException: syntax error, QUES %, pos 80 like报错解决

    最近,把各应用的jdbc连接池统一从dbcp2改成了druid,运行时druid报sql解析错误,如下: select * from test         where 1=1         &l ...

  9. 终于,我也要出一本C#的书了 - 我的写作历程与C#书单推荐

    我之前的面试题停了很久,是因为 - 我写书去了. 前言 我于2012年3月开始工作,到现在马上就满六年了.这六年里,我从一个连Sql server是什么都不知道,只会写最简单的c#的程序员开始做起,一 ...

  10. Minicom串口工具安装及配置

    Minicom串口工具安装及配置 1. 简述 嵌入式开发多采用串口线连接电脑进行开发及调试. 2 安装及配置串口工具(以Minicom为例) Tiny4412开发板提供的有RS232电平的DB9公头接 ...