zoukankan      html  css  js  c++  java
  • shape

    当多个类之间有继承关系时,创建子类对象会导致父类初始化块的执行。

    package 类与继承;

    public class Test {

        public static void main(String[] args)  {

            Shape shape = new Circle();//初始化,调用

            System.out.println(shape.name);

            shape.printType();//构造函数

            shape.printName();

        }

    }

    class Shape {

        public String name = "shape";//定义name的数据类型,并赋值

         

        public Shape(){

         //constructor 属性返回对创建此对象的数组函数的引用

            System.out.println("shape constructor");

        }

         

        public void printType() {

            System.out.println("this is shape");

        }

         

        public static void printName() {

            System.out.println("shape");

        }

    }

    //继承父类Shape

    class Circle extends Shape {

        public String name = "circle";

         

        public Circle() {

            System.out.println("circle constructor");

        }

         

        public void printType() {

            System.out.println("this is shape");//输出角

        }

         

        public static void printName() {

            System.out.println("circle");

        }

    }

  • 相关阅读:
    BZOJ1001 BJOI2006 狼抓兔子
    NOI2015 程序自动分析
    高斯消元法
    [POJ] 3666 Making the Grade
    [CODEVS] 2185 最长公共上升子序列
    [模板] LIS
    [CF] 219D Choosing Capital for Treeland
    [POJ] 2823 Sliding Window
    [BZOJ] 1072 [SCOI2007]排列perm
    [POJ] 1094 Sorting It All Out
  • 原文地址:https://www.cnblogs.com/1995-qxl/p/4887782.html
Copyright © 2011-2022 走看看