c#類的使用示例
Notes:
數(shù)據(jù)private類型,大部分方法public類型;
如果有繼承或者相互引用,注意數(shù)據(jù)的公有還是私有,保證數(shù)據(jù)的只讀性質(zhì);
C#不能夠像C++一樣在數(shù)據(jù)聲明時(shí)調(diào)用構(gòu)造函數(shù),必須使用Myclass temp = new Myclass()來調(diào)用構(gòu)造函數(shù);
C#不支持多重繼承關(guān)系, 也就是說,一個(gè)派生類不允許有多個(gè)基類。簡(jiǎn)單點(diǎn)就是,父親可能有好多兒子(父類可以派生出許多子類),但是一個(gè)孩子只能有一個(gè)爸爸(子類不允許多重繼承關(guān)系);
C#和其他面向?qū)ο笠粯?,重載,多態(tài),虛函數(shù),抽象類這些特性都有;
重載和C++中的重載毫無區(qū)別,只有一個(gè)問題是在重載大小比較運(yùn)算符(<, >=, >, <=, ==, !=)的時(shí)候,必須成對(duì)的重載;
虛函數(shù)需加virtual聲明,派生類中需要重寫,并且添加override聲明;
抽象函數(shù)需加abstract聲明,并且基類中沒有執(zhí)行代碼,只能在繼承類中添加;
如果需要傳出多個(gè)值,需要用到ref聲明或out聲明;
ref聲明的對(duì)象必須提前初始化;
out聲明的對(duì)象不需要提前初始化;
如果類中有靜態(tài)對(duì)象,可以使用靜態(tài)構(gòu)造函數(shù)對(duì)靜態(tài)對(duì)象進(jìn)行賦值操作。
Example:
ElemType.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_Class
{
class ElementType
{
private int value;
public ElementType() { value = 0; }
public ElementType(int _v)
{
this.value = _v;
}
public void disp()
{
Console.WriteLine("value is {0}", value);
}
public void edit(int _v)
{
this.value = _v;
}
public int reff()
{
return this.value;
}
}
class package
{
private ElementType x;
private ElementType y;
public package()
{
this.x = new ElementType();
this.y = new ElementType();
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
/// <param name="_x">x</param>
/// <param name="_y">y</param>
public package(int _x, int _y)
{
this.x = new ElementType(_x);
this.y = new ElementType(_y);
}
public void disp()
{
Console.WriteLine("package display");
Console.WriteLine("\tx vaule is {0}", this.x.reff());
Console.WriteLine("\ty vaule is {0}", this.y.reff());
Console.WriteLine();
}
/// <summary>
/// 修改值
/// </summary>
/// <param name="_x">x</param>
/// <param name="_y">y</param>
public void modify(int _x, int _y)
{
this.x.edit(_x);
this.y.edit(_y);
}
public void copyto(ref package p)
{
p.modify(this.x.reff(), this.y.reff());
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test_Class
{
class Program
{
static void Main(string[] args)
{
package p1 = new package(5, 5);
package p0 = new package();
p0.disp();
p1.disp();
p1.copyto(ref p0);
p0.disp();
p0.modify(10, 50);
p0.disp();
Console.ReadLine();
}
}
}
- C#類中方法的執(zhí)行順序是什么
- C#類繼承中構(gòu)造函數(shù)的執(zhí)行序列示例詳解
- C#類的多態(tài)性詳解
- C#類中static變量用法分析
- C#類的訪問修飾符用法分析
- C#類中的屬性使用總結(jié)(詳解類的屬性)
- C#類中屬性與成員變量的使用小結(jié)
- c#對(duì)象初始化順序?qū)嵗治?/a>
- C#對(duì)象為Null模式(Null Object Pattern)實(shí)例教程
- c#對(duì)象反序列化與對(duì)象序列化示例詳解
- C#對(duì)象與XMl文件之間的相互轉(zhuǎn)換
- 自定義實(shí)現(xiàn)Json字符串向C#對(duì)象轉(zhuǎn)變的方法
- 關(guān)于C# 類和對(duì)象詳情
相關(guān)文章
.NET/C# 使用Stopwatch測(cè)量運(yùn)行時(shí)間
這篇文章主要介紹了.NET/C# 使用Stopwatch測(cè)量運(yùn)行時(shí)間,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
C#驗(yàn)證控件validator的簡(jiǎn)單使用
這篇文章主要介紹了C#驗(yàn)證控件validator的簡(jiǎn)單使用方法和示例,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-06-06
C#集合數(shù)據(jù)去重的5種方式及對(duì)比分析
今天我們一起來討論一下關(guān)于C#集合數(shù)據(jù)去重的5種方式并且使用BenchmarkDotNet對(duì)這5種方式進(jìn)行性能基準(zhǔn)對(duì)比測(cè)試分析,每種方法都有其特點(diǎn)和適用場(chǎng)景,我們可以根據(jù)具體需求和執(zhí)行效率選擇一種進(jìn)行使用,需要的朋友可以參考下2024-11-11
協(xié)定需要會(huì)話,但是綁定“BasicHttpBinding”不支持它或者因配置不正確而無法支持它
在IIS7及以上版本服務(wù)器中提供了基于WAS的無.SVC文件的WCF服務(wù)激活功能,能夠提供基于HTTP和非HTTP協(xié)議的訪問,通過添加Windows Server AppFabric可以更方便的管理WCF服務(wù)2012-12-12
WPF拖動(dòng)DataGrid滾動(dòng)條時(shí)內(nèi)容混亂的解決方法
這篇文章主要介紹了WPF拖動(dòng)DataGrid滾動(dòng)條時(shí)內(nèi)容混亂的解決方法2016-10-10
VS2022+unity3D開發(fā)環(huán)境搭建的實(shí)現(xiàn)步驟
本文主要介紹了VS2022+unity3D開發(fā)環(huán)境搭建的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

