让继承的类正常工作时遇到问题

时间:2013-03-02 18:07:37

标签: c# inheritance

这是我的基类:

    class Pet
    {
        //class internal variables
        string petName;
        bool gender;  //true = female; false = male
        DateTime dob;
        int goodDogs = 0;
        int goodCats = 0;
        int goodKids = 0;

        //constructors
        public Pet(string pn, bool g, DateTime birth, int d, int c, int k)
        {
            petName = pn;
            gender = g;
            dob = birth;
            goodDogs = d;
            goodCats = c;
            goodKids = k;
        }

        public Pet(string pn, string g, DateTime birth, int d, int c, int k)
        {
            petName = pn;
            if (g.ToLower() == "female")
            {
                gender = true;
            }
            else
            {
                gender = false;
            }
            dob = birth;
            goodDogs = d;
            goodCats = c;
            goodKids = k;
        }

        //Properties
        public string PetName
        {
            get
            {
                return petName;
            }
            set
            {
                petName = value;
            }
        }

        public string Gender
        {
            get
            {
                if (gender == true)
                {
                    return "Female";
                }
                else
                {
                    return "Male";
                }
            }
            set
            {
                if (value.ToLower() == "female")
                {
                    gender = true; 
                }
                else
                {
                   gender = false;
                }
            }
        }

        public DateTime DOB
        {
            get
            {
                return dob;
            }
            set
            {
                dob = value;
            }
        }

        public string GoodWithDogs
        {
            get
            {
                switch (goodDogs)
                {
                    case 1:
                        return "Yes";
                        //break;
                    case 2:
                        return "No";
                        //break;
                    default:
                        return "Unknown";
                        //break;
                }

            }
            set
            {
                goodDogs = int.Parse(value);
            }
        }

        public string GoodWithCats
        {
            get
            {
                switch (goodCats)
                {
                    case 1:
                        return "Yes";
                    //break;
                    case 2:
                        return "No";
                    //break;
                    default:
                        return "Unknown";
                    //break;
                }

            }
            set
            {
                goodCats = int.Parse(value);
            }
        }

        public string GoodWithKids
        {
            get
            {
                switch (goodKids)
                {
                    case 1:
                        return "Yes";
                    //break;
                    case 2:
                        return "No";
                    //break;
                    default:
                        return "Unknown";
                    //break;
                }

            }
            set
            {
                goodKids = int.Parse(value);
            }
        }

       // Methods
        public string GetAge()
        {
            DateTime currentDate = System.DateTime.Now;
            TimeSpan daysPassed = currentDate - dob;
            int ageInDays = daysPassed.Days;
            if (ageInDays < 350)
            {
                return (Math.Round(ageInDays / 30.0)).ToString() + " months";
            }
            else
            {
                return (ageInDays / 365).ToString("N1") + " years" ; 
            }
        }
    }

Here is what I have for my class that is trying to inherit:

public class Cat : Pet
{
    private string breed;
    private bool litterBoxTrained;
    private bool declawed;

    public Cat(string pn, string g, DateTime birth, int d, int c, int k,string b, bool lbt, bool dc) 
    : base ( pn, g, birth, d, c, k)
    {
        breed = b;
        litterBoxTrained = lbt;
        declawed = dc;
    }

    public string Breed
    {
        get
        {
            return breed;
        }
        set
        {
            breed = value;
        }
    }

    public string HouseBroke
    {
        get
        {
            if (litterBoxTrained == true)
            {
                return "Yes";
            }
            else
            {
                return "No";
            }
        }
        set
        {
            if (value.ToLower() == "yes")
            {
                litterBoxTrained = true;
            }
            else
            {
                litterBoxTrained = false;
            }
        }
    }

    public string BasicCommand
    {
        get
        {
            if (declawed == true)
            {
                return "Yes";
            }
            else
            {
                return "No";
            }
        }
        set
        {
            if (value.ToLower() == "yes")
            {
                declawed = true;
            }
            else
            {
                declawed = false;
            }
        }
    }
    public string InfoListing()
    {

        return string.Format("{0,-6}{1:-15}{2,-9}{3,-24}{4:-12}", "CAT", petName, gender, breed, base.GetAge());
    }
}

我遇到了保护问题。我得到不一致的类保护错误和两个错误尝试访问继承的变量petName和继承的方法GetAge()。其中一个警告是我不允许改变基类(宠物)类代码。

任何建议都会很棒。谢谢

1 个答案:

答案 0 :(得分:2)

  

我的类保护错误不一致

那是因为Pet隐含internal(假设它是顶级类):

class Pet

Catpublic

public class Cat : Pet

您不能将internal类用作public类的基类。要么Pet公开,要么Cat内部。

  

和两个错误尝试访问继承的变量petName

尽管上面有误导性评论,但这个变量:

 string petName;

是私有的,因为这是类中成员的默认值。因此编译器绝对有权禁止您在Pet之外使用它,即使在派生类中也是如此。您应该使用PetName属性。

  

和继承的方法GetAge()

嗯。就我所见,这应该没问题。也许我错过了什么 - 会看看。

编辑:不,编译代码后,我发现使用GetAge时没有任何问题。但是在尝试访问gender时遇到问题 - 也许这就是您的想法?编译好了:

return string.Format("{0,-6}{1:-15}{2,-9}{3,-24}{4:-12}",
                     "CAT", PetName, Gender, breed, base.GetAge());

(并非您需要使用GetAge来限定base,因为它不会在Cat中被覆盖。只需GetAge()即可。)