Microsoft Word - 04_object_Boxing_property_indexer.doc

Similar documents
1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

untitled

untitled

untitled

untitled

Microsoft Word - ch04三校.doc

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

Chapter 9: Objects and Classes

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

第3章.doc

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

( 总 第 1073 期 ) 浙 江 省 人 民 政 府 主 办 2015 年 3 月 17 日 出 版 省 政 府 令 省 政 府 文 件 目 录 浙 江 省 大 型 群 众 性 活 动 安 全 管 理 办 法 ( 浙 江 省 人 民 政 府 令 第 333 号 ) (3) 浙 江 省 人 民 政

untitled

Chapter 9: Objects and Classes


Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

Microsoft PowerPoint - plan08.ppt

Microsoft PowerPoint - L17_Inheritance_v4.pptx

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

CC213

认定编号

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

高雄市左營國民小學八十九學年度第一學期一年級總體課程教學進度表

Chapter 1 What is Programing Paradigm 1

EJB-Programming-4-cn.doc

從詩歌的鑒賞談生命價值的建構

c_cpp

2 WF 1 T I P WF WF WF WF WF WF WF WF 2.1 WF WF WF WF WF WF

EJB-Programming-3.PDF

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

穨CY03519.PDF

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

針灸治療膝關節疼痛綜述

untitled

Learning Java

The Embedded computing platform

NethersoleJO89(8).indd

《大话设计模式》第一章

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

untitled

提纲 1 2 OS Examples for 3

2009年3月全国计算机等级考试二级Java语言程序设计笔试试题

chp6.ppt

Java

南華大學數位論文

The Development of Color Constancy and Calibration System

Chapter 9: Objects and Classes

投影片 1

CWP156.pdf

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

FY.DOC

epub83-1

2007

運算子多載 Operator Overloading

2/80 2

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

大连软~1

C/C++语言 - C/C++数据

untitled


香港藝術發展局委託報告

電機工程系認可證照清單 /7/1

任務二 : 產生 20 個有炸彈的磚塊, 放在隨機的位置編輯 Block 類別的程式碼 import greenfoot.; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) Write a description of class

, 即 使 是 在 昏 暗 的 灯 光 下, 她 仍 然 可 以 那 么 耀 眼 我 没 有 地 方 去, 你 会 带 着 我 么 杜 晗 像 是 在 嘲 笑 一 般, 嘴 角 的 一 抹 冷 笑 有 着 不 适 合 这 个 年 龄 的 冷 酷 和 无 情, 看 着 江 华 的 眼 神 毫 无 温

9330.doc

<4D F736F F D205F FB942A5CEA668B443C5E9BB73A740B5D8A4E5B8C9A552B1D0A7F75FA6BFB1A4ACFC2E646F63>

第1章

<4D F736F F D20B2F8A74AA4AF5FA578C657A175BCC6A6ECB6D7AC79A176BB50A46AB3B0A175A454BAF4A658A440A176AC46B5A6A641B1B4>

FileMaker 16 ODBC 和 JDBC 指南

mvc

1.5招募说明书(草案)

JavaIO.PDF

1 引言

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

<ADB6ADB1C25EA8FAA6DB2D4D56432E706466>

目 录 第 一 部 分 档 案 局 概 况 一 主 要 职 责 二 部 门 决 算 单 位 构 成 第 二 部 分 档 案 局 2016 年 度 部 门 预 算 表 一 2016 年 度 市 级 部 门 收 支 预 算 总 表 二 2016 年 度 市 级 部 门 支 出 预 算 表 三 2016

2015 年 度 收 入 支 出 决 算 总 表 单 位 名 称 : 北 京 市 朝 阳 区 卫 生 局 单 位 : 万 元 收 入 支 出 项 目 决 算 数 项 目 ( 按 功 能 分 类 ) 决 算 数 一 财 政 拨 款 一 一 般 公 共 服 务 支 出 二

20-25%,

untitled

第 一 节 认 识 自 我 的 意 义 一 个 人 只 有 认 识 自 我, 才 能 够 正 确 地 认 识 到 自 己 的 优 劣 势, 找 出 自 己 的 职 业 亮 点, 为 自 己 的 顺 利 求 职 推 波 助 澜 ; 一 个 人 只 有 认 识 自 我, 才 能 在 求 职 中 保 持

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

Microsoft Word - xb 牛尚鹏.doc

CC213

<4D F736F F D B0D3B77EC3FEA7DEC3C0C476C1C9A5BFA6A1B8D5C3442DB57BA6A1B35DAD702DBEC7ACEC2E646F6378>

提问袁小兵:

全国计算机技术与软件专业技术资格(水平)考试

附录J:Eclipse教程


Guide to Install SATA Hard Disks

5 : 67 ( ),,,,, : (1),,,,,,,, ( ),, ( : ) [ 1 ],, (2),,,, ;,,, [ 2 ] ( P1 2),,, (3),, [ 1 ] ( P1 1),,, [ 3 ] ( P1364),,,,, [ 4 ] ( P1135),,,,,,, [ 5 ]

國立中山大學學位論文典藏.PDF

K7VT2_QIG_v3

新版 明解C++入門編

C++ 程式設計

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Transcription:

