栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C#基础知识+代码(四)

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#基础知识+代码(四)

1.显式接口的应用
2.案例:停车场收费管理平台
3.结构(struct)与类的比较
4.运算符重载
5.索引器

显示接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    interface IChinese
    {
        void Speak( );
    }
    interface IUSA
    {
        void Speak( );
    }
}

继承接口类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Person: IChinese,IUSA
    {
        public void Speak( )
        {
            Console.WriteLine( "#@$%*.....");
        }
        void IChinese.Speak( )//显示接口不能加访问修饰符
        {
            Console.WriteLine( "你好,世界...." );
        }
        void IUSA.Speak( )
        {
            Console.WriteLine( "Hello,world...." );
        }
    }
}

主程序调用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main( string[ ] args )
        {           
            Person p = new Person( );
	          p.Speak( );
            IChinese ic = new Person( );//调用显示接口
            ic.Speak( );
            IUSA iu = new Person( );
            iu.Speak( );           
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public enum Brand
    { 
        奔驰,
        玛莎拉蒂,
        保时捷,
        金牛,
        金杯
    }
    public abstract class Vehicle
    {
        public string No { get; set; }
        public Brand VBrand { get; set; }
        public int Price { get; set; }//收费
        public DateTime InTime { get; set; }//进场时间

        public Vehicle( string no , Brand brand , int price , DateTime intime )//有参构造函数
        {
            this.No = no;
            this.VBrand = brand;
            this.Price = price;
            this.InTime = intime;
        }

        public Vehicle( )
        {

        }
    }

    public class Car : Vehicle
    {
        public double Oil { get; set; }//油耗量

        public Car( ):base()//调用父类无参构造函数
        {
        }

        public Car( string no , Brand brand , int price , DateTime intime , double oil )
            :base(no,brand,price,intime)//调用父类有参构造函数
        {
            this.Oil = oil;
        }

        //运算符重载(必须为公有静态方法),实现两个轿车对象的价格求和
        public static int operator +( Car c1 , Car c2 )
        {
            return c1.Price + c2.Price;  
        }

        //运算符重载,实现两个轿车对象的价格求差
        public static int operator -( Car c1 , Car c2)
        {
            return c1.Price - c2.Price;
        }
    }

    public class Bus : Vehicle
    {
        public int Count { get; set; }//载客量
        public Bus( )
        {
        }
        public Bus( string no , Brand brand , int price , DateTime intime , int count )
            : base( no , brand , price , intime )
        {
            this.Count = count;
        }
    }


    public class Park  //停车场类
    {
        private Vehicle[ ] vehicles = null;//车辆数组

        public Park( int length )
        {
            vehicles = new Vehicle[ length ];
        }
        //车辆进场
        public string InPark( Vehicle v )
        {
            string info = "很遗憾,车位已满,欢迎下次光临";
            
            for ( int i = 0; i < vehicles.Length; i++ )
            {
                if ( this.vehicles[i] == null )//有空位
                {
                    v.InTime = DateTime.Now; //进场时间
                    this.vehicles[ i ] = v;//车辆保存在数组中
                    info = string.Format( "你的车牌号({0})车辆已经在第{1}个车位" , v.No , ( i + 1 ) );
                    break;
                }
            }
            return info;
        }

        public Vehicle GetVehicle( string no )//根据车牌号查询车辆
        {
            Vehicle v = null;
            foreach ( var vehicle in vehicles )
            {
                if ( vehicle != null ) //确保数组元素不为空
                {
                     if ( vehicle.No.Equals(no))
                    {
                        v = vehicle;
                        break;
                    }
                }
            }

            return v;
        }
    
        //统计停车场的各种车辆数据
        public string GetInfo( )
        {
            string info = "";
            int count_car = 0;//轿车
            int count_bus = 0;//大巴
            int count_null = 0;//空位统计
            
            // object is class
            foreach ( Vehicle vv in this.vehicles )
            {
                if ( vv != null )
                {
                    if ( vv is Car )//is用法,判断对象vv是否属于Car类,是返回True,否则False
                    {
                        count_car++;
                    }
                    if ( vv is Bus )
                    {
                        count_bus++;
                    }
                }
                else
                {
                    count_null++;
                }
            }

            info = string.Format( "当前一共有{0}辆轿车n有{1}辆公交车n还有{2}个空位" , count_car , count_bus , count_null );

            return info;
        }

        //出场
        public string OutPark( Vehicle v )
        {
            string info = "";
            long money = ( DateTime.Now.Ticks - v.InTime.Ticks ) * v.Price;
            info = "您的车辆需要缴纳" + money + "元";
            for ( int i = 0; i < this.vehicles.Length; i++ )
            {
                if ( this.vehicles[ i ] != null )
                {
                    if ( vehicles[ i ].No.Equals( v.No ) )
                    {
                        this.vehicles[ i ] = null;//车位置空
                    }
                }
            }
            return info;
        }
    }
}

主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main( string[ ] args )
        {          
            // 停车场系统
            Car c1 = new Car( "鄂A 888888" , Brand.玛莎拉蒂 , 10 , DateTime.Now , 2.5 );
            Bus b1 = new Bus( "鄂A 666666" , Brand.金杯 , 20 , DateTime.Now,30 );
            Bus b2 = new Bus( "鄂A 686868" , Brand.金牛 , 20 , DateTime.Now , 30 );
            Park park = new Park( 5 );
            
            Console.WriteLine( park.InPark( c1 ) );
            Console.WriteLine( park.InPark( b1 ) );
            Console.WriteLine( park.InPark( b2 ) );
            for ( int i = 0; i < 100000; i++ )//产生一些时间间隔然后执行操作
            {

            }
            Vehicle v = park.GetVehicle( "鄂A 888888" );
            if ( v !=null )
            {
                Console.WriteLine( v.VBrand );
            }

            string info = park.GetInfo( );

            info = park.OutPark( b2 );//出场
            Console.WriteLine( info );            
        }
    }
}

类:引用类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public  struct PC
    {       
        private int aa;//结构字段不能定义时直接初始化

        public PC(int bb)//结构不能定义无参的构造函数
        {
            this.aa = bb;
        }
    }
    
}

运算符重载详细见以上主程序代码
索引器用的不多

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923167.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号