这是我的基类:
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() 的错误。需要注意的一点是我不能更改基础(宠物)类代码。
任何建议都会很棒。谢谢你