【JAVA学习路-think in java】:public、protected、private同一编译单元中的调用

package pkg;

class Base{// a BASE class
	public int public_i=1;
	protected int protect_j=2;
	private int private_k=3;
	
	public double public_Base_ii=1.0;	  
	protected double protect_Base_jj=2.0;
	private double private_Base_kk=3.0;
//----
	public void Base_method_public() {
		System.out.println("A Public method from Base class");
	}
	protected void Base_method_protected() {
		System.out.println("A Protected method from Base class");
	}
	private void Base_method_private() {
		System.out.println("A Private method from Base class");
	}
	public void SameNameMethod() {
		System.out.println("A public method-SameNameMethod from: Base class");
	}
	
}
class ExtendBase extends Base{// an EXTEND class from BASE
	public int public_i=11;// will this conflict with Base variable?
	protected int protect_j=12;
	private int private_k=13;
//------
	public void Ext_method_public() {
		System.out.println("A Public method from Extend class");
	}
	protected void Ext_method_protected() {
		System.out.println("A Protected method from Extend class");
	}
	private void Ext_method_private() {
		System.out.println("A Private method from Extend class");
	}
	public void SameNameMethod() {
		System.out.println("A public method-SameName Method from: ExtendBase class");
	}
	//------
	void verify() {
		System.out.println();
		System.out.println("***************  Verify Function is executing now    *******************");
		
		System.out.println("you can get ExtendBase's private variable in ExtendBase class:private_k="+private_k);
		this.Ext_method_private();//ExtendBase method using it's own private method
		
		System.out.println();
		System.out.println("------    Now, Let's verify whether can we get private data/method from Base in ExtendBase   --------");
		/*System.out.println("ExtendBase gets from Base: private int private_Base_kk="+this.private_Base_kk);
		 * The field Base.private_Base_kk is not visible
		 */
		
		this.Base_method_public();
		this.Base_method_protected();
		/*this.base_method_priavte();
		 * The method base_method_priavte() is undefined for the type ExtendBase
		 */
		System.out.println("Trying to get data from Base Class in ExtendBase: public_Base_ii="+public_Base_ii);
		System.out.println("Trying to get data from Base Class in ExtendBase: protect_Base_jj="+protect_Base_jj);
		/*System.out.println("Trying to get data from Base Class in ExtendBase: private_Base_kk="+private_Base_kk);
		 * The field Base.private_Base_kk is not visible
		 */ 	
	}
}
public class Chapter6 {
	public static void main(String[] args) {
		Base objB=new Base();
		ExtendBase objE=new ExtendBase();
		
		//Step1: verify BASE class
		System.out.println("******    Envoke data/method from one BASE object    ******");
		System.out.println("Base object,Public i="+objB.public_i);
		System.out.println("Base object,Protect j="+objB.protect_j);
		/*System.out.println("Base object,Private k="+objB.private_k);
		 *The field Base.private_k is not visible
		 */
		objB.Base_method_public();
		objB.Base_method_protected();
		/*objB.Base_method_private();
		 *The method Base_method_private() from the type Base is not visible
		 */
		
		//STEP2: verify Extend class
		System.out.println();
		System.out.println("******    Envoke data/method from one ExtendBase object    ******");
		System.out.println("ExtendBase object,Public i="+objE.public_i);//REPLACE the SAME name variable in BASE
		System.out.println("ExtendBase object,Protected j="+objE.protect_j);//REPLACE the SAME name variable in BASE
		/*System.out.println("ExtendBase object,Private k="+objE.private_k);
		 * The field ExtendBase.private_k is not visible
		 */
		objE.Ext_method_public();
		objE.Ext_method_protected();
		/*objE.Ext_method_private();
		 * The method Ext_method_private() from the type ExtendBase is not visible
		 */
		
		
		System.out.println(">>>>  Envoke BASE data/Method in Extend <<<<");
		System.out.println("ExtendBase object,wanna to take it's Base variable public_Base_ii="+objE.public_Base_ii);
		System.out.println("ExtendBase object,wanna to take it's Base variable protect_Base_jj="+objE.protect_Base_jj);
		/*System.out.println("ExtendBase object,wanna to take it's Base variable private_Base_kk="+objE.protect_Base_kk);
		 * protect_kk cannot be resolved or is not a field
		 */
		objE.Base_method_public();
		objE.Base_method_protected();
		//objE.Base_method_private();//The method Base_method_private() from the type Base is not visible
		
		System.out.println();
		objE.SameNameMethod();//will REPLACE the Base Class's SameNameMethod
		
		objE.verify();//verify Private variable/method from BASE in ExtendBase	
	}
}

 

OUTPUT:

******    Envoke data/method from one BASE object    ******
Base object,Public i=1
Base object,Protect j=2
A Public method from Base class
A Protected method from Base class

******    Envoke data/method from one ExtendBase object    ******
ExtendBase object,Public i=11
ExtendBase object,Protected j=12
A Public method from Extend class
A Protected method from Extend class
>>>>  Envoke BASE data/Method in Extend <<<<
ExtendBase object,wanna to take it's Base variable public_Base_ii=1.0
ExtendBase object,wanna to take it's Base variable protect_Base_jj=2.0
A Public method from Base class
A Protected method from Base class

A public method-SameName Method from: ExtendBase class

***************  Verify Function is executing now    *******************
you can get ExtendBase's private variable in ExtendBase class:private_k=13
A Private method from Extend class

------    Now, Let's verify whether can we get private data/method from Base in ExtendBase   --------
A Public method from Base class
A Protected method from Base class
Trying to get data from Base Class in ExtendBase: public_Base_ii=1.0
Trying to get data from Base Class in ExtendBase: protect_Base_jj=2.0
 

 

总结:

1、子类若定义了与基类相同名字的变量或方法,这会覆盖掉基类方法。

2、类生成对象后,对象无法调用该基类的private方法和变量。

3、在继承类中,无法使用基类中的private方法和变量(in 继承类的定义中、继承类生成的对象调用时)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值