代码 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"; public Shape(){ System.out.println(…
public class qqqq extends Parent{ public static void main(String[] args) { new Child(); } } class Parent{ static{ System.out.println("父类的静态块执行"); } { System.out.println("父类的非静态块执行"); } public Parent(){ System.out.println("父类的构造函数执…
先来看一段代码 /** * Created by bjchengpeng on 2018/7/19. */ /**运行结果 * woof * woofaa * * woof * Basenjiaa * * Basenji * Basenjiaa */ public class Bark { public static void main(String[] args) { Dog woofer = new Dog(); Dog nipper = new Basenji(); Basenji chi…
/* 子类可以重写父类中的方法,甚至是root class中的方法,比如NSObeject 的new方法,但是后提示警告如下 Method is expected to return an instance of its class type 'Student', but is declared to return 'void' */ #import <Foundation/Foundation.h> @interface Student : NSObject + (void)new; @en…
本文主要介绍Java里经常使用的子类和父类的变量实例化顺序及陷阱,并结合一个Android实例来探讨此问题.日后编程中应尽量避免此陷阱. 首先看以下一段代码: 定义一个虚类Server.java package org.yanzi.test1; public abstract class Server { private static final int DEFAULT_PORT = 900; public Server() { // TODO Auto-generated constructo…