CBrother 类
类和对象
类 | 类是一个模板,它描述一类对象的行为和状态。 |
对象 | 对象是类的一个实例,有状态和行为。例如,一条狗是一个对象,它的状态有:颜色、名字、品种;行为有:摇尾巴、叫、吃等。 |
Cbrother中的类
类可以看成是创建CBrother对象的模板。一个类可以包含以下内容
成员变量 | 成员变量是定义在类中,方法体之外的变量,这种变量在创建对象的时候实例化。成员变量可以被类中方法、构造方法和特定类的语句块访问。 |
---|---|
成员函数 | 成员函数是类的方法,每个实例化都有这些方法 |
构造函数 | 构造函数是一个和类命相同函数,但不是必须要写的。如果定义了构造函数,会在实例化时调用一次 |
class people
{
var _Name;
var _Age;
var _Nation;
var _Sex;
function people(name,age,nation)
{
_Name = name;
_Age = age;
_Nation = nation;
}
function speak()
{
var speakvalue = "hello! my name is " + _Name + ". i'm from " + _Nation + ". i'm " + _Age;
if(_Sex == 0)
{
speakvalue += ". i'm a girl.";
}
else
{
speakvalue += ". i'm a boy.";
}
print speakvalue;
}
}
创建对象
使用关键字new来创建一个对象,对象通过(.)来访问成员变量和成员方法
function main(parm)
{
var tonyObj = new people("tony",18,"england");
tonyObj._Sex = 1;
var hanmeimeiObj = new people("hanmeimei",18,"china");
hanmeimeiObj._Sex = 0;
tonyObj.speak();
hanmeimeiObj.speak();
}
结果:
hello! my name is tony. i'm from england. i'm 18. i'm a boy.
hello! my name is hanmeimei. i'm from china. i'm 18. i'm a girl.
类的继承
继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。
class 父类
{
}
class 子类 extends 父类
{
}
例子:
class peopleEx extends people
{
var _Hobby;
function peopleEx(name,age,nation,sex,hobby)
{
super(name,age,nation); //子类通过super关键字访问父类构造函数
_Sex = sex;
_Hobby = hobby;
}
function speak() //重载了父类的方法
{
super.speak(); //调用父类同名方法
print "I like " + _Hobby;
}
}
function main(parm)
{
var tonyObj = new peopleEx("tony",18,"england",1,"football");
var hanmeimeiObj = new peopleEx("hanmeimei",18,"china",0,"reading");
tonyObj.speak();
hanmeimeiObj.speak();
}
结果:
hello! my name is tony. i'm from england. i'm 18. i'm a boy.
I like football
hello! my name is hanmeimei. i'm from china. i'm 18. i'm a girl.
I like reading
虚函数
在普通的函数前面加上virtual
关键字,就声明了一个虚函数。虚函数可以不实现,就称为纯虚函数。有纯虚函数的类为虚类,不能被构造。
例子:
class peoplebase
{
virtual function getName();
virtual function getSex()
{
return 1;
}
virtual function getNation()
{
return "china";
}
virtual function getAge();
virtual function getHobby();
function speak()
{
var speakvalue = "hello! my name is " + getName() + ". i from1 " + getNation() + ". i'm " + getAge();
if(getSex() == 0)
{
speakvalue += ". i'm a girl.";
}
else
{
speakvalue += ". i'm a boy.";
}
print speakvalue;
print "I like " + getHobby();
}
}
class peopleTony extends peoplebase
{
virtual function getName()
{
return "tony";
}
virtual function getNation()
{
return "english";
}
virtual function getAge()
{
return 18;
}
virtual function getHobby()
{
return "football";
}
}
class peopleHanmeimei extends peoplebase
{
virtual function getName()
{
return "hanmeimei";
}
virtual function getSex()
{
return 0;
}
virtual function getNation()
{
return "china";
}
virtual function getAge()
{
return 18;
}
virtual function getHobby()
{
return "reading";
}
}
function main(parm)
{
var tonyObj2 = new peopleTony();
var hanmeimeiObj2 = new peopleHanmeimei();
tonyObj2.speak();
hanmeimeiObj2.speak();
}
结果:
hello! my name is tony. i from1 english. i'm 18. i'm a boy.
I like football
hello! my name is hanmeimei. i from1 china. i'm 18. i'm a girl.
I like reading