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 ...
随机推荐
- [Leetcode][048] Rotate Image 略详细 (Java)
题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有 ...
- Guava API学习之Ordering犀利的比较器 编辑
Ordering是Guava类库提供的一个犀利强大的比较器工具,Guava的Ordering和JDK Comparator相比功能更强.它非常容易扩展,可以轻松构造复杂的comparator,然后用在 ...
- 表单控件之select
一.表单控件之表单 1.依次获取表单里的所有控件: for (i = 0; i < document.getElementById("formName").length; i ...
- 初学mysql命令
创建数据库mydb: create database mydb; 运行sql脚本文件:(连接数据库后) \. e:\myphpWeb\createTables.sql 删除数据库mydb: drop ...
- JAVA_build_ant_Tstamp
Description Sets the DSTAMP, TSTAMP, and TODAY properties in the current project. By default, the DS ...
- 再转一篇gtest1.6安装
http://www.cppblog.com/izualzhy/archive/2012/07/31/185772.html googletest是一个用来写C++单元测试的框架,它是跨平台的,可应用 ...
- injector
angular 提供了一套依赖注入的机制,和后台很像.虽然我不觉得有很重要. var $injector = angular.injector(["myModule"]); var ...
- (2) 假设字符串类似这样的aba和aab就相等,现在随便给你二组字符串,请编程比较他们看是否相等
/** * 第一种方式: * 实现思路:将字符串通过getBytes方法转换为byte数组,或者通过toCharArray()转换为char数组 * 然后先调用Arrays的sort方法进行排序,再调 ...
- DateTime字段控件值显示短格式的做法
后台取dateTime格式,前台格式化就好了 <input type="text" name="txtPartyTime" id="txtPar ...
- hdu 5410 CRB and His Birthday(混合背包)
Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...