顾客类的派生

1.任务描述
顾客分为普通顾客和VIP顾客两种,普通顾客只保存姓名,VIP顾客还保存VIP卡号,从普通顾客中派生出VIP类。
2.任务要求
(1)普通顾客类中定义一个带参数的构造函数实现顾客姓名的初始化,再定义一个输出顾客信息的方法。
(2)VIP顾客类的构造函数继承父类的构造函数,完成姓名和VIP卡号的初始化;输出顾客信息的方法中姓名信息的输出调用父类的输出方法。
(3)为VIP顾客类再定义一个只有姓名参数的构造函数,VIP卡号取默认值none,通过(2)中的构造函数来实现。
3.知识点提示
本任务主要用到以下知识点。
(1)类的定义及使用。
(2)类带参数的构造函数的继承。
(3)方法的覆盖与重载。
(4)派生类调用基类中的方法。
(5)同一个类中利用已有的构造函数再去创建构造函数的参考示例如下。
public VIPCustomer(string name,string vipNo) {实现代码}
public VIPCustomer(string name):this(name,“none”) {}

废话不多说 上代码

using System;

namespace SX_51
{ 
    public class Customer { 
        private string name;
        public Customer(string name) { 
            this.name = name;
        }
        public void ct()
        { 
            Console.WriteLine("顾客姓名为:{0}",name);
        }
    }
    public class VIPCustomer : Customer { 
        private string name;
        private string id;

        public VIPCustomer(string name, string id):base(name)
        { 
            this.name = name;
            this.id = id;
        }
        public void ct()
        { 
            base.ct();
        }
        public void cc() { 
            Console.WriteLine("顾客卡号为:{0}",id);
        }
        public VIPCustomer(string name) : this(name, "none") {  }
    }
    
    class Program
    { 
        static void Main(string[] args)
        { 
            Customer a = new Customer("王三");
            VIPCustomer b = new VIPCustomer("李四","10010");
            VIPCustomer c = new VIPCustomer("赵五");
            a.ct();
            b.ct();
            b.cc();
            c.ct();
            c.cc();
        }
    }
}

本文地址:https://blog.csdn.net/qq_44699910/article/details/109637171