0

这是我的基类:

    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() 的错误。需要注意的一点是我不能更改基础(宠物)类代码。

任何建议都会很棒。谢谢你

4

1 回答 1

2

我收到不一致的类保护错误

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

class Pet

而是:Cat_public

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());

(不是说你需要有资格GetAgebase因为它没有被覆盖Cat。就GetAge()可以了。)

于 2013-03-02T18:11:44.537 回答