java继承

本文通过一个具体的Java程序示例,展示了类的继承机制及其构造过程。包括基类Shape与派生类Circle的定义、静态块及构造函数的执行顺序等关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.wt010.myextends;
/**
 * @author konecms
 * @date 2018年1月9日 上午10:44:06
 * 
 */
 public class Shape {
    public int i=100;
    public String s="shape string";
    
    {
        System.out.println("shape  block");
    }
    public Shape( ) {
        System.out.println("Shap Constructor .");
    }

    Draw a=new Draw("shape");
    public void printType() {
        System.out.println("type:shape"); 
    }
    public static void printName() {
        System.out.println("name:shape.");
    }
    protected void dosome() {
        System.out.println("shape private");
    }

}
package com.wt010.myextends;
/**
 * @author konecms
 * @date 2018年1月9日 上午11:16:56
 * 
 */
public class Circle extends Shape {
    public  int i=200;
    public static String s="circle string";
    static {
        System.out.println("circle static block .");
    }
    Draw a=new Draw("circle");
    
    public Circle() {
        System.out.println("Circle Constructor");
    }
    public void dosome() {
        
    }
    public void printType() {
        
        System.out.println("type:circle");
    }
    public static void printName() {
        System.out.println("name:circle");
    }

}
package com.wt010.myextends;
/**
 * @author konecms
 * @date 2018年1月9日 上午11:50:59
 * 
 */
public class Draw {

    public Draw(String type) {
        System.out.println(type+" Draw Constructor .");
    }
}
package com.wt010.myextends;
/**
 * @author konecms
 * @date 2018年1月9日 上午11:20:39
 * 关于继承:
 * 
 */
public class MyExtendsDEMO {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Shape a=new Circle();
        //Shape b=new Circle();
        //int i=a.i;
        //System.out.println(i);
        //a.printType();
        //Circle.printName();
         
    }

}

结果:

circle static block .
shape  block
shape Draw Constructor .
Shap Constructor .
circle Draw Constructor .
Circle Constructor

转载于:https://www.cnblogs.com/phpdo100/p/8251899.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值