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. SpringMVC之HelloWorld实例

    1.1 Helloworld实例的操作步骤  1. 加入jar包 2. 配置dispatcherServlet 3. 加入Spring配置文件 4. 编写请求处理器 并表示为处理器 5. 编写视图 1 ...

  2. c语言环境初始化&c语言和汇编混合编程

    bootloader通常会分为两个阶段:第一阶段采用汇编语言来编写,主要是一些核心的初始化工作(内存,时钟的初始化),第二阶段使用C语言来编写,主要是它会完成一些板载硬件的初始化(串口,网口)然后其启 ...

  3. [Python Study Notes]WdSaveFormat 枚举

    WdSaveFormat 枚举 指定要在保存文档时使用的格式. 版本信息 已添加版本: 名称 值 说明 wdFormatDocument 0 Microsoft Word 格式. wdFormatDO ...

  4. selenium2 python自动化测试实战(回归测试)

    selenium2 python自动化测试实战 最近接手商城的项目,针对后台测试,功能比较简单,但是流程比较繁多,涉及到前后台的交叉测试.在对整个项目进行第一轮测试完成之后,考虑以后回归测试任务比较重 ...

  5. 017 Java中的静态代理、JDK动态代理、cglib动态代理

    一.静态代理 代理模式是常用设计模式的一种,我们在软件设计时常用的代理一般是指静态代理,也就是在代码中显式指定的代理. 静态代理由业务实现类.业务代理类两部分组成.业务实现类负责实现主要的业务方法,业 ...

  6. Spring_Spring与AOP_AspectJ基于注解的AOP实现

    一.AspectJ.Spring与AOP的关系 AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Java字节编码规范的Cl ...

  7. Head First C 笔记

    嗨翻c语言 1. 入门 为什么字符从零开始编号? 字符的索引数值表示的是一个偏移量,它表示的是当前所引用的字符与第一个字符之间差多少个字符. 单双引号的区别? 单引号 一个字符,双- 字符串 字符串字 ...

  8. git使用步骤

    1报名出处: git config --global user.name lhp  用户名 git config --global user.email a@.qq.com 邮箱 2.建立项目文件夹: ...

  9. Android App 压力测试方法(Monkey)

    一.为什么要开展压力测试 a.提高产品的稳定性:b.提高产品的留存率 二.什么时候开展压力测试 a.首轮功能测试通过后:b.下班后的夜间进行 三.7个基础知识(理论部分) 3.1 手动测试场景与自动测 ...

  10. C/C++语言的语法基础

    数据类型指明变量或表达式的状态和行为,数据类型决定了数的取值范围和允许执行的运算符集.c++语言数据类型可以分为两大类:基本类型和引用类型.基本类型是指不能再分解的数据类型,其数据在函数的调用中是以传 ...