ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Porperties(属性)
1.A,示例(Sample) 返回顶部

“属性”示例

本示例演示属性为何是 C# 编程语言必不可少的一个组成部分。它演示了如何声明和使用属性。有关更多信息,请参见属性(C# 编程指南) 。

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行“属性”示例

  1. 在“解决方案资源管理器”中,右击“Person”项目并单击“设为启动项目”。

  2. 在“调试”菜单上,单击“开始执行(不调试)”。

  3. 对 shapetest 重复前面上述步骤。

从命令行生成并运行“属性”示例

  1. 使用“更改目录”命令转到“person”目录。

  2. 键入以下命令:

    csc person.cs
    person
  3. 使用“更改目录”命令转到“shapetest”目录。

  4. 键入以下命令:

    csc abstractshape.cs shapes.cs shapetest.cs
    shapetest
1.B,person 示例代码(Sample Code)返回顶部

1.B.1, person.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。 // person.cs
using System;
class Person
{
private string myName ="N/A";
private int myAge = ; // 声明 string 类型的 Name 属性:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
} // 声明 int 类型的 Age 属性:
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
} public override string ToString()
{
return "Name = " + Name + ", Age = " + Age;
} public static void Main()
{
Console.WriteLine("Simple Properties"); // 创建新的 Person 对象:
Person person = new Person(); // 打印出与该对象关联的姓名和年龄:
Console.WriteLine("Person details - {0}", person); // 设置 Person 对象的某些值:
person.Name = "Joe";
person.Age = ;
Console.WriteLine("Person details - {0}", person); // 递增 Age 属性:
person.Age += ;
Console.WriteLine("Person details - {0}", person);
}
}

1.B.2,

1.B.EXE,

Simple Properties
Person details - Name = N/A, Age =
Person details - Name = Joe, Age =
Person details - Name = Joe, Age =
请按任意键继续. . .

1.B

1.B,shapetest 示例代码2(Sample Code)返回顶部

1.B.1, abstractshape.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。 // abstractshape.cs
// 编译时使用:/target:library
// csc /target:library abstractshape.cs
using System; public abstract class Shape
{
private string myId; public Shape(string s)
{
Id = s; // 调用 Id 属性的 set 访问器
} public string Id
{
get
{
return myId;
} set
{
myId = value;
}
} // Area 为只读属性 - 只需要 get 访问器:
public abstract double Area
{
get;
} public override string ToString()
{
return Id + " Area = " + string.Format("{0:F2}",Area);
}
}

1.B.2, shapes.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。 // shapes.cs
// 编译时使用:/target:library /reference:abstractshape.dll
public class Square : Shape
{
private int mySide; public Square(int side, string id) : base(id)
{
mySide = side;
} public override double Area
{
get
{
// 已知边长,返回正方形的面积:
return mySide * mySide;
}
}
} public class Circle : Shape
{
private int myRadius; public Circle(int radius, string id) : base(id)
{
myRadius = radius;
} public override double Area
{
get
{
// 已知半径,返回圆的面积:
return myRadius * myRadius * System.Math.PI;
}
}
} public class Rectangle : Shape
{
private int myWidth;
private int myHeight; public Rectangle(int width, int height, string id) : base(id)
{
myWidth = width;
myHeight = height;
} public override double Area
{
get
{
// 已知宽度和高度,返回矩形的面积:
return myWidth * myHeight;
}
}
}

1.B.3, shaptest.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。 // shapetest.cs
// 编译时使用:/reference:abstractshape.dll;shapes.dll
public class TestClass
{
public static void Main()
{
Shape[] shapes =
{
new Square(, "Square #1"),
new Circle(, "Circle #1"),
new Rectangle( , , "Rectangle #1")
}; System.Console.WriteLine("Shapes Collection");
foreach(Shape s in shapes)
{
System.Console.WriteLine(s);
} }
}

1.B.EXE,

Shapes Collection
Square # Area = 25.00
Circle # Area = 28.27
Rectangle # Area = 20.00
请按任意键继续. . .

1.B,

1.C,下载地址(Free Download)返回顶部
作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

