//对象的关系:共享、包含、继承
指定声明的成员或类型的可访问性。
public 访问不受限制
protected 访问仅限于包含类或从包含类派生的类型
internal 访问仅限于当前项目
protected internal 访问仅限于从包含类派生的当前项目或类型
private 访问仅限于包含类型
sealed class ren //使用sealed对类的密封,结果是类无法被继承,密封类不能同时是抽象类,因为抽象类总是希望被继承的
对比public class ren
public partial class lei_jicheng : System.Web.UI.Page
{
//sealed class ren
public class ren
{
protected int yan;
protected int shou;
public delegate void RenChengHender (string s, int e);
public event RenChengHender RenChenged;
public ren()
{
yan = 1;
shou = 2;
}
public ren (int y, int s)
{
yan = y;
shou = s;
}
public int Yan
{
get {return yan; }
set
{
yan = value;
string s = "yan";
RenChenged(s, value);
}
}
public int Shou
{
get {return shou; }
set
{
shou = value;
string s = "shou";
RenChenged(s, value);
}
}
public string yanjing(int y)
{
switch(y)
{
case 0:
return "做人要厚道,再怎么着也给人家一只眼睛啊"
break;
case 1:
return "还有一只眼睛呢?"
break;
case 2:
return "普通人嘛,没意思"
break;
case 3:
return "二郎神有儿子了"
break;
default:
return "别玩啦,会死人的"
break;
}
}
}
public class man:ren
{
private int age;
private string sex;
public man(int a, string s, int y):base(6,888)
{
age = a;
sex = s;
yan = y;
}
public string ManRen
{
get
{
return "一个" + sex + "人,TA有" + yan + "只眼睛和" + shou + "只手,TA今年" + age + "岁了";
}
}
public string suibian()
{
return base.Shou.ToString() + "," + this.Yan.ToString();
}
public string yanjing(string a, int s)
{
return a + "和" + s;
}
public new string yanjing(int y)
{
string b1 = base.yanjing(y);
return "父类方法被覆写,现在返回" + y + ",调用基类方法的结果是" + b1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
ren aa = new ren(2, 2);
aa.RenChenged += new ren.ChengHender(aa_RenChenged); //注册一个事件,相反是-=
aa.Yan = 3;
aa.Shou = 2;
Response.Write("<br/>眼睛有"+ aa.Yan +"只,手有"+ aa.Shou );
Response.Write("<br/>方法返回的是:" + aa.yanjing(3) );
man mrr = new man(23, "男", 4);
Response.Write("<br/>" + mrr.ManRen );
Response.Write("<br/>子类对象调用父类方法返回的是(说法有问题):" + mrr.yanjing(0) );
Response.Write("<br/>子类对象调用父类方法返回的是(说法有问题):" + mrr.yanjing("aa", 5) );
Response.Write("<br/>" + mrr.suibian() );
}
void aa_RenChenged(string s, int e)
{
if (s == "yan")
{
if (e == 3)
{
Response.Write("我不信有3只眼睛");
}
}
if (s == "shou")
{
if(e == 2)
Response.Write("<br/>正常的2只手");
}
}
}
{
//sealed class ren
public class ren
{
protected int yan;
protected int shou;
public delegate void RenChengHender (string s, int e);
public event RenChengHender RenChenged;
public ren()
{
yan = 1;
shou = 2;
}
public ren (int y, int s)
{
yan = y;
shou = s;
}
public int Yan
{
get {return yan; }
set
{
yan = value;
string s = "yan";
RenChenged(s, value);
}
}
public int Shou
{
get {return shou; }
set
{
shou = value;
string s = "shou";
RenChenged(s, value);
}
}
public string yanjing(int y)
{
switch(y)
{
case 0:
return "做人要厚道,再怎么着也给人家一只眼睛啊"
break;
case 1:
return "还有一只眼睛呢?"
break;
case 2:
return "普通人嘛,没意思"
break;
case 3:
return "二郎神有儿子了"
break;
default:
return "别玩啦,会死人的"
break;
}
}
}
public class man:ren
{
private int age;
private string sex;
public man(int a, string s, int y):base(6,888)
{
age = a;
sex = s;
yan = y;
}
public string ManRen
{
get
{
return "一个" + sex + "人,TA有" + yan + "只眼睛和" + shou + "只手,TA今年" + age + "岁了";
}
}
public string suibian()
{
return base.Shou.ToString() + "," + this.Yan.ToString();
}
public string yanjing(string a, int s)
{
return a + "和" + s;
}
public new string yanjing(int y)
{
string b1 = base.yanjing(y);
return "父类方法被覆写,现在返回" + y + ",调用基类方法的结果是" + b1;
}
}
protected void Page_Load(object sender, EventArgs e)
{
ren aa = new ren(2, 2);
aa.RenChenged += new ren.ChengHender(aa_RenChenged); //注册一个事件,相反是-=
aa.Yan = 3;
aa.Shou = 2;
Response.Write("<br/>眼睛有"+ aa.Yan +"只,手有"+ aa.Shou );
Response.Write("<br/>方法返回的是:" + aa.yanjing(3) );
man mrr = new man(23, "男", 4);
Response.Write("<br/>" + mrr.ManRen );
Response.Write("<br/>子类对象调用父类方法返回的是(说法有问题):" + mrr.yanjing(0) );
Response.Write("<br/>子类对象调用父类方法返回的是(说法有问题):" + mrr.yanjing("aa", 5) );
Response.Write("<br/>" + mrr.suibian() );
}
void aa_RenChenged(string s, int e)
{
if (s == "yan")
{
if (e == 3)
{
Response.Write("我不信有3只眼睛");
}
}
if (s == "shou")
{
if(e == 2)
Response.Write("<br/>正常的2只手");
}
}
}