c#ppt练习

第六章

1.从控制台输入一个数,如果这个数大于等于60,就输出”及格”,否则输出”不及格”

从控制台输入一串字符,如果这个这串字符的长度大于3,并且字符首字母为A,,则输出“格式正确”,如果这串字符的长度小于等于3,则输出“字符长度必须大于3

 

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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
           //从控制台输入一个数,如果这个数大于等于60,就输出”及格”,否则输出”不及格”
            Console.WriteLine("请输入成绩:");
            int score = Convert.ToInt32(Console.ReadLine());
            if (score > 60)
            {
                Console.WriteLine("及格");
            }
            else
            {
                Console.WriteLine("不及格");
            }
           //从控制台输入一串字符
            Console.WriteLine("请输入一串字符:");
            string str = Console.ReadLine();
            //如果这个这串字符的长度大于3,并且字符首字母为A,,则输出“格式正确”,
            if (str.Length > 3 && str[0]=='A')
            {
                Console.WriteLine("格式正确!");
            }
            //如果这串字符的长度小于等于3,则输出“字符长度必须大于3”
        }
    }
}

 

 

2.实现以下功能:当用户输入0~18岁中任意一个选项时,系统输出对应的信息,如果用户输入0~18岁以外的选项,系统需提示请输入0~18岁。

 

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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
           //2.实现以下功能:当用户输入0岁~18岁中任意一个选项时,系统输出对应的信息,
            //如果用户输入0岁~18岁以外的选项,系统需提示请输入0岁~18岁。
            Console.WriteLine("请输入年龄");
            int age = Convert.ToInt32(Console.ReadLine());
            if (age > 0 && age < 18)
            {
                Console.WriteLine(age);
            }
            else
            {
                Console.WriteLine("请输入0岁~18岁");
            }

        }
    }
}

                                第七章

1.定义一个数组,用来保存书名,针对此数组完成以下题目

定义数组,不进行任何初始化

用动态初始化的方式定义数组,并添加3个书名到数组中

用静态初始化的方式定义数组,并添加3个书名到数组中

在控制台输出索引下标为12的书名

在控制台输出数组的元素个数

for循环在控制台遍历输出数组中每个元素的值

foreach循环在控制台遍历输出数组中每个元素的值

 

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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            //.定义一个数组,用来保存书名,针对此数组完成以下题目
            //定义数组,不进行任何初始化
            string[] bname;
            //用动态初始化的方式定义数组,并添加3个书名到数组中
            string[] bname1 = new string[] { "books1", "books2", "books3" };
            //用静态初始化的方式定义数组,并添加3个书名到数组中
            string[] bname2 = { "bname1", "bname2", "bname3" };
            //在控制台输出索引下标为1和2的书名
            Console.WriteLine("索引下标为1和2的书名:"+bname1[0]+" "+bname1[1]);
            //在控制台输出数组的元素个数
            Console.WriteLine("数组的元素个数"+bname1.Length);
            //用for循环在控制台遍历输出数组中每个元素的值
            for (int i = 0; i < bname1.Length; i++)
            {
                Console.WriteLine(bname1[i]+"  ");
            }
            //用foreach循环在控制台遍历输出数组中每个元素的值
            foreach (string  item in bname1)
            {
                Console.WriteLine(item);
            }

        }
    }
}

 

 

2.定义一个二维数组,用来保存不同专业的书,针对此数组完成以下题目

定义数组,不进行任何初始化

用动态初始化的方式定义数组,并将3个专业每个专业4本书共计12本书的书名添加到数组中

用静态初始化的方式定义数组,并将3个专业每个专业4本书共计12本书的书名添加到数组中

在控制台输出第1个专业的第2本书、第3个专业的第1本书

在控制台输出数组的元素个数

for循环在控制台遍历输出数组中所有书的书名

