Java private实现封装处理

面向对象之中有三大主要特征:封装、继承、多态,对于封装是Java中最复杂的概念,本次所写的只不过是几个基本概念而已。

如果是想清楚封装,首先必须清楚如果没有封装会怎么样。

范例: 观察如下一段程序

class Person {
	String name; //名字
	int age;  //年龄
	public void info() {
		System.out.println("name = " + name + "、age = " + age);
	}
}
public class TestDemo {
	public static void main(String[] args) {
		Person per = new Person(); //实例化对象
		per.name = "Mo Jiu";
		per.age = -20;
		per.info();
	}
}

此时不会出现任何的语法错误,因为从int型的数据范围来讲,是允许保存有负数的,但是不会有一个人的年龄是-20,那么也就证明这个时候属于业务逻辑出错。

此时如果要想回避此类问题,那么首先要解决的就是如何可以让对象不能够直接操作年龄的属性,或者指的是让类的外部如何不能操作类中的敏感内容。所以此时解决问题的核心观念就在于:如何让内部的操作让外部不可见,此时就可以利用private关键字来实现。

范例: 利用private来实现封装

class Person {
	private String name;
	private int age;
	public void info() {
		System.out.println("name = " + name + "、age = " + age);
	}
}
public class TestDemo {
	public static void main(String[] args) {
		Person per = new Person(); //实例化对象
		per.name = "Mo Jiu";
		per.age = -20;
		per.info();
	}
}

类中的属性和方法都可以使用private定义,但是大部分的情况下很少会在方法上使用private。一旦属性的声明使用了private定义之后,那么如果其它类再直接进行该属性访问的时候就将出现如下的错误提示:

TestDemo.java:13: 错误: name可以在Person中访问private
per.name = “Mo Jiu”;
^
TestDemo.java:14: 错误: age可以在Person中访问private
per.age = -20;

此时使用了private声明之后属性安全了,外部无法直接操作了,但是新的问题又来了。那么如果现在要想进行private私有属性的访问,按照Java的设计原则就可以使用setter、getter方法。

  • setter方法:主要用于进行属性内容的设置

  • private String name:public void setName(String n)

  • getter方法:主要用于属性内容的取得

  • private String name:public String getName()

范例: 扩展Person类中的内容

class Person {
	private String name;
	private int age;
	public void setName(String n) {
		name = n;
	}
	public void setAge(int a) {
		age = a;
	}
	public String getName() {
		return name;
	}
	public int getAge(){
		return age;
	}
	public void info() {
		System.out.println("name = " + name + "、age = " + age);
	}
}
public class TestDemo {
	public static void main(String[] args) {
		Person per = new Person(); //实例化对象
		per.setName("Mo Jiu");
		per.setAge(-20);
		per.info();
	}
}

如果说现在非要进行检测,那么可以再setter里面完成。

class Person {
	private String name;
	private int age;
	public void setName(String n) {
		name = n;
	}
	public void setAge(int a) {
		if(a >= 0 && a <= 150)
			age = a;
		age = 0;
	}
	public String getName() {
		return name;
	}
	public int getAge(){
		return age;
	}
	public void info() {
		System.out.println("name = " + name + "、age = " + age);
	}
}


public class TestDemo {
	public static void main(String[] args) {
		Person per = new Person(); //实例化对象
		per.setName("Mo Jiu");
		per.setAge(-20);
		per.info();
	}
}

类的设计原则:

以后在编写类的时候类中的所有属性必须使用private封装。而使用private封装的属性如果需要被外部所使用,那么就按照格式定义相应的 setter、getter 方法。

private 实现封装的最大特征在于:只允许本类访问。

private 只是封装的第一步。

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Network Security: Private Communication in a Public World, Second Edition By Charlie Kaufman, Radia Perlman, Mike Speciner ............................................... Publisher: Prentice Hall Pub Date: April 22, 2002 Print ISBN-10: 0-13-046019-2 Print ISBN-13: 978-0-13-046019-6 Web ISBN-10: 0-13-715588-3 Web ISBN-13: 978-0-13-715588-0 Pages: 752 Copyright The Radia Perlman Series in Computer Networking and Security Acknowledgments Chapter 1. Introduction Section 1.1. Roadmap to the Book Section 1.2. What Type of Book Is This? Section 1.3. Terminology Section 1.4. Notation Section 1.5. Primer on Networking Section 1.6. Active vs. Passive Attacks Section 1.7. Layers and Cryptography Section 1.8. Authorization Section 1.9. Tempest Section 1.10. Key Escrow for Law Enforcement Section 1.11. Key Escrow for Careless Users Section 1.12. Viruses, Worms, Trojan Horses Section 1.13. The Multi-Level Model of Security Section 1.14. Legal Issues Part 1: Cryptography Chapter 2. Introduction to Cryptography Section 2.1. What Is Cryptography? Section 2.2. Breaking an Encryption Scheme Section 2.3. Types of Cryptographic Functions Section 2.4. Secret Key Cryptography Section 2.5. Public Key Cryptography Section 2.6. Hash Algorithms Section 2.7. Homework Chapter 3. Secret Key Cryptography Section 3.1. Introduction Section 3.2. Generic Block Encryption Section 3.3. Data Encryption Standard (DES) Section 3.4. International Data Encryption Algorithm (IDEA) Section 3.5. Advanced Encryption Standard (AES) Section 3.6. RC4 Section 3.7. Homework Chapter 4. Modes of Operation Section 4.1. Introduction Section 4.2. Encry

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值