这是我的基类:
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()。其中一个警告是我不允许改变基类(宠物)类代码。
任何建议都会很棒。谢谢
答案 0 :(得分:2)
我的类保护错误不一致
那是因为Pet
隐含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());
(并非您需要使用GetAge
来限定base
,因为它不会在Cat
中被覆盖。只需GetAge()
即可。)