收藏 分销(赏)

2024年c语言面向对象程序设计复习.doc

上传人:快乐****生活 文档编号:8165503 上传时间:2025-02-05 格式:DOC 页数:30 大小:198.04KB 下载积分:12 金币
下载 相关 举报
2024年c语言面向对象程序设计复习.doc_第1页
第1页 / 共30页
2024年c语言面向对象程序设计复习.doc_第2页
第2页 / 共30页


点击查看更多>>
资源描述
C#设计模式(1) 课程内容:设计模式 起源:亚历山大的建筑模式、Gamma等人(1995)创作的"Design Patterns: Elements of Reusable Software"。这本书一般被称作"Gang of Four"或"GoF",开创性的创造了《设计模式》。 也有人说"三十六计"就是"模式"。 一、 C# 面对对象程序设计复习   点击下载,内容包括:   字段与属性.cs using System; class Account { private double balance = 0; //字段 public double Balance //属性 { get { return balance; } set { balance = value;} } /*============================================================= * 我们能够通过修改get、set措施达成控制存取的目标。 * 例如: * * 1)只读属性 * public double Balance //属性 * { * get { return balance; } * set { } * } * * 2)读写控制 * public double Balance * { * get * { * if(Console.ReadLine()=="1234") * return balance; * else * return -9999999; * } * set { } * } * ============================================================= */ public void Deposit(double n) { this.balance += n; } public void WithDraw(double n) { this.balance -= n; } } class Client { public static void Main() { Account a = new Account(); a.Balance = 1000; // 能够读写属性,因为属性Balance是public型的 //a.balance = 1000; //不能够读写字段,因为字段balance是private型的 a.WithDraw(500); a.Deposit(); Console.WriteLine(a.Balance); } }   属性、措施作用范围.cs using System; class Base { /* * public 的可访问范围是所有类 * private 的可访问范围是目前类 * protected 的可访问范围是目前类及其子类 */ public string name = "Tom"; private double salary = 1500; protected int age = 20; public virtual void ShowInfo() { Console.WriteLine(this.name); //能够,因为name是 public 型的 Console.WriteLine(this.salary); //能够,salary是private型,在Base类中能够访问 Console.WriteLine(this.age); //能够,因为age是protected型,在子类中能够访问 } } class Derived : Base { public override void ShowInfo() { Console.WriteLine(this.name); //能够,因为name是 public 型的 //Console.WriteLine(this.salary); //不能够,salary是private型,超出Base就无法访问 Console.WriteLine(this.age); //能够,因为age是protected型,在子类中能够访问 } } class Client { public static void Main() { Base b = new Base(); Console.WriteLine(b.name); //能够,因为name是 public 型的 //Console.WriteLine(this.salary); //不能够,salary是private型,超出Base就无法访问 //Console.WriteLine(this.age); //不能够,因为age是protected型,Client不是Base的子类 Console.WriteLine("=========================="); b.ShowInfo(); Console.WriteLine("=========================="); Derived d = new Derived(); d.ShowInfo(); } }   一加到一百.cs using System; class SumToHundred { public static void Main() { int sum=0; for(int i=1; i<=100; i++) sum += i; Console.WriteLine(sum); } }   使用接口排序(1).cs using System; using System.Collections; public class Person : IComparable { public int ID; public string Rank; public Person(int id, string rank) { this.ID=id; this.Rank = rank; } #region IComparable Members /* * IComparable 接口只有一个措施: CompareTo。CompareTo措施 * 只接收一个object类型的参数,这意味着它能够接收任何类 * 型的数据(object是所有类的父类),这个措施会返回一 * 整型数值,含义如下: * * 1) 小于零,目前实例(this)小于obj对象 * 2) 等于零,目前实例(this)等于obj对象 * 3) 不小于零,目前实例(this)不小于obj对象 * * Int32,Int16...,String,Decimal等数据类型都已经实现了IComparable接口 */ public int CompareTo(object obj) { Person p = (Person)obj; return this.ID.CompareTo(p.ID); } #endregion } class SortArrayList { static void Main(string[] args) { ArrayList list = new ArrayList(); list.Add(new Person(6, "排长")); list.Add(new Person(3, "团长")); list.Add(new Person(4, "司令")); list.Add(new Person(5, "旅长")); list.Add(new Person(7, "连长")); list.Add(new Person(1, "军长")); list.Add(new Person(2, "营长")); list.Add(new Person(8, "师长")); list.Sort(); Console.WriteLine("After Sorting"); foreach (Person person in list) { Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank); } } }   使用接口排序(2).cs using System; using System.Collections; public enum enuSortOrder {IDAsc, IDDesc, RankAsc, RankDesc} public class Person : IComparable { public static enuSortOrder intSortOrder = enuSortOrder.IDAsc; public int ID; public string Rank; public Person(int id, string rank) { this.ID=id; this.Rank = rank; } #region IComparable Members /* * IComparable 接口只有一个措施: CompareTo。CompareTo措施 * 只接收一个object类型的参数,这意味着它能够接收任何类 * 型的数据(object是所有类的父类),这个措施会返回一 * 整型数值,含义如下: * * 1) 小于零,目前实例(this)小于obj对象 * 2) 等于零,目前实例(this)等于obj对象 * 3) 不小于零,目前实例(this)不小于obj对象 * * Int32,Int16...,String,Decimal等数据类型都已经实现了IComparable接口 */ public int CompareTo(object obj) { Person p = (Person)obj; switch ((int)intSortOrder) { case (int)enuSortOrder.IDAsc: return this.ID.CompareTo(p.ID); case (int)enuSortOrder.IDDesc: return p.ID.CompareTo(this.ID); case (int)enuSortOrder.RankAsc: return RankCompare(this.Rank, p.Rank); case (int)enuSortOrder.RankDesc: return RankCompare(p.Rank, this.Rank); default: return this.ID.CompareTo(p.ID); } } private int RankCompare(string rank1, string rank2) { int intRank1 = ConvertRankToInt(rank1); int intRank2 = ConvertRankToInt(rank2); if(intRank1 < intRank2) return -1; else if(intRank1 == intRank2) return 0; else return 1; } private int ConvertRankToInt(string rank) { if(rank == "司令") return 8; else if(rank == "军长") return 7; else if(rank == "师长") return 6; else if(rank == "旅长") return 5; else if(rank == "团长") return 4; else if(rank == "营长") return 3; else if(rank == "连长") return 2; else return 1; } #endregion } class SortArrayList { static void Main(string[] args) { ArrayList list = new ArrayList(); list.Add(new Person(6, "排长")); list.Add(new Person(3, "团长")); list.Add(new Person(4, "司令")); list.Add(new Person(5, "旅长")); list.Add(new Person(7, "连长")); list.Add(new Person(1, "军长")); list.Add(new Person(2, "营长")); list.Add(new Person(8, "师长")); list.Sort(); Console.WriteLine("Sort By ID Asc:"); foreach (Person person in list) { Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank); } Console.WriteLine("----------------------------"); Console.WriteLine("Sort By ID Desc:"); Person.intSortOrder = enuSortOrder.IDDesc; list.Sort(); foreach (Person person in list) { Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank); } Console.WriteLine("----------------------------"); Console.WriteLine("Sort By Rank Asc:"); Person.intSortOrder = enuSortOrder.RankAsc; list.Sort(); foreach (Person person in list) { Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank); } Console.WriteLine("----------------------------"); Console.WriteLine("Sort By Rank Desc:"); Person.intSortOrder = enuSortOrder.RankDesc; list.Sort(); foreach (Person person in list) { Console.WriteLine("ID: " + person.ID.ToString() + ", Rank: " + person.Rank); } } }   求质数.cs using System; class Factor { public static void Main() { for(int i=1; i<=100; i++) if(IsPrime(i)) Console.WriteLine(i); } public static bool IsPrime(int n) { for(int i=2; i<=Math.Sqrt(n); i++) if(n%i == 0) return false; return true; } }   冒泡法排序.cs using System; class ArraySort { public static void Main() { int[] d = {10,15,21,43,17,98,2,74,63,10}; int temp; //冒泡法排序 for(int i=0; i<d.Length; i++) for(int j=i+1; j<d.Length; j++) if(d[i]<d[j]) { temp = d[i]; d[i]=d[j]; d[j]=temp; } //输出排序成果 foreach(int i in d) Console.Write("{0}, ", i); } }   九九表.cs using System; public class JiuJiuBiao { public static void Main(string[] args) { int i,j; for(i=1; i<10; i++) { for(j=1; j<10; j++) { Console.Write("{0:D1}*{1:D1}={2,2} ", i, j, i*j); } Console.WriteLine(""); } Console.ReadLine(); } }   静态与非静态.cs using System; class StaticHello { public static void SayHello() { Console.WriteLine("Static Hello"); } } class NonStaticHello { public void SayHello() { Console.WriteLine("Non Static Hello"); } } class Client { public static void Main() { //静态措施调用应当使用 “类名.措施” StaticHello.SayHello(); //非静态措施调用应当使用 “实例名称.措施” NonStaticHello h = new NonStaticHello(); h.SayHello(); } }   结构函数.cs using System; public class Person { public string name = ""; public int age = 0; //默认结构函数 public Person() { } //结构函数重载(1) public Person(int Age) { this.age = Age; } //结构函数重载(2) public Person(int Age, string Name) { this.age = Age; this.name = Name; } public void ShowInfo() { Console.WriteLine("The name is : " + name); Console.WriteLine("The age is:" + age); } } class Client { public static void Main() { Person p1 = new Person(); p1.ShowInfo(); Console.WriteLine("=========================="); Person p2 = new Person(30); p2.ShowInfo(); Console.WriteLine("=========================="); Person p3 = new Person(30, "Tom"); p3.ShowInfo(); } }   措施重载.cs using System; class Client { public static void Main() { //重载是指措施名相同,措施的署名不一样 Console.WriteLine(Add(10,5)); Console.WriteLine(Add("10","5")); } public static string Add(string a, string b) { return a + " add " + b; } public static int Add(int a, int b) { return a+b; } }   多态性.cs using System; class Car { public virtual void Drive() { Console.WriteLine("Drive Car"); } } class Truck : Car { public override void Drive() { Console.WriteLine("Drive Truck"); } } class Client { public static void Main() { Car c = new Truck(); c.Drive(); //多态性决定着将调用Truck的Drive措施 } }   递归求阶乘.cs using System; class Factor { public static void Main() { for(int i=1; i<=10; i++) Console.WriteLine("{0} 的阶乘是 {1}",i, Factorial(i)); } public static long Factorial(long n) { if(n == 1) return 1; else return n * Factorial(n-1); } }   打印三角形.cs using System; public class Hello { public static void Main() { Console.Write("请输入行数:"); int lines = int.Parse(Console.ReadLine()); Console.WriteLine(""); for(int i=1; i<=lines ; i++) { for(int k=1; k<= lines-i; k++) Console.Write(" "); for(int j=1; j<=i*2+1; j++) Console.Write("*"); Console.WriteLine(""); } Console.ReadLine(); } }   传值调用与引用调用.cs using System; class MethodCall { public static void Main() { /* * 参数类型分为 in, ref, out 三种,默以为 in。 * in 类型在子措施中修改了对应变量后,主措施中的值不会发生变化。 * ref 类型在子措施中修改了对应变量后,主措施中的值也会发生变化。 * out 主措施中对应的变量不需要初始化。 * */ int a = 3, b = 4, c; Console.WriteLine("Before Method Call : a = {0}, b = {1}, c 未赋值", a, b); AMethod(a, ref b, out c); Console.WriteLine("After Method Call : a = {0}, b = {1}, c = {2}", a, b, c); } public static void AMethod(int x, ref int y, out int z) { x = 7; y = 8; z = 9; } } 二、 设计模式举例 在设计模式中有一个模式叫Builder模式,其原理如下: 我们能够将Builder了解成电饭锅,给这个Builder放进去米和水,通过Builder的Build后,我们就能够取出香喷喷的米饭了。 C#中有一个类叫StringBuilder,输入必要的信息后,就能够取出对应的String。其使用措施如下: using System; using System.Text; class Exam {  public static void Main()  {   StringBuilder sb = new StringBuilder();   sb.Append('a',2);   sb.Append('b',3);   sb.Append('c',4);   Console.WriteLine(sb.ToString()); //打印出 aabbbcccc   sb.Remove(0, sb.Length); //清除sb中的所有信息  } } 程序执行成果为: aabbbcccc 请使用StringBuilder对如下打印三角型的程序进行改写,写出新程序。 using System; public class Exam {  public static void Main()  {   Console.Write("请输入行数:");   int lines = int.Parse(Console.ReadLine());   Console.WriteLine("");   for(int i=1; i<=lines ; i++)   {    for(int k=1; k<= lines-i; k++)     Console.Write(" ");    for(int j=1; j<=i*2-1; j++)     Console.Write("*");    Console.WriteLine("");   } } } 答: using System; using System.Text; class Exam {  public static void Main()  {   Console.Write("请输入行数:");   int lines = int.Parse(Console.ReadLine());   Console.WriteLine("");   StringBuilder sb = new StringBuilder();   for(int i=1; i<=lines ; i++)   {    sb.Append(' ', lines-i);    sb.Append('*', i*2-1);    Console.WriteLine(sb.ToString());    sb.Remove(0, sb.Length);   }  } } 三、 先有鸡还是先有蛋? 到底是先有鸡还是先有蛋?看下面的代码: using System; class Client {    public static void Main ()    {       Base b = new Base();       Derived d = new Derived();       b.d = d;       Console.WriteLine(b.d.m);          } } class Base {    public int n = 9;    public Derived d; } class Derived : Base {    public int m = 10;    } Derived继承自Base,能够说没有Base就没有Derived,可Base里面有一个组员是Derived类型。到底是先有鸡还是先有蛋?这个程序能够正常编译执行并打印成果10。 四、 大瓶子套小瓶子还是小瓶子套大瓶子? 另外一个例子: using System; class Client {    public static void Main ()    {       A a = new A();       B b = new B();       a.b = b;       b.a = a;    } } class A {    public B b; } class B {    public A a;  } 上面的代码似乎描述了"a包括b,b包括a"的关系,到底是大瓶子套小瓶子还是小瓶子套大瓶子呢? 五、 .net本质 有关"先有鸡还是先有蛋"的程序,系统运行后,内存结构如下:   由图中能够看出,根本不存在鸡与蛋的问题,而是型与值的问题以及指针引用的问题。 有关"大瓶子套小瓶子还是小瓶子套大瓶子"问题,系统运行后,内存结构如下:   因为是指针引用,因此也无所谓大瓶子还是小瓶子了。 其实上述问题的一个核心点是:值类型和引用类型的问题。 引用类型其实有2个部分:1个是他的“引用柄”,它是引用的外在体现。第2个是对象,是我们用“引用类型”时真正想要的东西(对象)。它是引用的内在实体。 当我们用“引用类型”时:是通过“引用柄”来引用“对象”。我们真正引用到的是内在的对象,而不是外在的“引用柄”。 有关更多内容能够参考《.NET本质论 第1卷:公共语言运行库》。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服