Java access modifiers to distinguish explain private, default, protected, public's

Here Insert Picture DescriptionTo achieve encapsulation (using access control breaks):
the Java those details need to be controlled through the use of package access specifier, those details needs to be exposed.
Four kinds of access control characters are: private, default, protected, public , explain encapsulation of objects, access rights can be decentralized to the lowest possible, through them, thereby improving safety.

A similar modifier with a bun of all classes
Private *
default * * (The reason why you can access the same sub-class package because the display is the same package)
protected * * *
public * * * *

Description:
1, Private represent private, only you can access your own class
2, default means no modification symbol, the same class can access a package of
3, protected indicating protection, the same package of classes and other subclasses package can also visit
4, public disclosure represents all packets can access all classes
Note:. "class name". "examples" access mode or into the form, with reference to the specific circumstances of use. (General use "instance." In the form of the class name. Generally static members and methods)

Code is case

package com.it.package1;

public class ClassOne {
	
	private  int privateProp;
	
	 int defaultProp;
	
	 protected int protectedProp;
	
	 public int publicProp;
	
	
	void testSameClass(){
		privateProp = 0;
		
		defaultProp = 0;
		
		protectedProp = 0;
		
		publicProp = 0;
	}
	

}
package com.it.package1;

public class ClassOneStatic {
	
	private static int privateProp;
	
	static int defaultProp;
	
	static protected int protectedProp;
	
	static public int publicProp;
	
	
	void testSameClass(){
		privateProp = 0;
		
		defaultProp = 0;
		
		protectedProp = 0;
		
		publicProp = 0;
	}
	

}

package com.it.package1;

public class ClassTwo{
	
	void testdifferentClass() {
//		new ClassOne().privateProp;
		new ClassOne().defaultProp = 1;
		new ClassOne().protectedProp = 1;
		new ClassOne().publicProp = 1;
		
//		ClassOneStatic.privateProp;
		ClassOneStatic.defaultProp = 1;
		ClassOneStatic.protectedProp = 1;
		ClassOneStatic.publicProp = 1;
		
		
	}

}
package com.it.package1;

public class ClassChild extends ClassOne{
	
	void testChild() {
//		privateProp = 1 ;
		defaultProp = 1;
		protectedProp = 1;
		publicProp = 1;
	}

}

package com.it.package2;

import com.it.package1.ClassOne;
import com.it.package1.ClassOneStatic;

public class ClassFour {
	
	void testDifferentPackage(){
//		new ClassOne().privateProp;
//		new ClassOne().defaultProp = 1;
//		new ClassOne().protectedProp = 1;
		new ClassOne().publicProp = 1;
		
//		ClassOneStatic.privateProp = 1;
//		ClassOneStatic.defaultProp = 1;
//		ClassOneStatic.protectedProp = 1;
		ClassOneStatic.publicProp = 1;
	}

}

package com.it.package2;

import com.it.package1.ClassOneStatic;

public class ClassChild extends ClassOneStatic {
	
	void testdifferentPackageChild() {
//		ClassOneStatic.privateProp = 1;
//		ClassOneStatic.defaultProp = 1;
		ClassOneStatic.protectedProp = 1;
		ClassOneStatic.publicProp  = 1;
	}

}

Published 37 original articles · won praise 29 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42755868/article/details/104831340