【C#】上级实验四
1、虚方法练习
设计一个控制台应用程序,定义一个Shape类,具体要求如下: ()类中定义2个私有字段长度(length)、宽度(breadth)。 ()类中定义相应公有属性分别对应上述2个字段; ()类中定义可重载的构造函数初始化上述2个字段; ()定义1个默认构造函数; ()类中定义公有方法输出对象的长度、宽度等详细信息; ()类中定义虚方法Draw,输出当前图型类别。 ()在main方法中测试Shape类及方法。 定义一个Box类,父类为Shape,具体要求如下: ()类中定义3个私有字段长度(length)、宽度(breadth)、高度(height); ()类中定义相应公有属性分别对应上述3个字段; ()类中定义可重载的构造函数初始化上述3个字段; ()定义1个默认构造函数; ()类中定义公有方法输出对象的长度、宽度、高度等详细信息; ()类中定义虚方法Draw,输出当前图型类别。 ()在main方法中测试Box类及方法; ()在main方法中测试多态。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyProject
{
class Program
{
static void Main(string[] args)
{
#region 测试Shape类及方法
/* 1、默认构造函数 对Length,breadth进行赋值
* 2、利用属性进行输出后,再利用属性进行赋值
* 3、最后利用ToString 输出对应的对象信息
*/
#endregion Shape s1 = new Shape(, );
Console.WriteLine("s1->Length:{0} , breadth:{1}",s1.Length,s1.Breadth);
s1.Length = ;
s1.Breadth = ;
Console.WriteLine(s1); #region 测试Box类及方法
/* 1、默认构造函数 对Length,breadth,height进行赋值
* 2、利用属性进行输出后,再利用属性进行赋值
* 3、最后利用ToString 输出对应的对象信息
*/
#endregion Box b1 = new Box(, , );
Console.WriteLine("b1->Length :{0}\tbreadth :{1}\theight :{2}",b1.Length,b1.Breadth,b1.Height);
b1.Length = ;
b1.Breadth = ;
b1.Height = ;
Console.WriteLine(b1); #region 测试多态
/*
* 多态体现在,父指针,将子类实例化.
*/
#endregion
Shape b2 = new Box(, , );
Console.WriteLine(b2); #region 测试虚函数
#endregion
s1.Draw();
b1.Draw();
}
} public class Shape
{
private int length;
private int breadth; public int Length
{
get { return length; }
set { length = value; }
}
public int Breadth
{
get { return breadth; }
set { breadth = value; }
} public Shape ( int length , int width)
{
this.Length = length;
this.Breadth = Breadth;
}
public Shape():this(, ) { }
public override string ToString()
{
return string.Format("Shape -> length:{0}\t Breadth:{1}", Length, Breadth);
}
public virtual void Draw()
{
Console.WriteLine(" Draw -> Shape " + "Length :{0} , Breadth :{1} ",Length , Breadth );
}
#region 主要注释
/*
* 类中的字段属性封装
* 构造函数 : 两参数,零参数
* 重载ToString类
*/
#endregion
} public class Box : Shape
{
private int height; public int Height
{
get { return height; }
set { height = value; }
} public Box(int length, int Breadth, int height):base(length, Breadth)
{
this.height = height;
}
public Box() : this(, , ) { }
public override string ToString()
{
return string.Format("Box -> length:{0}\t breadth:{1}\t height:{2}", Length, Breadth, Height);
}
public override void Draw()
{
Console.WriteLine(" Draw -> Shape " + "Length :{0} , Breadth :{1} , Height :{2}", Length, Breadth,Height);
}
#region Box类
/*
* Box继承了Shape的所有结构
*
* 1、无法直接访问父类的字段,但可通过"属性"间接访问
* 2、在重载 构造函数时,可以直接调用父类的构造函数实现部分字段的赋值
*
*/
#endregion
}
}
虚方法练习
2、IComparable接口练习
()定义Car类,包含两个字段:name和price; ()Car类中包含相应的属性、构造函数及ToString方法; ()Car类继承接口IComparable,并实现CompareTo方法; ()在Main方法中,使用Array.Sort方法对Car数组拍序,
拍序依据姓名和价格升序排序,
拍序后输出当前数组所有成员。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Myproject
{
class Program
{
static void Main(string[] args)
{
Car[] array = new Car[];
array[] = new Car("A", );
array[] = new Car("S", );
array[] = new Car("B", );
array[] = new Car("b", );
array[] = new Car("Z", ); Console.WriteLine("Length {0}", array.Length); Console.WriteLine("待排序序列");
foreach (var item in array)
{
Console.WriteLine(item);
} Array.Sort(array, , array.Length); Console.WriteLine("已排序序列");
foreach (var item in array)
{
Console.WriteLine(item);
} }
}
public class Car : IComparable
{
string name;
int price; public string Name
{
get { return name; }
set { name = value; }
} public int Price
{
get { return price; }
set { price = value; }
} public Car(string name, int price)
{
this.name = name;
this.price = price;
} public Car() : this("", ) { }
public override string ToString()
{
return string.Format("Car Name : {0} , Price : {1} ", Name, Price);
} public int CompareTo(Object obj)
{
//1、判断
if( obj == null)
{
return ;
}
//2、转换
Car rhs = obj as Car;
//3、判断
int r = ;
if( rhs == null )
{
throw new ArgumentException("Object is not a Car");
}
else
{
r = this.price.CompareTo(rhs.price);
if ( r == )
{
return this.name.CompareTo(rhs.name);
}
else
{
return r;
}
}
}
}
}
Icomparable
3、接口练习,设计一个控制台应用程序,模拟银行存取款业务。具体要求如下:
()定义一个接口IBankAccount,包含4个成员, 分别是
存款方法PayIn( )、
取款方法Withdraw( )、
转账方法TransferTo( )和
余额属性Banlance。 ()定义一个类CurrentAccount,继承接口IBankAccount,并实现该接口所有成员。 ()Main方法中调试并实现存款、取款、转账等操作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Myproject
{
class Program
{
static void Main(string[] args)
{
CurrentAccount c1 = new CurrentAccount("Tom", );
Console.WriteLine( c1 );
c1.Payln();
Console.WriteLine( c1 ); CurrentAccount c2 = new CurrentAccount("Jerry", );
c1.TransferTo(c2, ); Console.WriteLine( c1 );
Console.WriteLine( c2 );
}
} public interface IBankAccount
{
decimal Balance { get; }
decimal Payln(decimal x);
bool Withdraw(decimal x);
bool TransferTo(IBankAccount target ,decimal x);
} public class CurrentAccount : IBankAccount
{
private string id;
private decimal balance; public CurrentAccount (string id , decimal x)
{
this.id = id;
this.balance = x;
} public CurrentAccount() : this("NA", ) { }
public decimal Balance
{
get { return balance; }
} public decimal Payln(decimal x)
{
balance += x ;
return balance;
} public bool Withdraw(decimal x)
{
if (balance >= x)
{
balance -= x;
return true;
}
else return false;
} public bool TransferTo(IBankAccount target , decimal x)
{
if( Withdraw(x))
{
target.Payln(x);
return true;
}
else return false;
} public override string ToString()
{
return string.Format("{0} : {1}", id, balance);
} }
}
CurrentAccount类
【C#】上级实验四的更多相关文章
- Oracle 实验四-七
shutdown immediateORA-01097: 无法在事务处理过程中关闭 - 请首先提交或回退 解决:先 "commit" 实验四 SQL Production :: C ...
- php实验四
实验四 1.创建一个Person类,Person中包含三个属性name,age,wealth,分别设置为public,private,protected,再定义Person类的子类Student. 2 ...
- 实验四 简单的PV操作
实验四 简单的PV操作 专业 网络工程 姓名 方俊晖 学号 201406114309 一. 实验目的 1.掌握临界区的概念及临界区的设计原则: 2.掌握信号量的概念.PV操作的含义以 ...
- Java实验四
20145113 Java实验四 快捷键 之前没怎么记ISDEA的快捷键,但是熟练使用快捷键可以带来很多的便利,于是先开始学习一些常用的快捷键,就采用它默认的快捷键,这样后期就不会出现冲突,一些and ...
- 20145316&20145229实验四:驱动程序设计
20145316&20145229实验四:驱动程序设计 结对伙伴:20145316 许心远 博客链接:http://www.cnblogs.com/xxy745214935/p/6130871 ...
- 20145301&20145321&20145335实验四
20145301&20145321&20145335实验四 这次实验我的组员为:20145301赵嘉鑫.20145321曾子誉.20145335郝昊 实验内容详见:实验四
- 20145212 实验四《Andoid开发基础》
20145212 实验四<Andoid开发基础> 实验内容 安装Android Studio 运行安卓AVD模拟器 使用Android运行出模拟手机并显示自己的学号 实验过程 一.安装An ...
- Java实验四和实验五
实验四 类的继承性和多态性 [开发语言及实现平台或实验环境] Windows2000 或XP,JDK1.6与Jcreator4.0 [实验目的] 1. 掌握OOP方式进行程序设计的方法, 2. 了 ...
- 20145213 《Java程序设计》实验四 Android开发基础
20145213 <Java程序设计>实验四 Android开发基础 说在前面的话 不同以往实验,对于这次实验具体内容我是比较茫然的.因为点我,打开实验四的链接居然能飘出一股熟悉的味道,这 ...
随机推荐
- hibernate的API
程序源码: import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transa ...
- P4279 【[SHOI2008]小约翰的游戏】
我怎么什么都不会啊\(QAQ\)博弈论怎么和期望一样玄学啊\(QAQ\) 我们分几种情况讨论: \(Case1\):只有一堆且为1,那么后手胜利 \(Case2\):每一堆都是1,那么只需要判断奇偶性 ...
- mysql 升序降序
默认不指定,order by 按照升序排列. asc:升序 desc:降序
- Koa 脚手架创建项目
Koa 脚手架创建项目 通过应用 koa 脚手架生成工具 可以快速创建一个基于 koa2 的应用的骨架 全局安装koa npm install koa-generator -g //必须安装到全局 创 ...
- 软件工程 “校园汇” 个人IDEA竞选分析与总结
IDEA竞选 19/10/8软件工程课上举行了一次IDEA竞选: 我的竞选IDEA是"校友汇",大学生的在线活动中心. 投票结果: 可以看到,校友会(汇)IDEA竞选结果十分惨淡, ...
- Spring Boot源码中模块详解
Spring Boot源码中模块详解 一.源码 spring boot2.1版本源码地址:https://github.com/spring-projects/spring-boot/tree/2.1 ...
- Maven构建报错问题解决
[ERROR] Failed to execute goal on project zepeto-admin: Could not resolve dependencies -SNAPSHOT: Fa ...
- chrome浏览器postman 插件安装
postman 软件功能 模拟各种HTTPrequests 从常用的GET.POST到RESTful的PUT.DELETE…等等.甚至还可以发送文件.送出额外的header. Collection功能 ...
- Laya页面嵌套和Scene.destory导致的Bug
Laya2.1.1.1 参考: 预设使用 Laya给出了相同模块,逻辑代码也相同情况下,使用页面嵌+runtime的使用方案.但是该方案和Laya.Scene.open,Laya.Scene.dest ...
- Laya自定义组件
laya2.1.1.1 参考: 预设使用 一 没有自定义组件 教程翻了几遍,没有自定义组件,论坛搜了下,说是不能使用. 二 预置件做自定义组件 预置件无法右键创建. 又去翻教程.终于知道预置件怎么创建 ...