Home  >  Article  >  Java  >  Analysis of application examples of java encapsulation and permission modifiers

Analysis of application examples of java encapsulation and permission modifiers

WBOY
WBOYforward
2023-04-29 18:28:151314browse

1. Encapsulate the attributes so that users cannot directly input data. We need to prevent users from assigning values ​​​​to attributes using the "object.property" method. Then you need to declare the attributes as private.

2. We privatize the attributes of the class and provide public methods to get (getXxx) and set (setXxx) The value of this attribute

reflects the encapsulation and requires permission modifiers to match

1. The four permissions specified by Java (arranged from small to large) : private, Default, protected, public
2. These four permissions can be used to modify classes and their internal structures: Attributes, methods, constructors, internal classes
3. Specific Yes, 4 types of permissions can be used to modify the internal structure of a class: Attributes, methods, constructors, internal classes
If you modify a class, you can only use: default, public

Example:

package KindMethod3;
public class privateTest {
//	用private修饰,定义为私有变量,外不不能随意更改
	private int age;	//年龄
	private String name;	//名字
//	同时,我们给这两个属性创建两个可以更改他们的接口

	public void setAge(int age){
		this.age=age;
	}
	public int getAge(){
		return age;
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
	public void disPlay(){
		System.out.println("我的名字叫"+name+"今年"+age+"岁");
	}
}
package KindMethod3;
public class privateTest1 {
    public static void main(String[] args) {
        privateTest sc = new privateTest();
//        这个时候就不能直接给类中属性直接赋值了,就会报错
//        sc.age=18;
//        sc.name="小芳"

//        我们需要通过set方法给属性赋值,get取值
        sc.setAge(18);
        sc.setName("小芳");
        sc.disPlay();
    }

}

Analysis of application examples of java encapsulation and permission modifiers

Check the code to see the running results:

 public class Order {
	private int text1;
	int text2;
	public int text3;
	private void methodPrivate(){
		text1 = 1;
		text2 = 2;
		text3 = 3;
	}
	void methodDefault(){
		text1 = 1;
		text2 = 2;
		text3 = 3;
	}
	public void methodPublic(){
		text1 = 1;
		text2 = 2;
		text3 = 3;
	}
}
 public class OrderTest {
	public static void main(String[] args) {
		Order order = new Order();
		order.text2 = 1;
		order.text3 = 2;
		//出了Order类之后,私有的结构就不可以调用了
//		order.text1 = 3;//The field Order.text1 is not visible
		order.methodDefault();
		order.methodPublic();
		//出了Order类之后,私有的结构就不可以调用了
//		order.methodPrivate();//The method methodPrivate() from the type Order is not visible
	}
}

The above is the detailed content of Analysis of application examples of java encapsulation and permission modifiers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete