关于abstract类及其子类顺序

news/2024/7/4 0:49:58 标签: class, abstract
class="baidu_pl">
class="article_content clearfix">
class="markdown_views prism-dracula">

首先abstract

class="prettyprint">class=" hljs cs">class="hljs-keyword">public class="hljs-keyword">abstract class="hljs-keyword">class Glyph {

    class="hljs-keyword">abstract class="hljs-keyword">void draw();
    class="hljs-keyword">public class="hljs-title">Glyph() {
        System.class="hljs-keyword">out.println(class="hljs-string">"Glyph() before draw()");
        draw();
        System.class="hljs-keyword">out.println(class="hljs-string">"Glyph() after draw()");
    }


}

接来下子类

class="prettyprint">class=" hljs java">class="hljs-keyword">public class="hljs-class">class="hljs-keyword">class class="hljs-title">RoundGlyph class="hljs-keyword">extends class="hljs-title">Glyph{
      class="hljs-keyword">int radius = class="hljs-number">1;

    class="hljs-keyword">public class="hljs-title">RoundGlyph(class="hljs-keyword">int i ) {
        radius = i;
        System.out.println(class="hljs-string">"RoundGlyph().radius="+radius);

    }

    class="hljs-annotation">@Override
    class="hljs-keyword">void draw() {
        System.out.println(class="hljs-string">"RoundGlyph().radius="+radius);
    }

}

最好main方法运行

class="prettyprint">class=" hljs cs">class="hljs-keyword">public class="hljs-keyword">class Text {
    class="hljs-keyword">public class="hljs-keyword">static class="hljs-keyword">void class="hljs-title">main(String[] args) {
        class="hljs-keyword">new RoundGlyph(class="hljs-number">5);
    }

}


输出结果为:
Glyph() before draw()
RoundGlyph().radius=0
Glyph() after draw()
RoundGlyph().radius=5

按照普通类的继承方式 应该是先走完父类构造方法中的操作再走子类中的。
抽象的触发父类draw()方法也连带着子类中的一同触发。并且是在子类并没有为
radius分配该有空间操作之前进行。因此radius的值并不是默认的初始值1,
而是在未采取任何操作之前,系统默认的初始值0.


http://www.niftyadmin.cn/n/860245.html

相关文章

线段树+懒标记处理区间问题

线段树处理区间加上一个数,和区间乘以一个数 #include <cstdio> #include <iostream> using namespace std;typedef long long LL; const int N 1e5 10; int n, p, m; int w[N]; struct Node{int l, r, sum, add, mul; } tr[4 * N];void pushup(int u) {tr[u].…

二叉树的遍历(C语言)

递归遍历 #include <stdio.h> #include <stdlib.h> #include<stdbool.h>typedef int Elemtype;typedef struct BiTNode //树结点 {Elemtype data;struct BitTNode *lchild,*rchild; //指向左右子树的指针 } BiTNode,*BiTree;BiTree CreateLink() //二叉树…

JAVA各种编码所占用的字节数

闲来无事&#xff0c;研究了一下几种常用的编码格式所占用的字节数。写了一个小工程大家一目了然&#xff0c;话不多说上代码。 String str "于先森ABC";byte[] bytes1 str.getBytes("gbk");System.out.print("gbk: ");for (byte b : bytes1)…

中序线索二叉树

#include <stdio.h> #include <stdlib.h> #include<stdbool.h>#define MaxSize 50typedef int Elemtype;//线索二叉树的定义 typedef struct ThreadNode {Elemtype data;struct TreadNode *lchild,*rchild;int ltag,rtag; } ThreadNode,*ThreadTree;ThreadT…

王道数据结构习题代码5.3.3(树与二叉树)

第三题(二叉树非递归后序遍历) #include <stdio.h> #include <stdlib.h> #include<stdbool.h>#define MaxSize 50typedef int Elemtype;typedef struct BiTNode {Elemtype data;struct BiTNode * lchild,*rchild; } BiTNode,*BiTree;typedef struct Stack …

AndroidStudio,Idea,VsCode,Vs快捷键

AndroidStudio IDEA 搜索class : SHIFTShift搜索使用&#xff1a;AltF7参数提示 &#xff1a; CtrlAltSpace全局搜索变量&#xff1a;CtrlShiftF 易冲突 &#xff0c; 选择操作方法为 Edit > Find > Find in path提示导包 AltEnter;AltInsert 生成代码(如get,set方法,构…

二分查找(跳石头)

调试头洛谷 #include <bits/stdc.h> #define ll long long #define pr pair<int int> using namespace std;const int maxn5e410; ll n,m,ans,L;ll d[maxn];int check(ll mid) {int cnt0,now0;for(int i1;i<n1;i){if(d[i]-d[now]<mid)cnt;else nowi;}if(cnt…

Android NestedScrolling 实战

Android NestedScrolling 实战 21 AUGUST 2015 on Android 从 Android 5.0 Lollipop 开始提供一套 API 来支持嵌入的滑动效果。同样在最新的 Support V4 包中也提供了前向的兼容。有了嵌入滑动机制&#xff0c;就能实现很多很复杂的滑动效果。在 Android Design Support 库中…