C# 程式設計人員參考 object 型別是.NET Framework 中,System.Object 的別名 您可以將 任何型別的值指派給 object 型別的變數 所有的資料型別, 包括預先定義的和使用者定義的, 都繼承自 System.Object 類別 object 資料型別是物件 Box 目標或來源的型 別 範例下列範例顯示 object 型別的變數如何接受任何資料型別的值, 以及 object 型別的變數可如何使用.NET Framework 內的方法以應用在 System.Object 上 // keyword_object.cs using System; public class MyClass1 { public int i = 10; public class MyClass2 { public static void Main() { object a; a = 1; // an example of boxing 2006/3/2 1

Console.WriteLine(a); Console.WriteLine(a.GetType()); Console.WriteLine(a.ToString()); Console.WriteLine(); a = new MyClass1 (); MyClass1 ref_myclass1; ref_myclass1 = (MyClass1)a; Console.WriteLine(ref_MyClass1.i); 輸出 1 System.Int32 1 10 2006/3/2 2

Boxing Boxing that an object is automatically created from a value (that is of some value type). So a value can be used in contexts where an object is expected. 範例 : ArrayList tarraylist=new ArrayList(); tarraylist.add(27); // 將 27 轉成 object 存入 tarraylist 中. 此為 Boxing The reverse process (called unboxing) is also possible: int tvalue = (int) tarraylist[0]; Automatic boxing and unboxing are not present in Java. Instead, in Java, you have to do these tasks explicitly using the wrapper types. // SamplesArrayList.cs using System; using System.Collections; public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList. ArrayList myal = new ArrayList(); myal.add("hello"); myal.add("world"); myal.add("!"); myal.add(3); // 先將 3 box 成 object, 再放進 List 中. 2006/3/2 3

// Displays the properties and values of the ArrayList. Console.WriteLine( "myal" ); Console.WriteLine( "\tcount: {0", myal.count ); Console.WriteLine( "\tcapacity: {0", myal.capacity ); Console.Write( "\tvalues:" ); PrintValues( myal ); public static void PrintValues( IEnumerable mylist ) { System.Collections.IEnumerator myenumerator = mylist.getenumerator(); while ( myenumerator.movenext() ) Console.Write( "\t{0", myenumerator.current ); Console.WriteLine(); /* This code produces the following output. myal Count: 4 Capacity: 16 Values: Hello World! 3 */ 2006/3/2 4

C# 語言規格 4.3.1 Box 轉換 Box 轉換允許數值型別隱含地轉換成參考型別 因此出現下列 Box 轉換 : 從任何數值型別 ( 包含任何列舉型別 ) 轉換成 object 型別 從任何數值型別 ( 包含任何列舉型別 ) 轉換成 System.ValueType 型別 從任何數值型別轉換成數值型別實作的任何介面型別 從任何列舉型別轉換成 System.Enum 型別 Boxing 一個數值型別的數值時, 會包含配置一個物件執行個體, 並將數值型別數值複製到該執行個體中 說明數值型別值的 Boxing 實際程序的好方法, 是想像該類型別有個 Boxing 類別 針對任何數值型別 T 而言,boxing 類別會表現它好像是由下列方式宣告一樣 : sealed class T_Box: System.ValueType { T value; public T_Box(T t) { value = t; 現在為型別 T 的 v 值進行 Boxing 會執行 new T_Box(v) 運算式, 並將產生的執行個體傳回當作 object 型別的值 因此, 下面這段陳述式 int i = 123; object box = i; 2006/3/2 5

應該會對應到 int i = 123; object box = new int_box(i); 上述 T_Box 和 int_box 之類的 Boxing 類別實際上並不存在, 而一個 Boxed 值的動態型別也並非真的是一個類別型別 相反地, 型別 T 的一個 Boxed 值會擁有動態型別 T, 而使用 is 運算子的動態型別檢查只會參考型別 T 例如, int i = 123; object box = i; if (box is int) { Console.Write("Box contains an int"); 將會在主控台上輸出 Box contains an int 字串 一個 Box 轉換表示將會為要 Boxed 的值建立複本 這點和將參考型別轉換成 object 型別是不同的, 在這當中, 數值會繼續參考相同的執行個體, 且只是當作較少衍生的 object 型別 例如, 給定下列宣告 struct Point{ public int x, y; public Point(int x, int y) { this.x = x; this.y = y; 下列陳述式 2006/3/2 6

Point p = new Point(10, 10); object box = p; p.x = 20; Console.Write(((Point)box).x); 將會在主控台上輸出值 10, 因為出現在 p 的設定以隱含 Boxing 運算到 box, 會產生要複製的 p 值 如果 Point 已宣告成 class, 輸出結果就是值 20, 是因為 p 和 box 會參考相同的執行個體 2006/3/2 7

Properties Besides constructors, fields and methods, in.net languages, a class type (or a struct type) may also declare properties. Properties should be used instead of providing get and set methods. In the following example, the class Point is providing a property called X: 範例 : public class Point { private int ix; private int iy; public Point(int px, int py){ ix = px; iy = py; public int X{ get { return ix; set { ix = value; public int Y{ get { return iy; set { iy = value; public class Test{ public static void Main(string [] args){ Point tpoint = new Point(100, 200); int tx = tpoint.x; // uses get tpoint.x = 150; // uses set tpoint.x++; // uses get and set 2006/3/2 8

Indexers and operator overloading In C#, a class type (or a struct type) may declare indexers and/or operators. In the same way that a property provides access to one value, an indexer provides access to an array of values. An operator declaration permits a class (or a struct) to define new meanings for existing operators (such as ==, <, +, ++). 2006/3/2 9