C#this的五种用法
this的五种用法:
1.使用被掩盖的成员变量:
class AA
{
int a;
public void set1(int a)
{
this.a = a;//right
}
public void set2(int a)
{
a = a;//会有警告:“对同一变量进行赋值;是否希望对其他变量赋值?”;
}
}
2.把这个对象传给其他函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class BB
{
static public void set(AA t,int a)
{
t.set1(a);
}
}
class AA
{
int a;
public void set1(int a)
{
this.a = a;
}
public void set2(int a)
{
BB.set(this, a);
}
public int get()
{
return a;
}
} class Program
{
static void Main(string[] args)
{
AA t = new AA();
t.set2();
Console.WriteLine(t.get());
}
}
}
3.With Indexers(索引器)//这个暂时不懂
4.调用其他构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication2
{
class AA
{
int a, b;
public AA(int a)
{
this.a = a;
b = -;
print();
}
public AA(int a,int b):this(a)
{
this.b = b;
print();
}
void print()
{
Console.WriteLine("{0} {1}", a, b);
}
}
class Program
{
static void Main(string[] args)
{
AA t = new AA();
t = new AA(, );
}
}
}
输出:
1 -1
1 -1
1 1
5.是代码更明确
如把
public AA(int a)
{
this.a = a;
b = -;
print();
}
改为
public AA(int a)
{
this.a = a;
this.b = -;
print();
}
C#this的五种用法的更多相关文章
- webpack解惑:require的五种用法
我之前在 <前端搭环境之从入门到放弃>这篇文章中吐槽过,webpack中可以写commonjs格式的require同步语法,可以写AMD格式的require回调语法,还有一个require ...
- webpack解惑:require的五种用法 (转)
我之前在 <前端搭环境之从入门到放弃>这篇文章中吐槽过,webpack中可以写commonjs格式的require同步语法,可以写AMD格式的require回调语法,还有一个require ...
- webpack中,require的五种用法
a.js: module.exports = function(x){ console.log(x); } 一,commonjs同步: var b = require('./a');b('你好')// ...
- Android Toast 总结(五种用法)
Toast大家都很熟,不多说.直接上图上代码. 具体代码如下: main.xml: <?xml version="1.0" encoding="utf-8" ...
- Wix 安装部署教程(十五) --CustomAction的七种用法
在WIX中,CustomAction用来在安装过程中执行自定义行为.比如注册.修改文件.触发其他可执行文件等.这一节主要是介绍一下CustomAction的7种用法. 在此之前要了解InstallEx ...
- css详解position五种属性用法及其含义
position(定位) position - 作为css属性三巨头(position.display.float)之一,它的作用是用来决定元素在文档中的定位方式.其属性值有五种,分别是 - stat ...
- EntityFramework嵌套查询的五种方法
这样的双where的语句应该怎么写呢: var test=MyList.Where(a => a.Flows.Where(b => b.CurrentUser == “”) 下面我就说说这 ...
- 转:Windows Socket五种I/O模型
原文转自: Windows Socket五种I/O模型 Winsock 的I/O操作: 1. 两种I/O模式 阻塞模式:执行I/O操作完成前会一直进行等待,不会将控制权交给程序.套接字 默认为阻塞模 ...
- 【转】Java 枚举7常见种用法
原文网址:http://softbeta.iteye.com/blog/1185573 Java 枚举7常见种用法 博客分类: java java枚举enmu 原创地址:http://blog.li ...
随机推荐
- 模板模式(Template)
行为型:Template(模板模式) 作为一个曾经爱好写文章,但是不太懂得写文章的人,我必须承认,开头是个比较难的起步. 模板模式常规定义:模板模式定义了一个算法步骤,把实现延迟到子类. 事实上模板模 ...
- phpquery笔记
下载phpquery包 require('phpQuery/phpQuery.php');//加载 for($i=1168;$i<=10000;$i++){ phpQuery::newDocum ...
- pyqt5消息框QMessageBox
QMessageBox消息框有以下几种类型: QMessageBox.information 信息框 QMessageBox.question 问答框 QMessageBox.warning ...
- About abstract class.
Abstract means should be realized. Virtual means could be overrided. It is very different!
- 自制单片机之十三……时钟IC_DS1302
在网上看了很久,发现初学者最有兴趣的就是DS1302时钟电路,也很自然,它是个做出来就让你觉得最实用的电路了,但实际上制做上并不简单,首先你要让你的显示部分(不管是数码管还是LCD)调试通过.然后把D ...
- TEncoding & TNetEncoding(使用现成的TBase64Encoding,TEncoding和TMBCSEncoding)
TEncoding and TNetEncoding are abstract classes and you will never instantiate one of them, because ...
- /sbin/ifup: configuration for eth0 not found解决
/sbin/ifup: configuration for eth0 not found. Usage: ifup <device name> 那么应该是在/etc/sysconfig/n ...
- 1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 315 Solved: ...
- spring与数据库之间的配置
spring 配置数据源的三种方式 1.使用org.springframework.jdbc.datasource.DriverManagerDataSource配置文件: <bean id=& ...
- Windows 8.1下使用IE 64位
Internet Options -> Advanced -> Settings Security组 对Enable 64-bit processes for Enhanced Prote ...