enum Weekday{Mon,Tue,Wed,Thu,Fri,Sat,Sun}; //缺省情况下,第一个常量的值为0,随后每个成员的值递增1;
enum Error{FileNotFound=100,AccessDenied=FileNotFound+100,UnknowError=FileNotFound+400};
//使用表达式来初始化枚举值 |
enum Weekday:short{Mon,Tue,Wed,Thu,Fri,Sat,Sun};
|
enum Weekday{Mon,Tue,Wed,Thu,Fri,Sat,Sun};
Weekday w=Weekday.Mon;
if(w==Weekday.Mon)
{
Console.WriteLine("It's Monday");
}
|
using System;
using System.Collections.Generic; using System.Text;
namespace ConApp
{ //A struct called ComplexNumber struct ComplexNumber { public double Re; public double Im; }
//A struct called ComplexCircle
struct ComplexCircle { public ComplexNumber Origin; public double Radius;
//我们在ComplexCircle结构中添加OnBoundary方法,来判断射线是否与圆周相切
public bool OnBoundary(ComplexNumber z) { double t1 = z.Re - Origin.Re; double t2 = z.Im - Origin.Im;
return ((t1 * t1 + t2 * t2) == Radius * Radius);
} }
//A class called Test
public class Test { static void Main(string[] args) { //Declare a ComplexCircle ComplexCircle c;
//Set the coordinates
c.Origin.Re = 2; c.Origin.Im = 3; c.Radius = 4;
Console.WriteLine("Complex Circle:Centre{0}+{1}i Radius:{2}",c.Origin.Re,c.Origin.Im,c.Radius);
ComplexNumber z1, z2;
z1.Re = 6; z1.Im = 3; z2.Re = 4; z2.Im = 4; Console.WriteLine("6+3i is on the boundary:{0}",c.OnBoundary(z1)); Console.WriteLine("4+4i is on the boundary:{0}",c.OnBoundary(z2)); } } } |
public calss Account
{
//private const string sortCode="12-00-23";
private readonly string sortCode;
public Account(string sortCode)
{
this.sortCode=sortCode;
}
}
|
public class Account
{
private string name;
public void setName(string nm)
{
//do any checking you need,then
name=nm;
}
public int getName()
{
//do any checking you need,then
return name;
}
}
|
public class Account
{
private string name;
//A property called Name,公有属性的命名应遵循Pascal大小写输入规则,首字母应大写
public string Name()
{
get
{
return name;
}
set
{
name=value;
}
}
}
|
Account a = new Account();
a.Name="Mary";
string myName=a.Name;
|
public class Account
{
private double overDraftLimit;
//A property called OverDraftLimit,命名应遵循Pascal大小写输入规则,首字母应大写
public double OverDraftLimit()
{
get
{
return overDraftLimit;
}
set
{
if(value < 0)
throw new ArgumentOutOfRangeException("Invalid overdraf limit");
else
overDraftLimit=value;
}
}
}
|
public class Account
{
private static double penalty;
public static double Penalty()
{
set
{
penalty=value;
}
get
{
return penalty;
}
}
}
|
Account.Penalty=25; //设置透支限额为25
double thePenalty=Account.Penalty;
|
public class SavingsAccount:Account //Account is a class
{
……
}
|
SavingsAccount sa=new SavingsAccount ();
//Refer to it through a base class reference
Account a1=s1;
|
public class Account
{
public Account(double amt)
{
……
}
……
}
public class SavingsAccount:Account //派生
{
public SavingsAccount(double amt):base(amt)
{
}
……
}
|
public class Account
{
public virtual bool Withdraw(double amt)
{
……
}
……
}
|
public class SavingsAccount:Account //派生
{
public override bool Withdraw(double amt)
{
……
}
……
}
|
public class Account
{
public bool Withdraw(double amt)
{
……
}
……
}
public class SavingsAccount:Account //派生
{
public new bool Withdraw(double amt) //如不使用new关键字,则编译器会发出错误
{
//修改后的代码
}
……
}
|
Account s1=new SavingsAccount(); //这里引用了一个SavingsAccount对象
s1.Withdraw(2000.0); //这里调用的是Account类的Withdraw方法。
|
public class SavingsAccount:Account
{
public new bool Withdraw(double amt) //如不使用new关键字,则编译器会发出错误
{
if (amt>=50)
else
throw new ArgumentOutOfRangeException("SavingsAccountWithdraw:Insufficient Withdraw!");
}
……
}
|
public abstract class Account
{
……
}
|
//Create a savings Account
Account s1 = new SavingsAccount(); //OK
Account s1=new Account(); //Error!!
|
public sealed class MySecureClass
{
……
}
|
方 法 访问修饰符 作用
string ToString() public virtual 返回对象的字符串表示
int GetHashTable() public virtual 在实现字典(散列表)时使用
bool Equals(object obj) public virtual 对对象的实例进行相等比较
bool Equals(object objA,object objB) public static 对对象的实例进行相等比较
bool ReferenceEquals(object objA,object objB) public static 比较两个引用是否指向同一个对象
Type GetType() public 返回对象类型的详细信息
object MemberwiseClone() protected 进行对象的浅表复制
void Finalize() protected virtual 该方法是析构函数的.NET版本
|
一元运算符 + - ! ~ ++ -- true flase
二元运算符 + - * / % & | << >>
比较运算符 == != < > <= >== //比较运算符必须被成对重载,如== 和 != 必须在一起重载
|
public class Currency
{
double val;
public Currency(double val)
{
this.val=val;
}
//重载一元运算符,使得我们能简单地将两个Currency对象相加,而不用自定义的Add()方法。
public static Currency operator+(Currency lhs , Currency lhs) {
return ( new Currency (lhs.val + rhs.val));
}
public static bool operator== (Currency lhs , Curreny rhs) //重载比较运算符
{
return (lhs.val == rhs.val);
}
public static bool operator!= (Currency lhs , Curreny rhs) //与==运算符成对重载
{
return (lhs.val != rhs.val);
}
}
|
鹰能够飞,但不能游水
鸵鸟不能飞也不能游水
企鹅不能飞但能游水
鸬鹚既能飞也能游水
|
interface IFlying
{
void Fly();
}
interface ISwimming
{
void Swim();
}
//Hawk is a bird that can fly
class Hawk:Bird,IFlying
{
public void Fly(){……};
}
//Cormorant is a bird that can fly and swim
class Cormorant:Bird,IFlying,ISwimming
{
public void Fly(){……};
public void Swim(){……};
}
……
|
interface ISwimming
{
void Swim();
}
//IDiving实现Dive()的同时也必须实现Swim()
interface IDiving :ISwimming
{
public Dive(){……};
}
|