C# 面向对象5 this关键字和析构函数
this关键字
1.代表当前类的对象
2.在类当中显示的调用本类的构造函数(避免代码的冗余)
语法: ":this"
以下一个参数的构造函数调用了参数最全的构造函数!并赋值了那些不需要的属性!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _04面向对象练习
{
public class Student
{
//字段,属性,方法,构造函数 //构造函数
public Student(string name,int age,char gender,int chinese,int math,int english)
{
//
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Chinese = chinese;
this.Math = math;
this.English = english;
} //******
//this 关键字的用法
public Student(string name):this(name,,'a',,,)
{
//this.Name = name;
} public Student()
{
Console.WriteLine("Hello!");
} private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
} private int _age;
public int Age
{
get { return _age; }
set {
if (value < || value > )
value = ; _age = value; }
} private char _gender;
public char Gender
{
get {
if(_gender !='男'&&_gender!='女')
return _gender='男'; return _gender; }
set { _gender = value; }
} private int _chinese;
public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
} private int _math;
public int Math
{
get { return _math; }
set { _math = value; }
} private int _english;
public int English
{
get { return _english; }
set { _english = value; }
} public void SayHello()
{
Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生",this.Name,this.Age,this.Gender);
} public void ShowScore()
{
Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English)/);
} }
}
析构函数
当程序结束的时候,才会执行析构函数.
作用,不必等GC来回收垃圾,可以调用析构函数马上回收垃圾!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace _04面向对象练习
{
public class Student
{
//字段,属性,方法,构造函数 //析构函数
//当程序结束的时候,析构函数才会执行
//帮助我们释放资源
//GC:垃圾回收器可以自动回收垃圾
//使用析构函数可以马上回收垃圾,不必等GC来回收!
~Student()
{
Console.WriteLine("析构函数");
} //构造函数
public Student(string name,int age,char gender,int chinese,int math,int english)
{
//
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Chinese = chinese;
this.Math = math;
this.English = english;
} //******
//this 关键字的用法
public Student(string name):this(name,,'a',,,)
{
//this.Name = name;
} public Student()
{
Console.WriteLine("Hello!");
} private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
} private int _age;
public int Age
{
get { return _age; }
set {
if (value < || value > )
value = ; _age = value; }
} private char _gender;
public char Gender
{
get {
if(_gender !='男'&&_gender!='女')
return _gender='男'; return _gender; }
set { _gender = value; }
} private int _chinese;
public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
} private int _math;
public int Math
{
get { return _math; }
set { _math = value; }
} private int _english;
public int English
{
get { return _english; }
set { _english = value; }
} public void SayHello()
{
Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生",this.Name,this.Age,this.Gender);
} public void ShowScore()
{
Console.WriteLine("我叫{0},我的总成绩是{1},平均成绩是{2}", this.Name, this.Chinese + this.Math + this.English, (this.Chinese + this.Math + this.English)/);
} }
}
***Xmind脑图软件
C# 面向对象5 this关键字和析构函数的更多相关文章
- PHP 面向对象中常见关键字使用(final、static、const和instanceof)
PHP 面向对象中常见关键字的使用: 1.final :final关键字可以加在类或者类中方法之前,但是不能使用final标识成员属性. 作用: 使用final标识的类,不能被继承. 在类中使用fin ...
- Java面向对象-package import关键字
Java面向对象-package import关键字 package包关键字,在java中,有包的概念,主要是用来归类 分类作用: 便于项目的开发和维护: 这里截取随便截取一个我最近在开发的一个开源工 ...
- Java面向对象-构造方法,this关键字
Java面向对象-构造方法,this关键字 构造方法 概念:构造方法是一个特殊的方法,这个特殊方法用于创建实例时执行初始化操作: 上代码: package com.java1234.chap03.se ...
- 081 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 01 初识面向对象 06 new关键字
081 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 01 初识面向对象 06 new关键字 本文知识点:new关键字 说明:因为时间紧张,本人写博客过程中只是 ...
- 2016/3/21 面向对象: ①定义类 ②实例化对象 ③$this关键字 ④构造函数 ⑤析构函数 ⑥封装 ⑦继承
一:定义类 二:实例化对象 //定义类 class Ren { var $name; var $sex; var $age; function Say() { echo "{$this- ...
- PHP面向对象常见的关键字和魔术方法
在PHP5的面向对象程序设计中提供了一些常见的关键字,用来修饰类.成员属性或成员方法,使他们具有特定的功能,例如final.static.const等关键字.还有一些比较实用的魔术方法,用来提高类或对 ...
- [java学习笔记]java语言核心----面向对象之this关键字
一.this关键字 体现:当成员变量和函数的局部变量重名时,可以使用this关键字来区别:在构造函数中调用其它构造函数 原理: 代表的是当前对象. this就是所在函数 ...
- php 面向对象:this 关键字
PHP5中为解决变量的命名冲突和不确定性问题,引入关键字“$this”代表其所在当前对象. $this在构造函数中指该构造函数所创建的新对象. 在类中使用当前对象的属性和方法,必须使用$this-&g ...
- Java学习笔记之面向对象、static关键字
一周Java学习总结 今天就总结理清一下关于面向对象和面向过程的程序设计的一些不同特点,以及讲下static关键字. 面向对象 现在接触的Java是面向对象的,现在的程序开发几乎都是以面向对象为基础的 ...
随机推荐
- JAVA常见工具配置
1.MyEclipse中配备struts.xml的自动提示 https://jingyan.baidu.com/article/9158e0004054baa2541228e2.html 2.MySQ ...
- PL/SQL的命令行窗口中执行脚本
注意脚本路径中不能有空格, 格式如下:SQL>@D:\1211_Export\all.sql 备注: @后面接本地sql文件的路径及执行文件
- weblogic性能调优
1.设置java参数: a) 编辑Weblogic Server启动脚本文件: /user_projects/domains/Domain_jgbs/bin/startWebLogic.sh /use ...
- idea出现灰色或者黄色的波浪线如何去除
1.File--setting--Editor-Inspections-Geneal-Duplicated Code 去除 主要是类中出现太多的重复代码,idea自动提示.
- [占位符 ]
在做项目的时候,数据库中的数据会存在空值;这样,我们需要在前台给它加以判断, 如果我们不加以判断也是可行的,我们需要添加一个空白占位符 空白占位符 是个不错的选择,这样我们的页面显示数据的时候就不会 ...
- [go]new和make开辟内存
var申明取址和new效果一样 值类型 引用类型 make和new的区别 内置函数new按指定类型长度分配零值内存,返回指针,并不关心类型内部构造和初始化方式. 而引用类型则必须使用make函数创建, ...
- JNI知识扩展
JNI(Java Native Interface,JAVA原生接口) 使用JNI可以使Java代码和其他语言写的代码(如C/C++代码)进行交互. 问:为什么要进行交互? |- 首先,Java语言提 ...
- WPF Slider Tickbar 中显示数值
class CustomTickBar : TickBar { protected override void OnRender(System.Windows.Media.DrawingContext ...
- Centos7.4 yum 安装MariaDB
#系统及版本选择:https://downloads.mariadb.org/mariadb/repositories/#mirror=tunavim /etc/yum.repos.d/MariaDB ...
- JMeter使用plugins插件进行服务器性能监控
JMeter使用plugins插件进行服务器性能监控 性能测试时,我们的关注点有两部分 1 服务本身:并发响应时间 QPS 2 服务器的资源使用情况:cpu memory I/O disk等 JMet ...