ylbtech-LanguageSamples-Porperties(属性)的更多相关文章

  1. c3p0连接数据库的3种方式

    c3p0连接数据库的3种方式,这里以mysql为例 1. 直接用set方法设置参数, 基本方法 ComboPooledDataSource dataSource = new ComboPooledDa ...

  2. java:POI导出excel

    POI是一个开源项目,专用于java平台上操作MS OFFICE,企业应用开发中可用它方便导出Excel. 下面是使用示例: 1.maven中先添加依赖项 <dependency> < ...

  3. excel poi 文件导出,支持多sheet、多列自动合并。

    参考博客: http://www.oschina.net/code/snippet_565430_15074 增加了多sheet,多列的自动合并. 修改了部分过时方法和导出逻辑. 优化了标题,导出信息 ...

  4. jqu

    1 /*2 * 说明:3 * 本源代码的中文注释乃Auscarlin呕心沥血所作.旨在促进jQuery的传播以及向广大jQuery爱好者提供一个进阶4 *的途径,以让各位更加深入地了解jQuery,学 ...

  5. Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项

    本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...

  6. Vue.js:监听属性

    ylbtech-Vue.js:监听属性 1.返回顶部 1. Vue.js 监听属性 本章节,我们将为大家介绍 Vue.js 监听属性 watch,我们可以通过 watch 来响应数据的变化: 实例 & ...

  7. Vue.js:计算属性

    ylbtech-Vue.js:计算属性 1.返回顶部 1. Vue.js 计算属性 计算属性关键词: computed. 计算属性在处理一些复杂逻辑时是很有用的. 可以看下以下反转字符串的例子: 实例 ...

  8. 杂项:C# 方法、属性杂项-01

    ylbtech-杂项:C# 方法.属性杂项-01 1. 属性杂项返回顶部 1. public int ReadCnt { get; set; } 2.设置默认值 public int ReadCnt ...

  9. CSS:CSS cursor 属性

    ylbtech-CSS:CSS cursor 属性 1.返回顶部 1. 实例 一些不同的光标: span.crosshair {cursor:crosshair;} span.help {cursor ...

随机推荐

  1. POJ-1681

    Painter's Problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4839   Accepted: 2350 ...

  2. kafka 设置消费者线程数

    http://blog.csdn.net/derekjiang/article/details/9053863 分布式发布订阅消息系统 Kafka 架构设计 - 目前见到的最好的Kafka中文文章 M ...

  3. node自动调试

    supervisor 第一步:安装:npm -g install supervisor没有权限的时候可以sudo npm -g install supervisor 第二步:使用:supervisor ...

  4. AC日记——方差 洛谷 P1471

    方差 思路: 线段树: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct TreeN ...

  5. SpringBoot学习:整合Mybatis,使用HikariCP超高性能数据源

    一.添加pom依赖jar包: <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</ ...

  6. Mindjet Mindmanager复制文件打不开

    概述 使用Mindjet软件画思维导图,保存后得到一个后缀为mmap的文件.复制到一个新的位置,却发现新的文件打不开,导致Mindjet崩溃.这里提供一个解决方案. 解决方案 复制的文件打不开 先打开 ...

  7. angularjs学习笔记2—运行phonecat项目

    如果你去angularjs中文网看它的教程,你会发现一开始它提供了一个phonecat的引导项目,这个项目是angular官方给出的一个类似于demo的教程项目,并配有相应文档,按照这个项目并配合文档 ...

  8. 【hihoCoder 第133周】【hihoCoder 1467】2-SAT·hihoCoder音乐节

    http://hihocoder.com/problemset/problem/1467 2-sat模板...详细的题解请看题目里的提示. tarjan模板打错again致命伤qwq #include ...

  9. [BZOJ1444]有趣的游戏(AC自动机+矩阵乘法)

    n个等长字符串,机器会随机输出一个字符串(每个字母出现的概率为p[i]),问每个字符串第一个出现的概率是多少. 显然建出AC自动机,套路地f[i][j]表示i时刻位于节点j的概率. 构建转移矩阵,当i ...

  10. 【Miller-Rabin算法】

    存个板子,应该是对的吧……没太试 http://www.cnblogs.com/Norlan/p/5350243.html Matrix67写的 根据wiki,取前9个素数当base的时候,long ...