类继承多态,抽象类接口——练习题


       
    

1.编写Student类包含如下属性,并实现给属性赋值且打印出来 年龄(0-100)不符合的设为18 姓名(只读属性) 爱好(读写)

public class Student
{
int age;

public int Age
{
get
{ return age;

}
set
{
if (value>0&&value<100)
{
age = value;
}
else
{
age = 18;
}
}
}
string name="xaioming";

public string Name
{
get { return name; }

}
string hobby;

public string Hobby
{
get { return hobby; }
set { hobby = value; }
}
}

Student stu = new Student();
stu.Age = 1000;
stu.Hobby = "basketball";
Console.WriteLine(stu.Name);
Console.WriteLine(stu.Age);
Console.WriteLine(stu.Hobby);
Console.ReadKey();

 2.

某购物网站有多个网店,一个网店有多个会员,每个会员都有其会员账号和会员余额。 请创建网店类和会员类,要求实现以下功能 在网店类中,通过构造函数初始化多个会员对象,并可以通过索引值或会员账号查询或修改会员余额 在会员类中,通过构造函数中初始化会员账号和会员余额;修改会员账号时,要求会员账号必须是3位;查询会员余额时,如果会员余额小于等于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);
            }
            Console.ReadKey();
        }
    }
}

3.比较静态成员和非静态成员的区别,

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

class Cat
    {
        private static int count1 = 0;
        public int count2 = 0;
        public Cat() 
        {
            count1 = count1 + 1;
            count2 = count2 + 1;
        }
        public static void Display1() 
        {
            Console.WriteLine("白猫的数量是:"+count1);
        }
        public void Display2() 
        {
            Console.WriteLine("黑猫的数量是:"+count2);
        }
       
    }
    


            Cat.Display1();
            Cat cat1 = new Cat();
            cat1.Display2();
            Cat cat2 = new Cat();
            cat2.Display2();
            Cat.Display1();
            Console.ReadKey();
            

注:静态方法:类名+方法名(此时不调用构造函数) 只有定义对象初始化构造函数的时候才发生加1操作

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

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

namespace ConsoleApplication4
{
    //    创建电器类,声明电器的属性:功率、额定电压、额定电流、交直流类型。
    //    电器的方法:工作方法。
    public class Electric 
    {
        int power;

        public int Power
        {
            get { return power; }
            set { power = value; }
        }
        int voltage;
        public int Voltage 
        {
            get { return voltage; }
            set { voltage = value; }
        }
        int current;

        public int Current
        {
            get { return current; }
            set { current = value; }
        }
        int type;

        public int Type
        {
            get { return type; }
            set { type = value; }
        }
        public virtual void work() 
        {
            Console.WriteLine("电器的工作方法");
        }
        public Electric(int po, int vo, int cu, int ty) 
        {
            this.power = po;
            this.voltage = vo;
            this.current = cu;
            this.type = ty;
        }
    }
    // 电视机增加属性:种类、最大音量,重写工作方法。创建电视机类继承电器类
    class Television : Electric 
    {
        string tetype;

        public string Tetype
        {
            get { return tetype; }
            set { tetype = value; }
        }
        int voice;

        public int Voice
        {
            get { return voice; }
            set { voice = value; }
        }
        public override void work()
        {
            base.work();
            Console.WriteLine("电视机工作");
        }
        public Television(int po, int vo, int cu, int ty, string te, int voic) : base(po, vo, cu, ty) 
        {
            this.tetype = te;
            this.voice = voic;
        }
        //重写电视机的ToString()方法,输出电视机的信息,并使用该方法。
        public override string ToString()
        {
            return this.Power + "_" + this.voice + "_" + this.Type + "_" + this.tetype + "_"+this.voice;
        }

    }
    // 电冰箱了增加属性:容量,重写工作方法。创建冰箱类继承电器类,
    class Refiger : Electric 
    {
        int capacity;

        public int Capacity
        {
            get { return capacity; }
            set { capacity = value; }
        }
        public override void work()
        {
            base.work();
            Console.WriteLine("电冰箱工作");
        }
        public Refiger(int po, int vo, int cu, int ty,int ca):base(po,vo,cu,ty)
        {
            this.capacity = ca;
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Television te = new Television(1,2,3,4,"sanxing",10);
            te.work();
            
            Console.WriteLine(te.ToString());
            Console.WriteLine(te.Voice);
            Refiger re = new Refiger(1, 2, 3, 4, 5);
            re.work();
            Console.ReadKey();
        }
    }
}
 

4.测试程序结果:

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

namespace ConsoleApplication4
{
    class A
    {
        public string s = "A";
    }
    class B : A
    {
        new public string s = "B";
    }

    
    class Program
    {
        static void Main(string[] args)
        {
            A a1 = new B();
            B b1 = (B)a1;
            System.Console.WriteLine(b1.s);
            B b2 = new B();
            A a2 = b2;
            A a3 = b2;
            System.Console.WriteLine(a2.s);
            System.Console.WriteLine(((B)a3).s);
            Console.ReadKey();

            
        }
    }
}

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

namespace ConsoleApplication4
{
    class A
    {
        public string s = "A";
        public  void work() 
        {
            Console.WriteLine("sss");
        }
    }
    class B : A
    {
        new public string s = "B";
         new public  void work() 
        {
            Console.WriteLine("WWWWW");
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            A a1 = new B();
            a1.work();
            System.Console.WriteLine(a1.s);
            B b2 = new B();
            A a2 = b2;
            A a3 = b2;
            System.Console.WriteLine(a2.s);
            System.Console.WriteLine(((B)a3).s);
            Console.ReadKey();


        }
    }
}

