java object-oriented exercise rights modifier and encapsulation 8

/*

  • The meaning and role of the package
  • 1, hiding the internal complexity of the subject, only open to the public simple interface for easy recall, to improve system scalability, maintainability.
  • To hide the hidden, to the exposed exposed. It is the idea of ​​the design package.
  • 2, programming pursuit of high cohesion, low coupling
  • High cohesion: the internal operational details of the data class is done internally, external intervention is not allowed
  • Low coupling: only a small amount of external exposure method is used to call
  • 4, the specific circumstances: When you create a class, member variables inside the assignment can be invoked via the "Object Properties." Manner.
  • Assignment will be limited and the range of data types, there is no other restrictions. In practice, however, often you need to make additional property assignment
  • limits. Restrictions can not be reflected when the property declaration, can be added by means of restrictions. Other users also need to avoid the "Object Property"
  • The way property assignment. We need to declare private property (private). In this case the attribute will reflect the encapsulation.
  • 5, while the privatization of property, it is necessary to provide the public (public) method to assign (set) and access (get) operation on the property.
  • 6, other encapsulation embodies: Private method, Singleton (private constructor)
  • 7, packaging and modifiers to achieve hidden by permission.
  • Four rights modifiers in java; from small to large order of, private, default (default), protected, public.
  • Inner classes: private
  • default: an inner bag inside the same class
  • protected: class within a packet within the same package different subclasses
  • public: sub-category inside the same package different packages of a similar project under
  • Modifier written before the member variables
  • Modifiers can be used in classes and the internal structure (properties, method, constructor, internal). Class code block not
  • For permission class (class) can only be modified with the public and default
  • public class can be accessed anywhere.
  • default category type can only be accessed within the same package.
  • 8, if two packages a, c have the same name within a class B, in which a package bc introduced within a, c to create an instance of the object is an object bc, bc executed properties and methods.

*/

package com.atguigu.contact;
public class Object_Encapsulation {
	//权限修饰符测试
	public int orderPublic;//公开属性
	int orderDefault;//缺省属性
	private int orderPrivate;//私有属性
	public void methodPublic() {
		orderPublic = 1;
		orderDefault = 1;
		orderPrivate = 1;
		methodPrivate();
	}//公开方法
	void methodDefault() {
		orderPublic = 2;
		orderDefault = 2;
		orderPrivate = 2;
		methodPrivate();
	}//缺省方法
	private void methodPrivate() {
		orderPublic = 3;
		orderDefault = 3;
		orderPrivate = 3;
	}//私有方法
/*
  package com.atguigu.contact;
  public class ObjectTest {//同包不同类,调用私有属性和方法报错
    Object_Encapsulation o = new Object_Encapsulation();
	 o.orderDefault = 1;
	 o.orderPublic  = 1;
	// o.orderPrivate = 1; 
	 o.methodPublic();
	 o.methodDefault();
	// o.methodPrivate();
	  }	
 */
	/*
	 package exercise;
	import java.util.*;
	import com.atguigu.contact.*;//不同包不同类下,需要先导包,default属性和方法不可用
	public class ObjectTest {
	Object_Encapsulation o = new Object_Encapsulation();
	 //o.orderDefault = 1;
	 o.orderPublic  = 1;
	// o.orderPrivate = 1; 
	 o.methodPublic();
	 //o.methodDefault();
	// o.methodPrivate();
	 }
	 */
		
	public static void main(String[] args) {
	rabbit rab = new rabbit();
	rab.name = "007";
	System.out.println(rab.name);	
	//rab.age;//私有属性只能在类内调用	
	 rab.setAge(-1);
	 rab.setAge(32);
	 rab.setAge(10);
	 System.out.println(rab.getAge());
	}
}
//创建类以测试权限修饰符
	class rabbit{
		//设置类的属性
		String name = setName("cube");
		public int weight;		
		private int age;
		//创建方法设置私有属性
		public void setAge(int i){//赋值方法有形参无返回值
			if(i >= 0 && i <= 30) {//通过语句对赋值情况做限制。也可以抛出异常
				
			}else if (i < 0) {
				System.out.println("兔子还不存在");
			}else {
				System.out.println("兔子成精了");
			}
		}
		//创建方法访问私有属性
		public int getAge() {//获取方法无形参有返回值
			return age;
		}	
		
		private String setName(String s) {
			 return name = s;				
		}
		
		void getName(){
			System.out.println(name);
		}								
	}
发布了47 篇原创文章 · 获赞 1 · 访问量 1072

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/104052618