翁凯java讲义概要(八)
继承与多态性
<1>继承
class ThisClass extends SuperClass{
//class body
}
1>Java只能做单继承
//Detergent.java
//Inheritance syntax & properties
class Cleaner{
private String s = new String(
"Cleanser");
public void append(String a){s+=a;}
public void dilute(){ append(
"dilute()");}
public void scrub(){ append(
"scrub()");}
public void apply(){ append(
"apply()");}
public void print(){ append(
"System.out.print(s);}
public static void main(String args[])
{
Cleanser x= new Cleanser();
x.dilute();
x.apply();
x.scrub();
x.print();
}
}
public class Detegent extends Cleanser{
// Change a method:
public void scrub(){
append(
"Detergent.scrub()");
super.scrub();
}
// Add methods to the interface
public void foam(){append(
"foam()");}
// Test the new class
public static void main(String args[])
{
Detergent x = new Detegent();
x.dilute();
x.apply();
x.scrub();
x.foam();
x.print();
System.out.println(
"Testing base class:"
Cleanser.main(args);
}
}
2>传递构造参数
3>初始化和类的装载
//:beet.java
//the full pocess of initialization.
class Insect{
int i=9;
int j;
Insect(){
prt(
"i="+i+",j="+j);
j=39;
static int x1=prt(
"static Insect.x1 initialized");
static int prt(String s){
System.out.println(s);
return 47;
}
}
public class Beetle extends Insect{
int k=prt(
"Beetle.k initialized");
Beetle(){
prt(
"k="+k);
prt(
"j="+j);
}
static int x2=prt(
"static Beetle.x2 initialized");
4>Upcasting
is taking an object handle and treating it as the handle of its base type.
字类和父类之间的关系是:The new class is a type of the existing class.
Upcasting 是安全的.
//:Wind.java
//Inheritance & upcasting
import java.util.*;

class Instrument{
protected void play(){System.out.println(
"Instrument playing");
static void tune(Instrument i){
i.play();
}
}
//Wind obuect are instruments
//because then have the same interface:
class Wind extends Instrument{
protected void play(){System.out.println(
"Wind playing");}
public static void main(String args[]){
Wind flute=new Wind();
Instrument.tune(flute);
}
}
5>Method call binding(绑定)
is connecting a method call to a method body.
早绑定vs.晚绑定(动态/运行时刻)
java中的所有的绑定都是晚绑定,除了final的方法.
6>final
final:This cannot be changed
Final data 只能被赋值一次
// FinalData.java
//The effect of final fields
class Value{
int i=1;
}
public class FinalData{
//Can be compile-time constants
final int i1=9;
static final int I2=99;
//Typical public constant:
public static final int I3=39;
//Cannot be compile-time constants:
final int i4=(int)(Math.random()*20);
static final int i5=(int)(Math.random()*20);

Value v1=new Value();
final Value v2=new Value();
static final Value v3=new Value();
//!final Value v4;

//Arrays:
final int[] a={1,2,3,4,5,6};
public void print(String id){
System.out.print(
7>有两种final方法:
(1,在方法上加一把锁,防止继承者改变它的意义.
(2,编译器把对该方法的调用变成inline调用.
private is final
8>Final classes
Final classes 是不允许被继承的.
Final field in a final class?
Final methods in a final class?


lunzi   2005-10-21 18:50:59 评论:0   阅读:1627   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog