方法_静态_非静态

方法Add1、Add2和Multiply
Add1、Multiply都是静态的方法,所以可以不通过创建对象而只用“类名.方法”名调用即可;方法Add1和Main方法同在一个类Program中,因此省略“方法名.”。
非静态方法Add2只能通过实例化的方法来调用;否则会报13行的错误。

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

namespace 方法_静态_非静态_
{
	class Program
	{
		static void Main( string[] args )
		{	
			int nRes1 = Add1( 1, 3 );
			Program A = new Program(); // 报错::非静态的字段、方法或属性“方法_静态_非静态_.Program.Add2(int, int)”要求对象引用
			int nRes2 = A.Add2( 7, 100 );
			int nRes3 = Fangfa.Multiply( 3, 5 );
			Console.WriteLine( nRes1 );
			Console.WriteLine( nRes2 );
			Console.WriteLine( nRes3 );
			Console.ReadKey();
		}
		public static int Add1( int x, int y )
		{
			return   x + y;
		}
		public int Add2( int x, int y )
		{
			return   x + y;
		}
	}
	static class Fangfa
	{
		public static int Multiply( int x, int y )
		{
			return x * y;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值