foreach循环在控制台遍历输出数组中所有书的书名

 

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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            //.定义一个二维数组,用来保存不同专业的书,针对此数组完成以下题目
            string[,] books= new string[3,3];
            //定义数组,不进行任何初始化
            string[,] books1;
            //用动态初始化的方式定义数组,并将3个专业每个专业4本书共计12本书的书名添加到数组中
            string[,] books2 = new string[3, 4] { { "book1", "book1", "book1", "book1" }, { "book1", "book1", "book1", "book1" }, { "book1", "book1", "book1", "book1" } };
                   
            //在控制台输出第1个专业的第2本书、第3个专业的第1本书
            Console.WriteLine("第1个专业的第2本书是:"+books2[0,1]);
            Console.WriteLine("第1个专业的第2本书是:" + books2[2, 0]);
            //在控制台输出数组的元素个
            Console.WriteLine("数组的元素个数:"+books2.Length);
            //用for循环在控制台遍历输出数组中所有书的书名
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Console.Write(books2[i, j] + "  ");
                }
            }        
            //用foreach循环在控制台遍历输出数组中所有书的书名
            foreach (string item in books2)
            {
                Console.WriteLine(item);
            }
        }
    }
}

 

 

 

3.分别用以下三种方式实现一个用来保存学生成绩的一维数组的定义、元素添加、元素遍历、元素排序

创建一般数组对象,用数组索引下标的方式实现数组元素的添加和遍历

创建Array类数组对象,用Array类的方法实现元素添加、遍历、排序

创建一般数组对象,数组元素的添加、遍历、排序等方法用与Array类对应的方法

 

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

namespace test
{
// 三种方式实现 保存学生成绩的一维数组的定义、元素添加、元素遍历、元素排序
    class Program
    {
        static void Main(string[] args)
        {
            //创建一般数组对象,用数组索引下标的方式实现数组元素的添加和遍历
            //一般的数组定义
            int[] arr = new int[3];
            //元素添加
            arr[0] = 90;
            arr[1] = 98;
            arr[2] = 97;
            //便利
            Console.WriteLine("**************简单一维数组便利***********");
            foreach (int  item in arr)
            {
                Console.Write( item+"   ");
            }
            //创建Array类数组对象,用Array类的方法实现元素添加、遍历、排序
            Array arr2= Array.CreateInstance(typeof(int),5);
            arr2.SetValue(1, 0);
            arr2.SetValue(2, 1);
            arr2.SetValue(3, 2);
            arr2.SetValue(4, 3);
            arr2.SetValue(5, 4);
            Console.WriteLine();
            Console.WriteLine("**************array数组便利***********");
            foreach (int item in arr2)
            {
                Console.Write(item+"  ");
            }
            //创建一般数组对象,数组元素的添加、遍历、排序等方法用与Array类对应的方法
            int[] arr3 = new int[3];
            arr3.SetValue(1, 0);
            arr3.SetValue(2, 1);
            arr3.SetValue(3, 2);
            Array.Sort(arr3);
            Array.Reverse(arr3);
            Console.WriteLine();
            Console.WriteLine("**************一般数组定义,arr添加***********");
            foreach (int item in arr3)
            {
                Console.Write(item + "  ");
            }
            Console.WriteLine();
        }
    }
}

 

 

 

                           第十一章(属性和索引器)

1.编写Student类包含如下属性,并实现给属性赋值且打印出来

年龄(0-100)不符合的设为18

姓名(只读属性)

爱好(读写)

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

namespace test
{
//1.编写Student类包含如下属性,并实现给属性赋值且打印出来    
    class student
    {
        int age;
        string name;
        string fav;
        //年龄(0-100)不符合的设为18
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if (value > 0 && value < 100)
                {
                    age = value;
                }
            }
        }
        //姓名(只读属性)
        public string Name
        {
            get
            {
                return name;
            }
        }
        //爱好(读写)
        public string Fav
        {
            get { return fav; }
            set { fav = value; }
        }
        public student(int age, string name, string fav)
        {
            this.age=age;
            this.name=name;
            this.fav = fav;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入学生年龄:");
            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入学生的姓名:");
            string name = Convert.ToString(Console.ReadLine());
            Console.WriteLine("请输入学生的爱好:");
            string fav = Convert.ToString(Console.ReadLine());
            student stu = new student(age, name, fav);
            Console.WriteLine("学生的年龄是:"+stu.Age);
            Console.WriteLine("学生的姓名是:"+stu.Name);
            Console.WriteLine("学生的爱好是:"+stu.Fav);

        }
    }
}

2.某购物网站有多个网店,一个网店有多个会员,每个会员都有其会员账号和会员余额。

请创建网店类和会员类,要求实现以下功能

在网店类中,通过构造函数初始化多个会员对象,并可以通过索引值或会员账号查询或修改会员余额

