Loner_li 面向对象 实例练习--要求:用户输入上述四个字段的值,实例化一个Computer类,然后调用类中方法输出电脑详细信息

来源:互联网 发布:网络盒子电视直播软件 编辑:程序博客网 时间:2024/05/17 23:00

1.Computer类:
品牌名称:Brand
系列:Model
价格:price
颜色:color

方法:输出电脑的相信信息(就是这4个属性的值)

要求:用户输入上述四个字段的值,实例化一个Computer类,然后调用类中方法输出电脑详细信息

 

Computer类

 

   public class computer
    {
        //private string ComputerName;

        //public string Name
        //{
        //    get { return ComputerName; }
        //    set { ComputerName = value; }
        //}
       public computer() { }
       public computer(string model,string brand,double price,string color)
       { this.ComModel = model;
           this.ComBrand = brand;
         
           this.ComPrice = price;
           this.ComColor = color;
       }
        private string Model;

        public string ComModel
        {
            get { return Model; }
            set { Model = value; }
        }
        private string Brand;

        public string ComBrand
        {
            get { return Brand; }
            set { Brand = value; }
        }
        private double Price;

        public double ComPrice
        {
            get { return Price; }
            set { Price = value; }
        }
        private string Color;

        public string ComColor
        {
            get { return Color; }
            set { Color = value; }
        }

        public string writeComputer()
        {
            return string.Format("品牌为:{0},系列为:{1},价格为:{2},颜色为:{3}",ComModel,ComBrand,ComPrice,ComColor);
        }
    }

Program.cs  主程序

    class Program
    {
        static void Main(string[] args)
        {
            computer com = new computer();
          
          
            Console.WriteLine("请输入品牌名称:");
            string ppmc = Console.ReadLine();
            com.ComModel = ppmc;
            Console.WriteLine("请输入系列名称:");
            string brand = Console.ReadLine();

            com.ComBrand = brand;
            Console.WriteLine("请输入价格:");

            double price = Convert.ToDouble(Console.ReadLine());
            com.ComPrice = price;
            Console.WriteLine("请输入颜色:");

            string color = Console.ReadLine();
            com.ComColor = color;

           string info= com.writeComputer();

           Console.WriteLine(info);

           Console.ReadKey();
        }
       
    }

原创粉丝点击