抽象與封裝是構建基礎的 4 個支柱中的兩個麵向對象編程。基本特征包括代碼可重用性、重寫、安全目的、數據隱藏和實現隱藏。然而,對於初學者來說,理解兩者之間的區別可能會非常令人困惑,因為它們都各自提供數據隱藏。在本文中,我們將了解這些差異以及代碼。
Java中的封裝
封裝被定義為將數據包裝在單個單元下。它是將代碼及其操作的數據綁定在一起的機製。考慮封裝的另一種方式是,它是一個保護罩,可以防止數據被該罩外的代碼訪問。從技術上講,在封裝中,類的變量或數據對任何其他類都是隱藏的,並且隻能通過聲明它們的類的任何成員函數來訪問。與封裝一樣,類中的數據對其他類是隱藏的,因此也稱為數據隱藏。封裝可以通過將類中的所有變量聲明為私有,並在類中編寫公共方法來設置和獲取變量的值來實現。
// Java program to demonstrate encapsulation
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
public 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());
}
}
相關用法
- Java AbstractCollection add()用法及代碼示例
- Java AbstractCollection addAll()用法及代碼示例
- Java AbstractCollection clear()用法及代碼示例
- Java AbstractCollection contains()用法及代碼示例
- Java AbstractCollection containsAll()用法及代碼示例
- Java AbstractCollection isEmpty()用法及代碼示例
- Java AbstractCollection remove()用法及代碼示例
- Java AbstractCollection removeAll()用法及代碼示例
- Java AbstractCollection retainAll()用法及代碼示例
- Java AbstractCollection size()用法及代碼示例
- Java AbstractCollection toArray()用法及代碼示例
- Java AbstractCollection toString()用法及代碼示例
- Java AbstractList addAll()用法及代碼示例
- Java AbstractList clear()用法及代碼示例
- Java AbstractList equals()用法及代碼示例
- Java AbstractList get()用法及代碼示例
- Java AbstractList hashCode()用法及代碼示例
- Java AbstractList indexOf()用法及代碼示例
- Java AbstractList iterator()用法及代碼示例
- Java AbstractList lastIndexOf()用法及代碼示例
- Java AbstractList listIterator()用法及代碼示例
- Java AbstractList remove()用法及代碼示例
- Java AbstractList set()用法及代碼示例
- Java AbstractList subList()用法及代碼示例
- Java AbstractMap clear()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Difference between Abstraction and Encapsulation in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。