在会员类中,通过构造函数中初始化会员账号和会员余额;修改会员账号时,要求会员账号必须是3位;查询会员余额时,如果会员余额小于等于0,则返回“无可用金额!”。

创建完网店类0和会员类后,请访问网店类和会员类,初始化多个会员,并查询或修改会员余额

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

namespace test
{
//2.某购物网站有多个网店,一个网店有多个会员,每个会员都有其会员账号和会员余额。    
    class wd
    {
        hy[] hys;
        public hy[] Hys
        {
            get{return hys;}
            set{hys=value;}
        }
        public wd(int capacity)
        {
            hys = new hy[capacity];
        }
        //创建索引器
        //在网店类中,通过构造函数初始化多个会员对象,并可以通过索引值或会员账号查询或修改会员余额
        public hy this[string hid]
        {
            get
            {
                foreach (hy item in hys)
                {
                    if (hid == item.Hid)
                    {
                        return item;
                    }
                }
                return null;
            }
            set
            {
                for (int i = 0; i < hys.Length; i++)
                {
                    if (hys[i].Hid == hid)
                    {
                        hys[i] = value;
                    }
                }
            }
        }
        public hy this[int index]
        {
            get
            {
                if (index >= 0 && index < hys.Length)
                {
                    return hys[index];
                }
                else
                {
                    return null;
                }
            }
            set
            {
                if (index >= 0 && index < hys.Length)
                {
                     hys[index]=value;
                }
            }
        }
    }
    class wz
    {
         wd[] wds;
         public wd[] Wds
         {
             get { return wds; }
             set { wds = value; }
         }
        public wz(int capacity)
        {
            wds = new wd[capacity];
        }

    }
    //在会员类中,通过构造函数中初始化会员账号和会员余额;       
    class hy
    {
        string hid;
        int balance;
        //修改会员账号时,要求会员账号必须是3位;
        public string Hid
        {
            get { return hid; }
            set {
                if (hid.Length == 3)
                {
                    hid = value;
                }
                else
                {
                    Console.WriteLine("会员账号必须是三位!");
                }
               }
        }
        //查询会员余额时,如果会员余额小于等于0,则返回“无可用金额!”。
        public int Balance
        {
            get { return balance; }
            set 
            {
                if (value <= 0)
                {
                    Console.WriteLine("无可用金额!");
                }
                else
                {
                    balance = value; 
                }
               }
        }
        public hy(string hid, int balance)
        {
            this.hid = hid;
            this.balance = balance;
        }
    }
//创建完网店类0和会员类后,请访问网店类和会员类,初始化多个会员,并查询或修改会员余额
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个包括三个网店的网站
            wz wz = new wz(3);
            //创建三个包括三个网店的网站
            wd wd1 = new wd(3);
            wd wd2 = new wd(3);
            wd wd3 = new wd(3);  
            //将三个网店添加到网站
            wz.Wds[0] = wd1;
            wz.Wds[1] = wd2;
            wz.Wds[2] = wd3;
            //创建三个会员
            hy hy1 = new hy("001", 100);
            hy hy2 = new hy("001", 100);
            hy hy3 = new hy("001", 100);
            //将三个会员添加到网店
            wd1[0]= hy1;
            wd1[1] = hy2;
            wd1[2] = hy3;
            //通过hid修改balance
            wd1[0].Balance = 101;
            wd1["001"].Balance = 102;
            wd1["001"].Balance = 103;
            //输出网站第一个网店三个会员的信息
            foreach (hy item in wz.Wds[0].Hys)
            {
                Console.WriteLine(item.Hid);
                Console.WriteLine(item.Balance);
            }
        }
    }
}

3.定义猫类,实例化多个猫类的对象,累计白猫和黑猫各多少只

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

namespace test
{
    //3.定义猫类,实例化多个猫类的对象,累计白猫和黑猫各多少只
    class cat
    {
        string color;
        public static  int count1 = 0;
        public static int count2 = 0;
        
        public string Color
        {
            get { return color; }
            set { color = value; }
        }
        public cat(string color)
        {
            this.color = color; 
            if (color == "white")
            {
                count1++;
            }
            else if(color=="blank")
            {
                count2++;
            }
            
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //创建一直白猫
            Console.WriteLine("创建一只白猫");
            cat cat1 = new cat("white");
            Console.WriteLine("白猫的个数:"+cat.count1);
            Console.WriteLine("黑猫的个数:"+cat.count2);
            Console.WriteLine("创建一只黑猫");
            cat cat2 = new cat("blank");
            Console.WriteLine("白猫的个数:" + cat.count1);
            Console.WriteLine("黑猫的个数:" + cat.count2);

        }
    }
}

