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 ...
随机推荐
- Collection使用方法
package cn.stat.p3.conection.demo; import java.util.ArrayList; import java.util.Collection; import j ...
- C#类中字段,属性与方法
person类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- C++程序设计教程学习(1)-第一部分 编程基础
第一章 概述 C++到底难不难学?没有学不会的事情 1.1 程序设计语言 语言 编程语言 人和计算机交流的工具,群体扩大,人人间交流过程描述与信息表达的工具 机器语言,汇编语言,高级语言 1.2 C+ ...
- 三星手机照相机出现了故障,htc无法连接无线网
1 重启 2 若还不行,把内存卡,电池拿掉, 也可下载一个相机应用
- HTML&CSS基础学习笔记1.27-input提交数据
提交数据 我们在表单上填的信息很多情况下需要提交到后台. <input>的[type]属性值为“submit”时,表示提交表单数据.它在页面的表现形式也是个按钮,点击该按钮,表单数据将会提 ...
- JS获取select的值
记录一下JS获取select的value值和选项值 <select id="video_status"> <option value="1" ...
- python sqlite3使用
python sqlite3文档地址:http://docs.python.org/2/library/sqlite3.html The sqlite3 module was written by G ...
- 自定义DTD(myeclipser的XML提示功能)
了解DTD定义详见:http://www.w3school.com.cn/dtd/dtd_elements.asp PS:文本只是简单的介绍,启到抛砖引玉的作用. 1.创建DTD文件 <?xml ...
- 【转】解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
原文网址:http://blog.csdn.net/ueryueryuery/article/details/20607845 我写了一个Dialog,Dialog中有一个ListView,想要点Li ...
- 【转】64位win7硬盘安装64位ubuntu 13.04
原文网址:http://www.cnblogs.com/jiangz/p/3751617.html 最近本来是准备通过升级的方式把ubuntu从12.04升级到12.10再升级到13.04的,但是升级 ...