/*************************************************
function:java language Technology
author :chinayaosir QQ:44633197
blog :http://blog.csdn.net/chinayaosir
note :禁止其它网站转载此文章
**************************************************/
第6章 复用类
目录
6.1组合语法
6.2继承语法
6.3使用组合与继承
6.4组合与继承的选择
6.5protected关键字
6.6增量开发
6.7向上转型
6.8final关键字
6.9初始化及类的加载
**********************************************************************
复用代码是JAVA引入注目的功能之一,
有二种方法可简化代码利用:起用用类生成对象和用现有类组装成新类
6.1组合语法
组合方法就是用现有的类组合成一个新类.
//file name sprinklersystem.java
//类组合成另一个类的成员
class watersource{
public String s;
watersource(){
System.out.println("watersource()");
s=new String("watersource Constructed");
}
public String toString(){return s;}
public void printsource(){System.out.println("s="+s);}
}
public class sprinklersystem {
private String v1;
private watersource source;
private float f;
sprinklersystem(){
v1=new String("sprinklersystem Constructed");
source=new watersource(); //调用此类的构造函数
}
public void printstring(){
System.out.println("v1="+v1+"/n"+"source="+source+"/n"+"f="+f +"/n");
}
public static void main(String[] args) {
sprinklersystem sp=new sprinklersystem();
sp.printstring();
}
}
运行结果:
watersource()
v1=sprinklersystem Constructed
source=watersource Constructed
f=0.0
**********************************************************************
6.2继承语法
先建立一个包名human,再在human包下面建立3个类human,women,men
最后在其它地方建立一个测试类demo
例如下面的类的继承功能
人类有3大功能吃喝睡
男人从人类中继承3大功能之后,另外还有2大功能抽烟和做爱,在喝水的功能上还能喝酒
女人从人类中继承3大功能之后,另外还有2大功能生小孩和做爱
/*file name human.java
*human人类三大基本功能:吃,喝,睡
*/
package human;
public class human{
protected String s;
public human(){s=new String("They can ");}
public void append(String a){s+=a;}
public String toString(){return s;}
public void eatThing(){append("eatThing()");}//吃
public void drinkWater(){append("drinkWater()");}//喝
public void sleep(){append("sleep()");}//睡
}
/*file name men.java
*男人类在继承人类的三大功能之外,还有smoking,makeLove功能
*/
package human;
public class men extends human {
//覆盖父类方法
public void drinkWater(){append("drinkWater()+drinkSpirit()");}//除了喝水还能喝酒
//添加新方法
public void smoking(){append("smoking() ");} //能抽烟
public void makeLove(){append("makeLove() ");} //能做爱
}
/*file name women.java
*女人类在继承人类的三大功能之外,还有buildChild,makeLove功能
*/
package human;
public class women extends human {
//添加新方法
public void buildChild(){append("buildChild() ");}//能生小孩
public void makeLove(){append("makeLove() ");} //能做爱
}
/*file name demo.java
*测试类
*/
import human.*;
class demo {
public static void main(String[] args) {
//测试人类三个功能human
human h=new human();
h.eatThing();
h.drinkWater();
h.sleep();
System.out.println(h);
//测试女人类五个功能
women w=new women();
w.eatThing();
w.drinkWater();
w.sleep();
w.makeLove();
w.buildChild();
System.out.println(w);
//测试男人类五个功能
men m=new men();
m.eatThing();
m.drinkWater();
m.sleep();
m.makeLove();
m.smoking();
System.out.println(m);
}
}
运行结果
They can eatThing()drinkWater()sleep()
They can eatThing()drinkWater()sleep()makeLove() buildChild()
They can eatThing()drinkWater()+drinkSpirit()sleep()makeLove() smoking()
**********************************************************************
6.3使用组合与继承
同时使用组合和继承是最常见的事情.
//file name demo.java
//盘类
class Plate{
Plate(int i){System.out.println("plate constructor");}
}
//盘类中的餐盘类
class DinnerPlate extends Plate{
DinnerPlate(int i){
super(i);
System.out.println("DinnerPlate constructor");
}
}
//器皿类
class Utensil{
Utensil(int i){ System.out.println("Utensil constructor");}
}
//器皿类中的汤匙类
class Spoon extends Utensil{
Spoon(int i){super(i);System.out.println("Spoon constructor");}
}
//餐叉类
class Fork extends Spoon{
Fork(int i){super(i);System.out.println("Fork constructor"); }
}
//餐刀类
class Knife extends Spoon{
Knife(int i){super(i);System.out.println("Fork constructor");}
}
class Customer{
Customer(int i){System.out.println("Customer constructor"); }
}
public class demo extends Customer{
private Spoon sp;
private Fork frk;
private Knife kn;
private DinnerPlate pl;
demo(int i){
super(i);
sp =new Spoon(i+2);
frk =new Fork(i+3);
kn =new Knife(i+4);
pl =new DinnerPlate(i+5);
System.out.println("placeSetting constructor");
}
public static void main(String[]agrs){
System.out.println("test begin...");
demo x=new demo(5);
}
}
运行结果:
test begin...
Customer constructor
Utensil constructor
Spoon constructor
Utensil constructor
Spoon constructor
Fork constructor
Utensil constructor
Spoon constructor
Fork constructor
plate constructor
DinnerPlate constructor
placeSetting constructor
**********************************************************************
6.4组合与继承的选择
如果类之间有has-a的关系则选择组合
如果类之间有is -a的关系则选择继承
例如汽车零件与汽车的关系就是has-a的组合关系
//编译OK with Eclipse
//file name is car.java
//发动机
class Engine{
public void start(){};
public void stop(){};
public void rev(){};
}
//轮子
class Wheel{
public void inflate(int psi){}//充气
}
//窗户玻璃
class Windows{
public void rollup(){};//上滚
public void rolldown(){};//下滚
}
//车门
class Door{
public void open(){};//打开
public void close(){};//关闭
}
//整车
public class car {
public Engine engine=new Engine();//一个发动机
public Windows[] windows=new Windows[4];//4个窗户
public Wheel[] wheel=new Wheel[4]; //4个轮子
public Door LeftDoor=new Door(); //两个门
public Door RightDoor=new Door();
car(){
for (int i=0;i<4;i++){
wheel[i]=new Wheel();
windows[i]=new Windows();
}
}
public static void main(String[] args) {
car mycar=new car();
mycar.LeftDoor.open();//打开左门
mycar.LeftDoor.close();//关上左门
mycar.windows[0].rolldown();//司机的窗户下拉
mycar.engine.start();//开动发动机
}
}
上面就是用各个零件类组装成一个汽车类的仿真程序
**********************************************************************
6.5protected关键字
它主要应用于类之间的继承的访问权限.
**********************************************************************
6.6增量开发
程序开发是一中增量过程,如同人类的学习一样,认识到这一点非常重要.
继承技术的优点之上就是增量开发.
**********************************************************************
6.7向上转型
在基类和基类导出的子类,在继承图上是向上移动的,因此一般都称为向上转型.
**********************************************************************
6.8final关键字
final表示这是固定的
它可应用于数据,方法,类
final数据,就是final 变量形式,它指定变量不能改变,这就说它是一个常量
final方法,就是指出类中此方法在继承时,锁定此方法和在继承类中不能覆盖它
final类 ,就是锁定此类,此类不能被继承或者扩充,这对提高类的运行效率非常有效
**********************************************************************
6.9初始化及类的加载
先加载类的上一级父类,然后是上一级类,一直加载到最高基类最止,
然后是执行基类的static,然后是下一级类的static,在要用到的类都加载完毕之后,就可以创建对象啦