第十二章

1.创建电器类,声明电器的属性:功率、额定电压、额定电流、交直流类型。电器的方法:工作方法。创建电视机类和冰箱类继承电器类,电视机增加属性:种类、最大音量,重写工作方法。电冰箱了增加属性:容量,重写工作方法。

2.编写测试类测试上题中定义的3个类,使用电器来引用电视对象和冰箱对象,让电器工作。

3.重写电视机的ToString()方法,输出电视机的信息,并使用该方法。

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

namespace ConsoleApplication9
{
    //1.创建电器类,属性:功率、额定电压、额定电流、交直流类型。
    //电器的方法:工作方法。
    class dianqi
    {
        private int gonglv;

        public int Gonglv
        {
            get { return gonglv; }
            set { gonglv = value; }
        }
        private int dianya;

        public int Dianya
        {
            get { return dianya; }
            set { dianya = value; }
        }
        private int dianliu;

        public int Dianliu
        {
            get { return dianliu; }
            set { dianliu = value; }
        }
        private string type;

        public string Type
        {
            get { return type; }
            set { type = value; }
        }

        public virtual void work()
        {
            Console.WriteLine("电器工作");
        }
        public dianqi(int gonglv,int dianya,int dianliu,string type)
        {
            this.gonglv = gonglv;
            this.dianya = dianya;
            this.dianliu = dianliu;
            this.type = type;
        }
    }
    //创建电视机类和冰箱类继承电器类,
    //电视机增加属性:种类、最大音量,重写工作方法。
    class tv : dianqi
    {
        string types;

        public string Types
        {
            get { return types; }
            set { types = value; }
        }
        int maxyin;

        public int Maxyin
        {
            get { return maxyin; }
            set { maxyin = value; }
        }
        public override void work()
        {
            Console.WriteLine("电视工作方式"); ;
        }
        public tv(int gonglv, int dianya, int dianliu, string type, string types)
            : base(gonglv, dianya, dianliu, type)
        {
            this.types = types;
        }
        public override string ToString()
        {
            return this.types + "  " + this.Type + "  " + this.Gonglv + "  " + this.Dianya;
        }
    }
    //电冰箱了增加属性:容量,重写工作方法。
    class bingxiang:dianqi
    {
        int rongliang;

        public int Rongliang
        {
            get { return rongliang; }
            set { rongliang = value; }
        }
        public override void work()
        {
            Console.WriteLine("冰箱工作方式");
        }
        public bingxiang(int gonglv, int dianya, int dianliu, string type, string types, int rongliang)
            : base(gonglv, dianya, dianliu, type)
        {
            this.rongliang = rongliang;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            dianqi dq1 = new tv(1,2,3,"dianqi","tv");
            dianqi dq2 = new bingxiang(1,2,3,"dianqi","tv",12);
            dq1.work();
            dq2.work();
            Console.WriteLine(dq1.ToString()) ;

        }
    }
}

4.编写一个C#应用程序,设计一个汽车类 Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car Vehicle 的子类,其中包含的属性有载人数 loader。卡车类 Truck Car类的子类,其中包含的属性有载重量 payload。每个类都有构造方法和输出相关数据的方法 。编写测试类CarTest进行测试。

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

namespace ConsoleApplication9
{
    //设计一个汽车类 Vehicle,包含的属性有车轮个数 wheels和车重weight。
    class Vehicle
    {
        int wheels;

        public int Wheels
        {
            get { return wheels; }
            set { wheels = value; }
        }
        int weight;

        public int Weight
        {
            get { return weight; }
            set { weight = value; }
        }
        public Vehicle(int wheels, int weight)
        {
            this.wheels = wheels;
            this.weight = weight;
        }
        public virtual void display()
        {
            Console.WriteLine( wheels + "  "+weight);
        }

    }
    //小车类Car 是Vehicle 的子类,其中包含的属性有载人数 loader。
    class car:Vehicle
    {
        int loader;

        public int Loader
        {
            get { return loader; }
            set { loader = value; }
        }

