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");

        }

    }

  • 相关阅读:
    Noip2012 开车旅行
    「NOI2018」归程
    2019.10.30 队测(晚上)
    洛谷P1138 第k小整数
    洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
    Noip-pj2018游记
    洛谷P4994 终于结束的起点
    《退役的你》
    《膜你抄》
    洛谷P5087 数学
  • 原文地址:https://www.cnblogs.com/1995-qxl/p/4887782.html
Copyright © 2011-2022 走看看