zoukankan      html  css  js  c++  java
  • java回顾之继承 二

    调用父类构造器

    子类不会获得父类构造器,但是子类构造器里面可以调用父类构造器初始化代码
    类似于一个构造器调用另外一个构造器:

    public class Box {
    	String name;
    	String color;
    	String weight;
    	public Box(String name,String color)
    	{
    		this.name=name;
    		this.color=color;
    	}
    	public Box(String name,String color,String weight)
    	{
    		this(name, color);//用this来调用另外一个构造器,就不用重新写了,对工程来说是好事
    		this.weight=weight;
    	}
    	
    }
    

      类似于一个构造器调用另外一个构造器使用this做关键字,在子类调用父类用super作关键调用

    public MyBox(String name,String color,String weight,String height)
    	{
    		super(name, color,weight);//用this来调用另外一个构造器,就不用重新写了,对工程来说是好事
    		this.height=height;
    	}
    	
    

      

    创建任何对象总是从该类的继承树顶层构造器开始执行,然后往下,一直到本类的构造器

  • 相关阅读:
    新浪微博采用Oauth发送图片和文字
    android proguard也有弱点
    POJ 2376
    POJ 3259
    POJ 2253
    POJ 1062
    POJ 2299
    POJ 2186
    POJ 1860
    POJ 2823
  • 原文地址:https://www.cnblogs.com/vincentmax/p/5932540.html
Copyright © 2011-2022 走看看