运行原理: 基类的引用指向派生类的对象,  注意区别方法的隐藏和方法的覆盖(重写)比如 A  aa = new B; 则访问的是A中的成员  A的引用访问不到B

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

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

namespace ConsoleApplication4
{
    //设计一个汽车类 Vehicle,包含的属性有车轮个数 wheels和车重weight
    public 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 whe, int wei) 
        {
            this.wheels = whe;
            this.weight = wei;
        }
        public override string ToString()
        {
            return this.wheels+"_"+this.weight;
        }
    }
    //    小车类Car 是Vehicle 的子类,其中包含的属性有载人数 loader。
    public class Car : Vehicle 
    {
        int loader;

        public int Loader
        {
            get { return loader; }
            set { loader = value; }
        }
        public Car(int whe, int wei, int loa) : base(whe, wei) 
        {
            this.loader = loa;
        }
        public override string ToString()
        {
            return this.Wheels + "_" + this.Weight+"_"+this.loader;
        }
    }
    
    // 卡车类 Truck 是Car类的子类,其中包含的属性有载重量 payload。
    class Trunk : Car 
    {
        int payload;
        public Trunk(int whe, int wei, int loa, int pal) : base(whe, wei,loa) 
        {
            this.payload = pal;
        }
        public override string ToString()
        {
            return this.Wheels + "_" + this.Weight + "_" + this.Loader+"_"+this.payload;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
         
           // 每个类都有构造方法和输出相关数据的方法 。编写测试类CarTest进行测试。
            Vehicle ve = new Vehicle(4,100);
            Console.WriteLine(ve.ToString());
            Car ca = new Car(5,100,50);
            Console.WriteLine(ca.ToString());
            Trunk tr = new Trunk(6,100,50,70);
            Console.WriteLine(tr.ToString());
            Console.ReadKey();

            
        }
    }
}

 6.基类的引用指向子类的对象。

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

namespace ConsoleApplication4
{
    class A
    {
        public virtual void fun()
        {
            System.Console.Write(" A");
        }
    }
    class B : A
    {
        public override void fun()
        {
            System.Console.Write(" B");
        }
    }
    class C : A
    {
        public override void fun()
        {
            System.Console.Write(" C");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A a1 = new B();
            a1.fun();
            a1 = new C();
            a1.fun();
            Console.ReadKey();
    
        }
    }
}

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

8.请编码实现如下需求: 乐器(Instrument)分为:钢琴(Piano)、小提琴(Violin) 各种乐器的弹奏( play )方法各不相同。 编写一个测试类InstrumentTest,要求: 编写方法testPlay,对各种乐器进行弹奏测试。要依据乐器的不同,进行相应的弹奏。 在main方法中进行测试

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

namespace ConsoleApplication4
{
    public class Instrument 
    {
        public virtual void testPlay() 
        {
            Console.WriteLine("弹奏:");
        }
    }
    class Pinao : Instrument 
    {
        public override void testPlay()
        {
            base.testPlay();
            Console.WriteLine("钢琴弹奏");
        }
    }
    class Violin : Instrument 
    {
        public override void testPlay()
        {
            base.testPlay();
            Console.WriteLine("小提琴弹奏");
        }
    }
    class InstrumentTest 
    {
        public void test(Instrument ins) 
        {
            ins.testPlay();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            InstrumentTest ins1 = new InstrumentTest();
            InstrumentTest ins2 = new InstrumentTest();
            Pinao pi = new Pinao();
            Violin vi = new Violin();
            ins1.test(pi);
            ins2.test(vi);
            Console.ReadKey();

        }
    }
}

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

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

namespace ConsoleApplication4
{
    public interface Ivehicel
    {
       int money(int weigth, int distance);
    }
    public class Trunk :Ivehicel
    {
        public int money(int weigth, int distance)
        {
            if (weigth < 60 && distance < 1000)
            {
                int mm = weigth * distance * 120;
                return mm;
            }
            else 
            {
                return -1;
            }
            
        }

    }
    public class Train : Ivehicel 
    {
        public int money(int weigth, int distance) 
        {
            if (distance < 900)
            {
                return weigth * distance * 250;
            }
            else 
            {
               return  weigth*distance*300;
            }
        }
    }
    public class Fight : Ivehicel 
    {
        public int money(int weigth, int distance) 
        {
            if (distance > 500)
            {
                return weigth * distance * 750;
            }
            else 
            {
                return -1;
            }
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
           Console.WriteLine("输入货物重量和运输里程");
            int wei = Convert.ToInt32(Console.ReadLine());
            int bal = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("选择交通工具:1:卡车,2:火车,3:飞机");
            int tra = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("运费为:");
            switch (tra)
            {
                case 1: 
                    {
                        
                    Trunk tr = new Trunk();
                    
                    Console.WriteLine(tr.money(wei,bal));
                    break;
                    }
                case 2: 
                    {
                        Train train1 = new Train();
                        
                        Console.WriteLine(train1.money(wei, bal));
                        break;
                    }
                case 3:
                    {
                        Fight figth1 = new Fight();
                       
                        Console.WriteLine( figth1.money(wei, bal));
                        break;
                    }
                    
           
            }
            Console.ReadKey();
                   


            

        }
    }
}

原文地址:https://www.cnblogs.com/sunxiaoyan/p/8323675.html