C#中方法的详细介绍
时间:2023-08-06 12:31:21
1.让方法返回多个参数
1.1在方法体外定义变量保存结果
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Method
{
class Program
{
public static int quotient;
public static int remainder;
public static void Divide(int x, int y)
{
quotient = x / y;
remainder = x % y;
}
static void Main(string[] args)
{
Program.Divide(6,9);
Console.WriteLine(Program.quotient);
Console.WriteLine(Program.remainder);
Console.ReadKey();
}
}
}
1.2使用输出型和输入型参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Method
{
class Program
{
public static void Divide(int x, int y, out int quotient, out int remainder)
{
quotient = x / y;
remainder = x % y;
}
static void Main(string[] args)
{
int quotient, remainder;
Divide(6,9,out quotient,out remainder);
Console.WriteLine("{0} {1}",quotient,remainder);
Console.ReadKey();
}
}
}
2.方法的重载
方法重载是面向对象对结构化编程特性的一个重要扩充
构成重载的方法具有以下特点:
(1)方法名相同
(2)方法参数列表不同
判断上述第二点的标准有三点,满足任一点均可认定方法参数列表不同:
(1)方法参数数目不同:
(2)方法拥有相同数目的参数,但参数的类型不一样。
(3)方法拥有相同数目的参数和参数类型,但是参数类型出现的先后顺序不一样,
需要注意的是:方法返回值类型不是方法重载的判断条件。
3.方法的隐藏
namespace 方法隐藏
{
class Program
{
static void Main(string[] args)
{
Parent p = new Child();
p.show();
Console.ReadKey();
}
}
class Parent
{
public void show()
{
Console.Write("父类方法");
}
}
class Child : Parent
{
public new void show()
{
Console.Write("子类方法");
}
}
}
namespace 方法隐藏
{
class Program
{
static void Main(string[] args)
{
Parent.show();
Console.ReadKey();
Child.show();//父类方法
}
}
class Parent
{
public static void show()
{
Console.Write("父类方法");
}
}
class Child : Parent
{
public static new void show()
{
Console.Write("子类方法");
}
}
}
在未指明成员存储权限的前提下,其中的成员都是私有的。
namespace 方法隐藏
{
class Program
{
static void Main(string[] args)
{
Parent p1= new Parent();
Parent p2 = new Child();
p1.show();//父类方法
p2.show();//父类方法
((Child)p2).show();//父类方法
Console.ReadKey();
}
}
class Parent
{
public void show()
{
Console.WriteLine("父类方法");
}
}
class Child : Parent
{
new void show()
{
Console.WriteLine("子类方法");
}
}
}
4.方法重写和虚方法的调用
namespace 方法重写
{
class Program
{
static void Main(string[] args)
{
Parent p1 = new Parent();
Parent p2 = new Child();
p1.show();
p2.show();
((Parent)p2).show();//子类方法
Console.ReadKey();
}
}
class Parent
{
public virtual void show()
{
Console.WriteLine("父类方法");
}
}
class Child:Parent
{
public override void show()
{
Console.WriteLine("子类方法");
}
}
}
标签:C#方法
0
投稿
猜你喜欢
Java利用Easyexcel导出excel表格的示例代码
2023-05-30 23:36:04
Java Objects工具类原理及用法详解
2022-10-15 01:40:35
详解如何把cmd黑窗口把java文件打包成jar
2021-08-09 16:04:39
C# StreamReader类实现读取文件的方法
2023-06-20 13:02:00
java(包括springboot)读取resources下文件方式实现
2021-06-03 20:16:06
C#常用目录文件操作类实例
2023-06-24 13:30:24
c#快速写本地日志方法
2021-08-24 09:31:17
解决MyEclipse出现the user operation is waiting的问题
2022-05-02 21:44:07
java实现MD5加密算法的实例代码
2021-10-01 16:49:14
c#数据绑定之linq使用示例
2022-07-17 11:07:22
Mybatis使用JSONObject接收数据库查询的方法
2023-01-17 05:10:43
Java实现双色球抽奖随机算法示例
2023-04-25 12:15:38
MyBatis常用标签以及使用技巧总结
2022-02-27 20:52:14
Spring bean为什么需要依赖注入
2022-01-24 11:07:21
java使用字符画一个海绵宝宝
2023-09-08 09:45:19
java 发送带Basic Auth认证的http post请求实例代码
2021-11-03 06:21:20
SpringBoot微信消息接口配置详解
2023-08-23 09:51:21
spring cloud zuul增加header传输的操作
2022-12-31 17:24:50
Java实现分布式系统限流
2022-05-31 22:38:05
MyBatis JdbcType 与Oracle、MySql数据类型对应关系说明
2023-08-23 02:23:06