        public car(int wheels, int weight, int loader)
            : base(wheels, weight)
        {
            this.loader = loader;
        }
        public virtual void display()
        {
            Console.WriteLine(Wheels + "  " + Weight+"  "+loader);
        }

    }
    //卡车类 Truck 是Car类的子类,其中包含的属性有载重量 payload。
    class Truck:car
    {
        int payload;

        public int Payload
        {
            get { return payload; }
            set { payload = value; }
        }

        public Truck(int wheels, int weight, int loader, int payload)
            : base(wheels, weight, loader)
        {
            this.payload = payload;
        }
        public virtual void display()
        {
            Console.WriteLine(Wheels + "  " + Weight + "  " + Loader+"  "+payload);
        }
    }
    //每个类都有构造方法和输出相关数据的方法 。
    //编写测试类CarTest进行测试。
    class Program
    {
        static void Main(string[] args)
        {
            car car = new car(1,2,3);
            car.display();
            Truck tr = new Truck(1,2,3,4);
            tr.display();

        }
    }
}

5.对平面形体有长和面积,对立体有表面积和体积,对几何图形基类,周长、面积和体积应怎样计算(用什么函数)?对平面图形体积怎样计算(用什么函数)?对立体图形周长怎么计算(用什么函数)?要求实现运行时的多态性。请编程,并测试。

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

namespace ConsoleApplication9
{
   //平面图形有长和面积
   //立体图形有表面积和体积,
   // 几何图形基类,周长、面积和体积应怎样计算(用什么函数)?
    class jihe
    {
        public virtual void zhou(){}
        public virtual void area(){}
        public virtual void tiji(){}
    }
    class ping:jihe
    {
        public override void zhou()
        {
            Console.WriteLine("平面图形的周长");
        }
        public override void area()
        {
            Console.WriteLine("平面图形的面积");
        }
        public override void tiji()
        {
            Console.WriteLine("平面图形没有体积");
        }

    }
    class liti:jihe
    {
        public override void zhou()
        {
            Console.WriteLine("立体图形没有周长");
        }
        public override void area()
        {
            Console.WriteLine("立体图形的面积");
        }
        public override void tiji()
        {
            Console.WriteLine("立体图形的体积");
        }


    }
   // 平面图形体积怎样计算(用什么函数)?
   // 立体图形周长怎么计算(用什么函数)?
   // 要求实现运行时的多态性。请编程,并测试。
    class Program
    {
        static void Main(string[] args)
        {
            jihe jh1 = new ping();
            jihe jh2 = new liti();
            jh1.zhou();
            jh1.area();
            jh1.tiji();
            jh2.zhou();
            jh2.area();
            jh2.tiji();

        }
    }
}

6.请编码实现如下需求:

乐器(Instrument)分为:钢琴(Piano)、小提琴(Violin)

各种乐器的弹奏( play )方法各不相同。

编写一个测试类InstrumentTest,要求:

编写方法testPlay,对各种乐器进行弹奏测试。要依据乐器的不同,进行相应的弹奏。

main方法中进行测试

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

namespace ConsoleApplication9
{
//乐器(Instrument)分为:钢琴(Piano)、小提琴(Violin)
//各种乐器的弹奏( play )方法各不相同。
//编写一个测试类InstrumentTest,要求:
//编写方法testPlay,对各种乐器进行弹奏测试。要依据乐器的不同,进行相应的弹奏。
//在main方法中进行测试
    class Instrument
    {
        public virtual void play()
        {
            Console.WriteLine("乐器演奏");
        }
        
    }
    class Piano : Instrument
    {
        public override void play()
        {
            Console.WriteLine("钢琴演奏");
        }
    }
    class Violin : Instrument
    {
        public override void play()
        {
            Console.WriteLine("小提琴演奏");
        }
    }
    class InstrumentTest
    {
        public void testPlay(Instrument ins)
        {
            ins.play();
        }
        static void Main(string[] args)
        {
            InstrumentTest it1 = new InstrumentTest();
            InstrumentTest it2= new InstrumentTest();
            Piano pi = new Piano();
            Violin vi = new Violin();
            it1.testPlay(pi);
            it2.testPlay(vi);


        }
    }
}

第十三章

1.一个运输公司从网上得到订单,订单上标有货物重量和运输里程,该公司可以使用3中运输工具:卡车、火车、飞机。编写运输接口,声明3个接口常量,表示运输工具,声明一个计算运费的方法,参数是重量和里程。

