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
}
}

虚方法练习


2IComparable接口练习

()定义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#】上级实验四的更多相关文章

  1. Oracle 实验四-七

    shutdown immediateORA-01097: 无法在事务处理过程中关闭 - 请首先提交或回退 解决:先 "commit" 实验四 SQL Production :: C ...

  2. php实验四

    实验四 1.创建一个Person类,Person中包含三个属性name,age,wealth,分别设置为public,private,protected,再定义Person类的子类Student. 2 ...

  3. 实验四 简单的PV操作

    实验四 简单的PV操作 专业 网络工程   姓名 方俊晖 学号 201406114309 一.        实验目的 1.掌握临界区的概念及临界区的设计原则: 2.掌握信号量的概念.PV操作的含义以 ...

  4. Java实验四

    20145113 Java实验四 快捷键 之前没怎么记ISDEA的快捷键,但是熟练使用快捷键可以带来很多的便利,于是先开始学习一些常用的快捷键,就采用它默认的快捷键,这样后期就不会出现冲突,一些and ...

  5. 20145316&20145229实验四:驱动程序设计

    20145316&20145229实验四:驱动程序设计 结对伙伴:20145316 许心远 博客链接:http://www.cnblogs.com/xxy745214935/p/6130871 ...

  6. 20145301&20145321&20145335实验四

    20145301&20145321&20145335实验四 这次实验我的组员为:20145301赵嘉鑫.20145321曾子誉.20145335郝昊 实验内容详见:实验四

  7. 20145212 实验四《Andoid开发基础》

    20145212 实验四<Andoid开发基础> 实验内容 安装Android Studio 运行安卓AVD模拟器 使用Android运行出模拟手机并显示自己的学号 实验过程 一.安装An ...

  8. Java实验四和实验五

    实验四 类的继承性和多态性 [开发语言及实现平台或实验环境] Windows2000 或XP,JDK1.6与Jcreator4.0 [实验目的] 1.  掌握OOP方式进行程序设计的方法, 2.  了 ...

  9. 20145213 《Java程序设计》实验四 Android开发基础

    20145213 <Java程序设计>实验四 Android开发基础 说在前面的话 不同以往实验,对于这次实验具体内容我是比较茫然的.因为点我,打开实验四的链接居然能飘出一股熟悉的味道,这 ...

随机推荐

  1. telegraf 学习一 基本安装

    telegraf 是influxdata 开发的一个插件驱动的服务器代理,可以方便的用来收集以及报告系统的metrics 我使用mac 系统,测试安装使用了brew 安装 下载地址 说明官方也提供了m ...

  2. 使用vault pki engine 方便的管理证书

    vault 是一个很方便的secret .敏感数据管理工具,当前的版本已经包含了UI,使用起来很方便 以下演示一个简单的pki 管理 项目使用docker-compose 运行,为了简单使用单机开发模 ...

  3. JS全局变量是如何工作的?

    JS全局变量是如何工作的? <script> const one = 1; var two = 2; </script> <script> // All scrip ...

  4. Kafka 消费者到底是什么 以及消费者位移主题到底是什么(Python 客户端 1.01 broker)

    Kafka 中有这样一个概念消费者组,所有我们去订阅 topic 和 topic 交互的一些操作我们都是通过消费者组去交互的. 在 consumer 端设置了消费者的名字之后,该客户端可以对多个 to ...

  5. pycharm+gitee【代码上传下载】实战(windows详细版)

    pycharm+gitee环境搭建好以后应该如何进行代码上传下载操作呢?举几个例子,此文会一直更新 环境:2019社区版pycharm+gitee+git 系统:windows系统 一.代码上传功能 ...

  6. Mac版微信无法安装之始末

    前言 Mac版微信安装不了...纠结了一周时间 ̄□ ̄||... 今天终于可以登录了(虽然还是没有安装到电脑上,但可以使用了) 因为之前也查了很多,有人遇到,但是没有可以解决我这个问题的方法, 浪费了很 ...

  7. PHP 之根据两个经纬度计算距离

    一.函数代码 /** * @param $lng1 * @param $lat1 * @param $lng2 * @param $lat2 * @return float */ function g ...

  8. ssh修改默认远程端口

    ---------------------centos6-----------------1.查看系统版本cat /etc/redhot-releose 2.编辑sshd配置,修改默认的端口vim / ...

  9. Portainer实战

    Portainer是一个轻量级的Docker环境管理UI,可以管理docker host和docker swarm(我主要看中了能管理swarm这个,毕竟市面上能管理swarm的平台不多).之所以说是 ...

  10. POP IM 产品分析报告

    一.   体验环境 产品名称:POP IM 软件版本:v2.4.0 手机系统:一加5T Android 9 体验时间:2019.10.22-2019.10.31 二.   产品简介 1.   产品定位 ...