java面向对象的抽象特性是什么?为什么抽象很重要?关于抽象的真相...

原文链接.

1.什么是抽象?

也许,抽象是OOP中最令人困惑的概念。 大多数人倾向于认为抽象与接口和抽象类有关。 但这不是真的。 根据我的经验,大多数程序员对此事没有充分而适当的理解。

因此,今天将是您真正了解一般抽象和Java抽象的一天。

So today would be the day you truly understand abstraction in general and abstraction in Java.

抽象是一个通用概念,它表示将“真实事物”建模为编程语言的过程。 例如,当您编写一个名为Dog的类时,您将一条真正的狗抽象为Java中的一个类型(类):
类是一种抽象

public class Dog {
}

When you declare a field named breed in the Dog class, you also abstract the real attribute of the dog in terms of a class’ variable:
属性字段也是一种抽象

public class Dog {
    String breed;//品种
}

When you implement a method named bark() in the Dog class, you also abstract the real behavior, barking, of a dog in terms of a method in a class:
方法也是一种抽象

public class Dog {
    String breed;//品种

    void bark() {
        System.out.println("Gow gow");
    }
}

如您所见,此Dog类根据类型(类)使用字段(抽象特征)和方法(抽象行为)来抽象现实世界中的真实狗。 抽象表示对象的基本特征。

当使用空方法声明接口时,也是使用抽象:

public interface Animal {

    void eat();

    void move();

    void sleep();
    
}

声明变量时,也还是使用抽象:

int numberOfLegs = 4;

从上面的示例中,所有内容都是抽象的。 当您声明一个接口,编写一个类,实现一个方法,声明一个变量等时,抽象的进度就会发生。一切都是抽象。

2. Why Is Abstraction Important?

Suppose that abstraction is not there, we cannot identify an object via its class name.
If abstraction is not there, we cannot access the attributes of an object.
If abstraction is not there, we cannot invoke the behaviors of an object.
Having said that, abstraction is the most fundamental concept on which others rely on, such as encapsulation, inheritance and polymorphism.

3. Java中关于抽象的真相

抽象与接口或抽象类无关。 抽象是将现实世界的对象建模为编程语言的过程。 因此,接口和抽象类只是此过程中使用的两种技术。
在像Java这样的面向对象的编程语言中,一切都是抽象:接口,类,字段,方法,变量等。
抽象是其他概念所依赖的基本概念:封装,继承和多态性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值