当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Abstraction和Encapsulation的区别用法及代码示例


抽象与封装是构建基础的 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());
    }
}

相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Difference between Abstraction and Encapsulation in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。