( ! ) Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in D:\www\up\CSharp\class05.php on line 20
Call Stack
#TimeMemoryFunctionLocation
10.0000356200{main}( )...\class05.php:0

( ! ) Warning: include(http://pub.houheaven.com/Nav02/Nav_deep2.htm): failed to open stream: no suitable wrapper could be found in D:\www\up\CSharp\class05.php on line 20
Call Stack
#TimeMemoryFunctionLocation
10.0000356200{main}( )...\class05.php:0

( ! ) Warning: include(): Failed opening 'http://pub.houheaven.com/Nav02/Nav_deep2.htm' for inclusion (include_path='.;C:\php\pear') in D:\www\up\CSharp\class05.php on line 20
Call Stack
#TimeMemoryFunctionLocation
10.0000356200{main}( )...\class05.php:0

① 类的封装性

② 类的继承性

继承性是指在定义新的类时,不需要重新编写代码,就能够包含另一个类定义的字段、属性、方法等成员,这种在一个类的基础上建立新的类的过程,叫做继承。

被继承的类叫做基类或父类,继承得到的类叫做派生类或子类。继承一次只能从一个基类继承,叫做单一继承。

格式:访问修饰符 class 派生类类名:基类类名

实例:打开

[ 源代码 ]

public class Example1

{

public int add(int x, int y)

{ return x + y; }

}

public class Example2:Example1

{

}

……

example1 ex1 = new example1();

example2 ex2 = new example2();

Console.WriteLine(ex1.add(5,6));

Console.WriteLine(ex2.add(5,6));


[ 输出结果 ]

11

11

③ 类的多态性

多态性是指在程序运行时,基类(父类)对象执行一个基类与派生类都具有的同名方法时,程序可以根据基类对象类型的不同(基类还是派生类)进行正确的调用。

虚方法与方法重写

默认情况下,派生类从基类继承属性和方法。如果继承的方法需要在派生类中有不同的行为,则可以重写它,即可以在派生类中定义该属性或方法的新实现。用户必须分别用virtual关键字与override关键字在基类与派生类中声明其同名方法。

基类中的声明格式为:

public virtual 方法名(参数列表){……}

派生类中的声明格式:

Public override 方法名(参数列表){……}

实例:打开

[ 源代码 ]

class Example1

{

public virtual int add(int x, int y)

{ return x + y; }

}

class Example2:Example1

{

public override int add(int x, int y)

{ return x + y + 5; }

}

……

example1 ex1 = new example1();

example2 ex2 = new example2();

Console.WriteLine(ex1.add(5,6));

Console.WriteLine(ex2.add(5,6));


[ 输出结果 ]

11

16

显示框架
显示框架
显示框架
显示框架