简体   繁体   English

无法理解 Java/Kotlin 中的抽象

[英]Unable to understand Abstraction in Java/Kotlin

I have difficulty understanding the concept of Abstraction.我很难理解抽象的概念。 By definition abstraction is the process of hiding certain details and showing only essential information to the user from w3schools .根据定义,抽象是隐藏某些细节并仅向用户显示来自w3schools 的基本信息的过程 So below is my class -所以下面是我的课-

open class SimpleClass {
    fun engine() {}
}

class ChildSimpleClass : SimpleClass()

abstract class AbstractClass {
    abstract fun engine()
}

class AbstractClassImpl : AbstractClass() {
    override fun engine() {}
}

interface Interface {
    fun engine()
}

class InterfaceImpl : Interface {
    override fun engine() {}
}

fun main(){
    //first case
    val simpleClass: SimpleClass  = ChildSimpleClass()
    simpleClass.engine()

    //second case
    val abstractClassImpl:AbstractClass = AbstractClassImpl()
    abstractClassImpl.engine()

    //third case
    val interfaceImpl: Interface = InterfaceImpl()
    interfaceImpl.engine()
}  

1.Does the first case qualify to be abstraction ? 1.第一种情况是否有资格成为抽象?

2.If yes, then why do we say we achieve abstraction using "interfaces and abstract classes" and why not using normal classes ? 2.如果是,那为什么我们说我们使用“接口和抽象类”来实现抽象,为什么不使用普通类? from here and many other sources which I went through这里和我经历的许多其他来源

3.If not, then why does it not qualify to be abstraction as my class only knows about ChildSimpleClass and not about the SimpleClass is this not hiding details about SimpleClass ? 3.如果不是,那么为什么它没有资格成为抽象,因为我的班级只知道 ChildSimpleClass 而不是 SimpleClass 这不是隐藏有关 SimpleClass 的详细信息吗?

you have multiple choices to do abstraction with.您有多种选择可以进行抽象。 the first choice must be interface because you can implement multiple interfaces but not more than one class.第一个选择必须是接口,因为您可以实现多个接口但不能超过一个类。 for example, you can have a class that implements both a Driver and an Eater interface.例如,您可以拥有一个同时实现DriverEater接口的类。 but can't do that with classes.但不能用类做到这一点。 and then you can use an instance of your class as either one.然后你可以使用你的类的一个实例作为任何一个。

the second choice must be abstract classes.第二个选择必须是抽象类。 the difference between an abstract class and an interface is state.抽象类和接口之间的区别在于状态。 abstract classes can have state.抽象类可以有状态。 and if you need state in your abstraction you should consider using abstract classes.如果您在抽象中需要状态,您应该考虑使用抽象类。 for example a Driver might not need state, but a Car might need it to know if it's started or not(totally depends on your requirements)例如Driver可能不需要状态,但Car可能需要它来知道它是否已启动(完全取决于您的要求)

the problem with an open class is that you can't force subclasses to implement a method.开放类的问题是你不能强制子类实现一个方法。 and also you always need to have implementations for your methods.而且你总是需要为你的方法实现。

also, abstraction is not only about hiding details.此外,抽象不仅仅是隐藏细节。 it's also about setting rules for your class.这也是关于为你的班级制定规则。 for example a class Engine should always have a name and that changes when the type of engine changes.例如,一个类Engine应该总是有一个名称,并且随着引擎类型的改变而改变。 and a start() method that works based on name:和一个基于名称工作的start()方法:

abstarct class Engine {

    var isStarted = false

    abstract val name: String

    fun start() {
        isStarted = true
        println("engine $name started")
    }
}

so as long as you can't define rules with open classes, you should not consider it an abstraction.所以只要你不能用开放类定义规则,你就不应该认为它是一个抽象。 but as Tenfour04 said in comments, This is just a semantic argument about the word abstraction.但正如 Tenfour04 在评论中所说,这只是关于抽象这个词的语义争论。 but it's important to know the difference.但了解差异很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM