Java的封装
封装被定义为在单个单元下封装数据。它是将代码与其操作的数据绑定在一起的机制。考虑封装的另一种方式是,它是一个保护屏障,可防止数据被该屏障之外的代码访问。
从技术上讲,在封装中,类的变量或数据对任何其他类都是隐藏的,只能通过声明它们的类的任何成员函数才能访问。
与封装一样,一个类中的数据对其他类是隐藏的,所以也称为数据隐藏。
封装可以通过将类中的所有变量声明为私有,并在类中编写公共方法来设置和获取变量的值来实现。
// Java program to demonstrate encapsulation
public class Encapsulate {
// private variables declared
// these can only be accessed by
// public methods of class
private String geekName;
private int geekRoll;
private int geekAge;
// get method for age to access
// private variable geekAge
public int getAge()
{
return geekAge;
}
// get method for name to access
// private variable geekName
public String getName()
{
return geekName;
}
// get method for roll to access
// private variable geekRoll
public int getRoll()
{
return geekRoll;
}
// set method for age to access
// private variable geekage
public void setAge(int newAge)
{
geekAge = newAge;
}
// set method for name to access
// private variable geekName
public void setName(String newName)
{
geekName = newName;
}
// set method for roll to access
// private variable geekRoll
public void setRoll(int newRoll)
{
geekRoll = newRoll;
}
}
// Class to access variables
// of the class Encapsulate
class TestEncapsulation {
public static void main(String[] args)
{
Encapsulate obj = new Encapsulate();
// setting values of the variables
obj.setName("Harsh");
obj.setAge(19);
obj.setRoll(51);
// Displaying values of the variables
System.out.println("Geek's name: " + obj.getName());
System.out.println("Geek's age: " + obj.getAge());
System.out.println("Geek's roll: " + obj.getRoll());
// Direct access of geekRoll is not possible
// due to encapsulation
// System.out.println("Geek's roll: " + obj.geekName);
}
}
输出:
Geek's name: Harsh
Geek's age: 19
Geek's roll: 51
Java的抽象
数据抽象是一种仅向用户显示基本细节的属性。不向用户显示琐碎或非必需的单元。例如:汽车被视为汽车而不是其单个组件。
数据抽象也可以定义为仅识别对象所需特征而忽略不相关细节的过程。对象的属性和行为将其与其他类似类型的对象区分开来,也有助于对对象进行分类/分组。
// Java program to illustrate the concept of Abstraction
abstract class Shape {
String color;
// these are abstract methods
abstract double area();
public abstract String toString();
// abstract class can have a constructor
public Shape(String color)
{
System.out.println("Shape constructor called");
this.color = color;
}
// this is a concrete method
public String getColor()
{
return color;
}
}
class Circle extends Shape {
double radius;
public Circle(String color, double radius)
{
// calling Shape constructor
super(color);
System.out.println("Circle constructor called");
this.radius = radius;
}
@Override
double area()
{
return Math.PI * Math.pow(radius, 2);
}
@Override
public String toString()
{
return "Circle color is "
+ super.color
+ "and area is : "
+ area();
}
}
class Rectangle extends Shape {
double length;
double width;
public Rectangle(String color,
double length,
double width)
{
// calling Shape constructor
super(color);
System.out.println("Rectangle constructor called");
this.length = length;
this.width = width;
}
@Override
double area()
{
return length * width;
}
@Override
public String toString()
{
return "Rectangle color is "
+ super.color
+ "and area is : "
+ area();
}
}
public class Test {
public static void main(String[] args)
{
Shape s1 = new Circle("Red", 2.2);
Shape s2 = new Rectangle("Yellow", 2, 4);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
输出:
Shape constructor called
Circle constructor called
Shape constructor called
Rectangle constructor called
Circle color is Redand area is : 15.205308443374602
Rectangle color is Yellowand area is : 8.0
抽象和封装的区别:
Abstraction | Encapsulation |
---|---|
Abstraction is the process or method of gaining the information. | While encapsulation is the process or method to contain the information. |
In abstraction, problems are solved at the design or interface level. | While in encapsulation, problems are solved at the implementation level. |
Abstraction is the method of hiding the unwanted information. | Whereas encapsulation is a method to hide the data in a single entity or unit along with a method to protect information from outside. |
We can implement abstraction using abstract class and interfaces. | Whereas encapsulation can be implemented using by access modifier i.e. private, protected and public. |
In abstraction, implementation complexities are hidden using abstract classes and interfaces. | While in encapsulation, the data is hidden using methods of getters and setters. |
The objects that help to perform abstraction are encapsulated. | Whereas the objects that result in encapsulation need not be abstracted. |