2.卡车、火车、飞机分别实现上题的运输接口,计算运费的方法如下:

1)卡车:运输=重量×距离×120,当距离大于1000(km)或重量大于60t)的时候拒载,返回-1.

2)火车:当距离在900km)内(包含)时,运费=重量×距离×250,大于900km)运费=重量×距离×300

3)飞机:当距离大于500km)时,运费=重量×距离×750,否则拒载,返回-1

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

namespace ConsoleApplication9
{
    //一个运输公司从网上得到订单,订单上标有货物重量和运输里程,
    //    该公司可以使用3中运输工具:卡车、火车、飞机。
    //    编写运输接口,声明3个接口常量,表示运输工具,
    //    声明一个计算运费的方法,参数是重量和里程。
     interface itr
    {
        int cu(int longs, int weight);

    }
    //(1)卡车:运输=重量×距离×120,当距离大于1000(km)或重量大于60(t)的时候拒载,返回-1.
    class car : itr
    {
        public  int cu(int longs, int weight)
        {
            if (longs > 1000 || weight > 60)
            {
                return -1;
            }
            else
            {
                return weight * longs * 120;
            }
        }
    }
    //(2)火车:当距离在900(km)内(包含)时,运费=重量×距离×250,大于900(km)运费=重量×距离×300
    class huo : itr
    {

        public  int cu(int longs, int weight)
        {
            if (longs <= 900)
            {
                return weight * longs * 250;
            }
            else
            {
                return weight * longs * 300;
            }
        }
    }
    //(3)飞机:当距离大于500(km)时,运费=重量×距离×750,否则拒载,返回-1
    class fei : itr
    {
        public  int cu(int longs, int weight)
        {
            if (longs > 500)
            {
                return weight * longs * 750;
            }
            else
            {
                return -1;
            }
        }
    }
    //.请用户在控制台输入货物重量及运输里程,并选择一种运输工具,然后系统返回运费。
    class company 
    {

        static void Main(string[] args)
        {
            int weight;
            int longs;
            Console.WriteLine("请输入货物重量和里程:");
            weight = Convert.ToInt32(Console.ReadLine());
            longs = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请选择运输方式:");
            Console.WriteLine("1:卡车");
            Console.WriteLine("2:火车");
            Console.WriteLine("3:飞机");
            int single = Convert.ToInt32(Console.ReadLine());
            switch (single)
            {
                case 1:
                    {
                        car car = new car();
                        Console.WriteLine("小汽车的运输费用为:" + car.cu(longs, weight));
                    }
                    break;
                case 2:
                    {
                        huo huo = new huo();
                        Console.WriteLine("火车的运输费用为:" + huo.cu(longs, weight));
                    }
                    break;
                default:
                    {
                        fei fei = new fei();
                        Console.WriteLine("飞机的运输费用为:" + fei.cu(longs, weight));
                    }
                    break;
            }           
        }
    }
}

3.请用户在控制台输入货物重量及运输里程,并选择一种运输工具,然后系统返回运费。

1: 做一个动物(animal)行为的接口,包括eatgo,做一个控制台程序,定义一个动物的类通过继承动物行为来触发这两个方法。

namespace ConsoleApplication9
{
//1: 做一个动物(animal)行为的接口,包括eat和go,
 
    interface xingwei
    {
        void eat();
        void go();
    }
    //定义一个动物的类通过继承动物行为来触发这两个方法。
    class animal:xingwei
    {
        public void eat()
        {
            Console.WriteLine("动物 吃");
        }
        public void go()
        {
            Console.WriteLine("动物 走");
        }
    }
    class company
    {

        static void Main(string[] args)
        {
            animal an = new animal();
            an.eat();
            an.go();
        }
    }
}

2: 做一个控制台程序,定义一个类a,里面包含方法b,再定义一个类c继承于类a,实践隐藏基类的方式。

namespace ConsoleApplication9
{
    class a
    {
        public void b()
        {
            Console.WriteLine("基类的b()");
        }
    }
    class c : a
    {
        //3:修改练习2的例子,在继承的c子类中调用a基类的方法b。
        public void b()
        {        
            Console.WriteLine("子类的b()");
        }
    }

    class company
    {
        static void Main(string[] args)
        {
            c c = new c();
            c.b();
        }
    }
}

3:修改练习2的例子,在继承的c子类中调用a基类的方法b

