面向对象程序设计编程 实验 上机.doc
《面向对象程序设计编程 实验 上机.doc》由会员分享,可在线阅读,更多相关《面向对象程序设计编程 实验 上机.doc(5页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、面向对象程序设计编程上机作业题目:(1) 调试构造函数Pen()例题using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2 class Pen public string Color; private int Price; public Pen() Color=BLACK; Price=5; public void SetPrice(int newPrice) Price=newPrice; public int GetPrice() return Price;
2、public void SetColor(string newColor) Color=newColor; public string GetColor() return Color; class Test public static void Main()Pen myPen=new Pen() ;Console.WriteLine(The price is 0,myPen.GetPrice();Console.WriteLine(The Color is 0,myPen.Color); (2) 调试构造函数重载Pen()例题using System;class Pen public stri
3、ng Color; private int Price; public Pen() Color = BLACK; Price = 5; public Pen(string newColor, int newPrice) Color = newColor; Price = newPrice; public void SetPrice(int newPrice) Price = newPrice; public void SetPrice() Price = 5; public int GetPrice() return Price; public void SetColor(string new
4、Color) Color = newColor; public string GetColor() return Color; class Test public static void Main() Pen myPen = new Pen(); Pen hisPen = new Pen(GREEN, 8); Console.WriteLine(The price is 0, myPen.GetPrice(); Console.WriteLine(The Color is 0, myPen.Color); Console.WriteLine(The price is 0, hisPen.Get
5、Price(); Console.WriteLine(The Color is 0, hisPen.GetColor(); hisPen.SetPrice(); Console.WriteLine(The price is 0, hisPen.GetPrice(); (3)使用重载方法求5和5.65的平方。using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication8 class Program public class app public int app1(int x
6、) return x * x; public double app1(double y) return y * y; static void Main(string args) app x = new app(); Console.WriteLine(5的平方是0, x.app1(5); Console.WriteLine(5.65的平方是0, x.app1(5.65); (4)将运算符+和- -进行重载,使之可对字符型数据进行运算,即:对一个字符型变量a,当a=L时a+(或+a)的值为M, a- -(或- -a)的值为Kusing System;using System.Collection
7、s.Generic;using System.Text;namespace ConsoleApplication6 public class charTest private char ch; public charTest() this.ch = ; public charTest(char val) this.ch = val; public char c get return this.ch; set this.ch = value; static public charTest operator +(charTest orig) charTest Result = new charTe
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象程序设计编程 实验 上机 面向 对象 程序设计 编程
限制150内