分类: Java
2008-08-10 22:56:04
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javatutorials;
/**
*
* @author wanpor
*/
class Shape{
int x;
int y;
Shape(){
x = 0;
y = 0;
}
Shape(int x,int y){
this.x = x;
this.y = y;
}
protected void print(){
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
class Circle extends Shape{
int r;
Circle(){
r = 0;
}
Circle(int x,int y,int r){
super(x,y);
this.r = r;
}
public void print(){
super.print();
System.out.println("r = " + r);
}
}
public class Inheritance {
public static void main(String[] args){
Circle cl = new Circle();
cl.print();
Circle cl2 = new Circle(100,100,10);
cl2.print();
}
}
1. 创建一个子类,使用关键字extends
2. 在子类中调用父类的方法;
3. 扩展父类的类成员;
4. 扩展父类的成员函数
5. 覆盖父类的成员函数
6. 向上转化
|