zoukankan      html  css  js  c++  java
  • java继承引用方法、变量

    记录一下,不是太懂其实,有高人可以指点一二

    class parent {
    	String name = "parent";
    	parent() {
    		//测试的两种情况
    		this.say();//I'm son
    		say();//I'm son
    	}
    	void say() {
    		System.out.println("I'm parent");
    	}
    }
    
    class son extends parent {
    	String name = "son";
    	
    	son() {
    		super.say();//I'm parent
    		this.say();//I'm son,跟直接调用say一样
    		System.out.println(super.name);//parent
    		System.out.println(this.name);//son,跟直接打印name一样name
    	}
    	
    	void say() {
    		System.out.println("I'm son");
    	}
    }
    
    void test() {
            son s = new son();
            System.out.println(s.name);//son
            s.say();//I'm son
            parent p = new son();
            System.out.println(p.name);//parent,这里类似于super.name
            p.say();//I'm son		    
    }
    

    通过这个例子大概可已得出,实例化的时候,子类和父类都生成一份,只不过,对方法和实例变量默认都加上了this引用,除非有super修饰。对于变量,经过向上转型,可以得到父类的值。

  • 相关阅读:
    huffman压缩解压文件
    C++ fstream 详解
    huffman编码
    ios cocoapods
    POI2Vec: Geographical Latent Representation for Predicting Future Visitors
    latex生成pdf 出现missing$ inserted
    矩阵、向量求导法则

    矩阵范数求导
    hive
  • 原文地址:https://www.cnblogs.com/so-easy/p/12107588.html
Copyright © 2011-2022 走看看