Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1708400
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: Java

2008-08-10 22:56:04

1.   继承

1.1. 代码

/*

 * 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.2. 说明

1.       创建一个子类,使用关键字extends

2.       在子类中调用父类的方法;

3.       扩展父类的类成员;

4.       扩展父类的成员函数

5.       覆盖父类的成员函数

6.       向上转化

文件:Inheritance.zip
大小:0KB
下载:下载

 

阅读(1050) | 评论(0) | 转发(0) |
0

上一篇:Java接口

下一篇:我的BayBens.2008.08.22

给主人留下些什么吧!~~