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#编写winform程序时,用到的,格式强转,存储到数据库,数据库连接那块就不写了希望对大家有帮助,欢迎评论互相分享技术!//日期格式强制
- java实现学生通讯录,使用XML文件作为数据存储,实现学生编号,姓名,年龄,住址,专业,生日等信息的添加,删除,修改,查找,备份,恢复,专
- Java8 LocalDateTime与timestamp转换将timestamp转为LocalDateTimepublic LocalDa
- Android通过wifi连接手机的方法,供大家参考,具体内容如下1.首先电脑,手机连接同一个网络2.在Android studio中Ter
- 由于ajax本身实际上是通过XMLHttpRequest对象来进行数据的交互,而浏览器出于安全考虑,不允许js代码进行跨域操作,所以会警告&
- 算法描述堆排序算法的描述如下:将待排序的数组调整为最大堆,此时未排序的长度 N 为数组的长度,调整的过程就是倒序将数组的
- 近期,Apache SkyWalking 修复了一个隐藏了近4年的Bug - TTL timer 可能失效问题,这个 bug 在 SkyWa
- JDK8已发布,写了一个datetime时间函数使用方法的小示例package datetime;import static java.ti
- 我正在开发一个软键盘,做得很好,但是我不知道如何自定义一个长按键的弹出窗口.我的键盘视图:<?xml version="1.
- 目录什么是角点?为什么要检测角点?Harris角点检测API操作效果源码什么是角点?角点就是极值点,即在某方面属性特别突出的点。当然,你可以
- Android中的Service和其调用者既可以在同一个App中,也可以在不同的App。如果Service在App1中,而调用Service
- Java压缩文件与解压缩zip文件在日常的使用中经常会使用到像WinRAR或WinZIP这样的压缩文件,通过这些软件可以把一个很大的文件进行
- 安卓应用闪退后总会出现一个“抱歉,App已经停止运行”的弹窗,这样的用户体验并不好。很多大厂的App都去除了这个弹窗,因此本文主要介绍如何去
- 在开发的过程中大家一般都会选择使用数据线连接的方式进行调试,但是有些时候比如使用模拟器时就不能这样了,所以有必要来研究下怎么使用adb通过w
- Rmb.javapublic class Rmb { /** *人民币的基本信息和操作 *@auth
- 本文实例讲述了C#的内存回收方法。分享给大家供大家参考。具体实现方法如下:如下示例代码是调用win32底层操作,可实现内存的回收。 
- CLR支持两种类型:引用类型和值类型。 引用类型总是从托管堆上分配的。 c#中的New操作符返回对象的内存地址。 引用对象的注意点: 1、内
- Android 应用冷启动时,需要从Application开始启动,加载时间就会比较长,容易出现白色或黑色闪屏,观察一下手机上一些 其他AP
- 前言之前做移动端开发,都不清楚WebService是啥东东,现在接触c#,项目中有三处WebService调用,就不得不与其打交道了,最近碰
- C#生成指定范围内的不重复随机数// Number随机数个数// minNum随机数下限// maxNum随机数上限public int[]