• java 的访问权限控制


    package test06;
    
    public class PermissionModel {
    	private int age;
    	public String name;
    	
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    
    }


    package test06;
    
    public class PermissionTest {
    	public static void main(String[] args) {
    		PermissionModel p=new PermissionModel();
    		p.getAge();
    		p.setAge(12);
    		p.getName();
    		p.setName("wang");
    		p.name="wangsong";//public 方法可以在类外部访问
    		p.age=12;//这个方法是错误的,私有方法,只能在类内部访问
    	}
    	//public 可以直接访问这个类内部的元素
    	public void setName(){
    		new PermissionModel().name="li";
    	}
    
    }

    访问权限的控制加强了具体实现的隐藏

    定制自己的工具库取代输出
    package test06.util;
    import java.util.Locale;
    
    public class Print {
    	public static void print(boolean b){
    		System.out.print(b);
    	  }
    	  public static void print(char c){
    		System.out.print(c);
    	  }
    	  public static void print(char []s){
    		System.out.print(s);
    	  }
    	  public static void print(double d){
    		System.out.print(d);
    	  }
    	  public static void print(float f){
    		System.out.print(f);
    	  }
    	  public static void print(int i){
    		System.out.print(i);
    	  }
    	  public static void print(long l){
    		System.out.print(l);
    	  }
    	  public static void print(String s){
    		System.out.print(s);
    	  }
    	  public static void printf(Locale l,String format,Object args){
    		System.out.printf(l, format, args);
    	  }
    	  public static void println(){
    		System.out.println();
    	  }
    	  public static void println(boolean b){
    		System.out.println(b);
    	  }
    	  public static void println(char c){
    		System.out.println(c);
    	  }
    	  public static void println(char []s){
    		System.out.println(s);
    	  }
    	  public static void println(double d){
    		System.out.println(d);
    	  }
    	  public static void println(float f){
    		System.out.println(f);
    	  }
    	  public static void println(int i){
    		System.out.println(i);
    	  }
    	  public static void println(long l){
    		System.out.println(l);
    	  }
    	  public static void println(Object obj){
    		System.out.println(obj);
    	  }
    	  public static void println(String s){
    		System.out.println(s);
    	  }
    
    	}

     用static引入包

    package test06;
    import static test06.util.Print.*;
    public class Hello {
    	public static void main(String[] args) {
    		print("Hello World!");
    	}
    
    
    }
  • 相关阅读:
    【Charles】高级过滤
    【Python】遍历字典
    sso
    数据可视化大屏,屏幕多分辨率适配方案,且在任意屏幕下保持16:9的比例等比缩放
    从实战的角度谈微服务(八):Sentinel简单使用
    软件研发的成本,效率与质量
    Java获取POST请求Json格式参数(raw)
    Linux日志文件分析
    mongodb非交互式命令
    Java线程池之Executors.newSingleThreadExecutor()
  • 原文地址:https://www.cnblogs.com/JAYIT/p/5603042.html
Copyright © 2020-2023  润新知