简体   繁体   English

在不实现的情况下操作抽象 class

[英]Manipulating abstract class without implementing

I have an Runnable abstract class winch represent a socket to a peer.我有一个 Runnable 抽象 class 绞盘代表对等方的插座。

I have to keep a reference of all the socket I have created so I choose to wrap it into a manager class for creating sockets and add them into a list我必须保留我创建的所有套接字的引用,因此我选择将其包装到管理器 class 以创建 sockets 并将它们添加到列表中

import java.util.concurrent.Executors

internal interface SocketInterface {
    fun onSocketConnected()
}

abstract class Socket(val host: String) : Runnable, SocketInterface {
    override fun run() {
        //Connected
        onSocketConnected()
    }
}

abstract class SocketManager : SocketInterface {
    private val executorService = Executors.newSingleThreadExecutor()
    var sockets = mutableListOf<Socket>()

    fun addSocket(host: String) {
        //val socket = Socket(host)
        executorService.execute(socket)
        sockets.add(socket)
    }
}

class Main {
    val manager = object : SocketManager() {
        override fun onSocketConnected() {
            //Peer connected correctly
        }
    }
    //add new socket to manager
    manager.addSocket('google.com')

}

But the line但是线

//val socket = Socket(host)

is on error that saying that an abstract class cannot be instantiated.说抽象 class 无法实例化是错误的。

I want that my manager don't care about implementing until it is instantiating in the top level application (Main)我希望我的经理不关心实施,直到它在顶级应用程序(主)中实例化

EDIT: I found a temporary solution implementing a default value to the abstract method onSocketConnected inside the SocketManager编辑:我找到了一个临时解决方案,在 SocketManager 内实现抽象方法 onSocketConnected 的默认值

abstract class SocketManager : SocketInterface {
    private val executorService = Executors.newSingleThreadExecutor()
    var sockets = mutableListOf<Socket>()

    override fun onSocketConnected() {/* Log.d('tag', 'uglySolution'*/}

    fun addSocket(host: String) {
        //val socket = Socket(host)
        executorService.execute(socket)
        sockets.add(socket)
    }
}

But looks like ugly and not standard但是看起来很丑而且不标准

For the moment, I resolve the problem like this.目前,我这样解决问题。

But I don't think it's the proper way, let me know if u found any solution.但是我认为这不是正确的方法,如果您找到任何解决方案,请告诉我。

import java.util.concurrent.Executors

abstract class Socket(val host: String) : Runnable, SocketInterface {
    override fun run() {
        onAbstractSocketConnected()
    }
}
internal interface SocketInterface {
    fun onAbstractSocketConnected()
}


abstract class SocketManager : SocketInterface, SocketManagerInterface {
    private val executorService = Executors.newSingleThreadExecutor()
    var sockets = mutableListOf<Socket>()

    fun addSocket(host: String) {
        val socket = object : Socket(host){
            override fun onAbstractSocketConnected(){
                onSocketConnected()
            }
        }
        executorService.execute(socket)
        sockets.add(socket)
    }
}
internal interface SocketManagerInterface {
    fun onSocketConnected()
}

class Main {
    val manager = object : SocketManager() {
        override fun onSocketConnected() {
            //Override now
            Log.d("DEBUG", "socket is connected")
        }
    }
    //add new socket to manager
    manager.addSocket('google.com')
}

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

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