namespace ConsoleApplication9
{
    class a
    {
        public void b()
        {
            Console.WriteLine("基类的b()");
        }
    }
    class c : a
    {
        //3:修改练习2的例子,在继承的c子类中调用a基类的方法b。
        public void b()
        {
            base.b();         
        }
    }
    class company
    {
        static void Main(string[] args)
        {
            c c = new c();
            c.b();
        }
    }
}

4:修改练习3的例子,将a定义为一个抽象类,修改方法b,再用c来继承输出。

namespace ConsoleApplication9
{
    abstract class a
    {
        public virtual void b()
        {
            Console.WriteLine("基类的b()");
        }
    }
    class c : a
    {
        public override void b()
        {
            Console.WriteLine("子类b()") ;
        }
    }
    class company
    {
        static void Main(string[] args)
        {            
            c c = new c();
            c.b();
        }
    }
}

5:在练习1的基础上增加一个animal的体重weighteat时,就增加10go时,就减少10

namespace ConsoleApplication9
{
    interface xingwei
    {
        void eat();
        void go();
    }
    //定义一个动物的类通过继承动物行为来触发这两个方法。
    class animal : xingwei
    {
        int weight;

        public int Weight
        {
            get { return weight; }
            set { weight = value; }
        }

        public void eat()
        {
            weight = weight - 10;
            Console.WriteLine("动物 吃");
        }
        public void go()
        {
            weight = weight + 10;
            Console.WriteLine("动物 走");
        }
        public animal(int weight)
            : base()
        {
            this.weight = weight;
        }
    }
    class company
    {
        static void Main(string[] args)
        {
            animal an = new animal(80);
            Console.WriteLine(an.Weight);
            an.go();
            Console.WriteLine(an.Weight);
            an.eat();
            Console.WriteLine(an.Weight);
        }
    }
}

6:在练习5的基础上再增加一个sleep功能,当使用此功能时,在控制台中输出“now I am sleeping”(要求使用接口继承功能)。

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

namespace ConsoleApplication9
{
    interface xingwei
    {
        void eat();
        void go();
        void sleep();
    }
    //定义一个动物的类通过继承动物行为来触发这两个方法。
    class animal : xingwei
    {
        int weight;

        public int Weight
        {
            get { return weight; }
            set { weight = value; }
        }

        public void eat()
        {
            weight = weight - 10;
            Console.WriteLine("动物 吃");
        }
        //6:在练习5的基础上再增加一个sleep功能,
        //当使用此功能时,在控制台中输出“now I am sleeping”(要求使用接口继承功能)。
        public void sleep()
        {
            Console.WriteLine("now I am sleeping");
        }
        public void go()
        {
            weight = weight + 10;
            Console.WriteLine("动物 走");
        }
        public animal(int weight)
            : base()
        {
            this.weight = weight;
        }
    }
    abstract class a
    {
        public virtual void b()
        {
            Console.WriteLine("基类的b()");
        }
    }
    class c : a
    {
        public override void b()
        {
            Console.WriteLine("子类b()") ;
        }
    }
    
    class company
    {
        static void Main(string[] args)
        {
            animal an = new animal(80);
            an.sleep();
        }
    }
}

第十四章

1.编写一个类ExceptionTest1,在main方法中使用trycatchfinally

try块中,编写数组访问越界的代码

catch块中,捕获数组访问越界所产生的异常,并且打印异常信息

finally块中,打印一条语句

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

namespace ConsoleApplication9
{
//1.编写一个类ExceptionTest1,
    class ExceptionTest1:Exception 
    {
        //在main方法中使用try、catch、finally:
        //在try块中,编写数组访问越界的代码
        //在catch块中,捕获数组访问越界所产生的异常,并且打印异常信息
        //在finally块中,打印一条语句
        static void Main(string[] args)
        {
            int[] a = { 1, 2, 3 };
            try
            {
                Console.WriteLine(a[3]);
            }
            catch (IndexOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                Console.WriteLine("finally");
            }
            
        }
    }
}

2.问题描述:

David正在为一个项目工作,其中他正在计算一个整型数组中的总和。David需要处理当他使用数组时发生的异常。如果David在执行程序的时候遇到了任何异常情况,程序需要显示一个异常消息。

编写能产生System.OverflowException异常的代码,并将其捕获,在控制台上输出异常信息。

 

原文地址:https://www.cnblogs.com/excellencesy/p/8310744.html