首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >С#如何设置父母的字段和属性而不需要过多的构造函数?

С#如何设置父母的字段和属性而不需要过多的构造函数?
EN

Stack Overflow用户
提问于 2022-06-19 18:45:40
回答 1查看 39关注 0票数 0

我有父类产品和子类DairyProduct,我想在子类中设置父类的所有字段和属性,但是我不想做很多新的构造函数。如何在没有新构造函数的情况下在DairyProduct中设置属性(名称、价格和重量)?

还有代码,例如:

父类

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Task1
{
    public class Product
    {
        private string _name;
        public string Name { get => _name; set => _name = value; }

        private float _price;
        public float Price
        {
            get => _price;

            set => _price = value < 0 ? throw new ArgumentException() : _price = value; 
        }

        private float _weight;
        public float Weight
        {
            get { return _weight; }

            set { _weight = value < 0 ? throw new ArgumentException() : _weight = value; }
        }

        public Product() : this(null, default, default) {}

        public Product(string name, float price, float weight) 
        {
            Name = name;
            Price = price;
            Weight = weight;
        }

        public virtual void Discount(float percentage) 
        {
            if (percentage <= 100) Price = Price * (100 - percentage) / 100;
            else throw new ArgumentException();
        
        }

        public override string ToString() 
        {
            return string.Format("Name: {0}; Price: {1}; Weight: {2};", Name, Price, Weight);
        }

        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(Product))
            {
                return false;
            }
            else 
            {
                Product p = (Product)obj;
                return p.Name == Name && p.Price == Price && p.Weight == Weight; 
            }
        }
    }
}

儿童班

代码语言:javascript
运行
复制
using System;

namespace Task1
{
    class DairyProduct : Product
    {
        public readonly DateTime ExpirationDate;
        private int _daysToExpiration;
        private bool _isFresh;

        public DairyProduct()
        {
            _daysToExpiration = 90;
            ExpirationDate = DateTime.Now.AddDays(_daysToExpiration);
            _isFresh = true;
        }
        public DairyProduct(int daysToExpiration)
        {
            ExpirationDate = DateTime.Now.AddDays(daysToExpiration);
            _isFresh = true;
        }

        public DairyProduct(DateTime experationDate, int daysToExpiration) 
        {
            _daysToExpiration = daysToExpiration;
            ExpirationDate = experationDate;
            CheckExpirationDate();
        }

        public void CheckExpirationDate() => _isFresh = (ExpirationDate - DateTime.Now).Days > 0;

        public override void Discount(float percentage)
        {
            CheckExpirationDate();
            if (_isFresh)
            {
                float discontPersentage = (float)(1 - ((ExpirationDate - DateTime.Now).Days) / _daysToExpiration) + percentage / 100;
                if (discontPersentage >= 1) 
                { 
                    Price = 0;
                }
                else
                {
                    Price *= discontPersentage;
                }
            }
            else 
            {
                Price = 0;
            }
        }

        public override string ToString()
        {
            return string.Format("Name: {0}; Price: {1}; Weight: {2}; Expiration date: {3}", Name, Price, Weight, ExpirationDate);
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-19 19:17:48

可以将父属性包含在子构造函数参数中。然后调用base()方法来初始化父属性。

代码语言:javascript
运行
复制
    public DairyProduct(string name, float price, float weight) :
                                             base(name, price, weight)
    {
        _daysToExpiration = 90;
        ExpirationDate = DateTime.Now.AddDays(_daysToExpiration);
        _isFresh = true;
    }

    public DairyProduct(string name, float price, 
                        float weight, int daysToExpiration) : 
                                           base(name, price, weight)
    {
        ExpirationDate = DateTime.Now.AddDays(daysToExpiration);
        _isFresh = true;
    }

    public DairyProduct(string name, float price, float weight,                       
                        DateTime experationDate, int daysToExpiration) : 
                            base(name, price, weight)
    {
        _daysToExpiration = daysToExpiration;
        ExpirationDate = experationDate;
        CheckExpirationDate();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72679495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档