权限

						本包		非本包	    非本包的子类		本类
 public 公共的			   y		 y			y			 y
 protected 受保护的		  y		    X		   y		    y  
 default 默认缺省/友元	 y		   X		  X            y
 private 私有 			X        X           X           y

package com.znsd.two;

public class Main {

	public static void main(String[] args) {
		Test t=new Test();//
		t.method();//在本包中可以调用public修饰的静态方法
		t.methodTwo();//在本包中可以调用protected修饰的静态方法
		/*t.methodTree();
		t.methodFour();*/
		t.fun();//在本包中可以调用public修饰的非静态方法
		t.funTwo();//在本包中可以调用protected修饰的非静态方法
		/*t.funThree();
		t.funFour();*/

	}

}

package com.znsd.two;

public class Test {

	public static void method() {
		System.out.println("公共的静态方法");
	}
	protected static void methodTwo() {
		System.out.println("受保护的静态方法");
	}
	static void methodThree() {
		System.out.println("默认的静态方法");
	}
	private static void methodFour() {
		System.out.println("私有的静态方法");
	}
	
	public void fun() {
		System.out.println("公共的非静态方法");
	}
	
	protected void funTwo() {
		System.out.println("受保护的非静态方法");
	}
	
	void funTree() {
		System.out.println("默认的非静态方法");
	}
	
	private void funFour() {
		System.out.println("私有的非静态方法");
	}
}
class TestTwo{
	//外部类:可以用哪些权限修饰符修饰:可以用public和缺省修饰,不可以用private和protected修饰 
	protected class Inter{
		//内部类:可以用所有权限修饰符修饰 
	}
}

package com.znsd.one;
import com.znsd.two.Test;
public class Main {

	public static void main(String[] args) {
		Test t=new Test();//非本包中的公共类可以调用
		t.method();//在非本包中public修饰的静态方法可以调用,其它不可以调用
		/*t.methodTwo();
		t.methodThree();
		t.methodFour();*/
		t.fun();//在非本包中public修饰的非静态方法可以调用,其它不可以调用
		/*t.funTwo();
		t.funThree();
		t.funFour();*/

	}

}
package com.znsd.one;

import com.znsd.two.Test;

public class MainTwo extends Test{MainTwo 与Test不在同一个包中,但是mainTwo是Test的子类,非本包的子类

	public static void main(String[] args) {
		MainTwo main=new MainTwo();//非本包的子类
		main.method();//调用父类的public修饰的静态方法
		main.methodTwo();//调用父类的protected修饰的静态方法
		/*main.methodThree();
		main.methodFour();*/
		main.fun();//调用父类的public修饰的非静态方法
		main.funTwo();//调用父类的protected修饰的非静态方法
		/*main.funThree();
		main.funFour();*/

	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值