我刚刚在这篇博文中看到了这段代码
type Logger interface {
Debug(msg string, keyvals ...interface{}) error
Info(msg string, keyvals ...interface{}) error
Error(msg string, keyvals ...interface{}) error
}
type tmLogger struct {
srcLogger kitlog.Logger
}
// Interface assertions
var _ Logger = (*tmLogger)(nil) // What is this?
// ... interface definition ...
Run Code Online (Sandbox Code Playgroud)
什么是"接口断言"?
你好,我有一个简单的问题.由于我不太熟悉C++而且我现在正在查看一些代码,我想知道在类似于这个函数中发生了什么:
#include "SomeInterface.h"
class SomeClass {
public:
...
void SomeFunction(...){
if (interface) { //What is asked here exactly?
/*do something*/
}
}
/*...*/
protected:
SomeInterface* interface;
}
Run Code Online (Sandbox Code Playgroud)
由于我来自Java,我想知道在if-bracket中提出了什么问题.当变量属于bool类型时,我习惯于只能在if括号中放置一个孤独的变量名.但是因为这里是一个自定义界面类型我很困惑.
提前感谢您的回答.
这是我的代码.我有一个基本接口来定义常用功能.
interface BaseInterface<T> {
void baseFunc(T param)
}
interface MyInterface1 extends BaseInterface<MyEntity1> {
void func1()
}
interface MyInterface2 extends BaseInterface<MyEntity2> {
void func2()
}
Run Code Online (Sandbox Code Playgroud)
我的班级是:
abstract class BaseClass implements BaseInterface<T> {
// ...some common things
}
class ClassA extends BaseClass implements MyInterface1 {
@Override
public void func1() {...}
@Override
public void baseFunc(MyEntity1 param) {...}
}
class ClassB extends BaseClass implements MyInterface2 {
@Override
public void func2() {...}
@Override
public void baseFunc(MyEntity2 param) {...}
}
Run Code Online (Sandbox Code Playgroud)
不同的类使用不同的bean MyEntity1和MyEntity2来做同样的事情.所以我用通用T定义了baseFunc().
但这里有一个问题."两种方法都有相同的擦除但是没有覆盖其他baseFunc(T)和baseFunc(MyEntity1)......"
如何解决这个问题?
顺便说一句,当我删除泛型并将baseFunc移动到MyInterface1和MyInterface2时,它工作正常.但我不认为这是最好的方式.
假设我有一个接口IA,包含一个名为Foo的泛型方法.
public interface IA {
int Foo<T>(T otherType);
}
Run Code Online (Sandbox Code Playgroud)
我希望T与派生类的类型相同:
class A : IA {
int Foo(A otherType)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我试过以下(语法错误):
public interface IA {
int Foo<T>(T otherType) where T : this;
}
Run Code Online (Sandbox Code Playgroud)
我的约束如何才能实现这一目标?
我printData()在MyInterface.php和中创建了相同的方法,MyTrait.php我printData()从我的控制器调用方法来实现MyInterface.php和使用MyTrait.php.但是这个方法总是来自MyInterface.php.请解释一下为什么会这样?
MyInterface.php
<?php namespace App\Interfaces;
interface MyInterface
{
public function printData();
}
Run Code Online (Sandbox Code Playgroud)
MyTrait.php
<?php namespace App\Traits;
trait MyTrait
{
public function printData()
{
dd("From Trait");
}
}
Run Code Online (Sandbox Code Playgroud)
HomeController.php
<?php namespace App\Http\Controllers;
use App\Interfaces\MyInterface;
use App\Traits\MyTrait;
use App\User;
class HomeController extends Controller implements MyInterface
{
use MyTrait;
public function index()
{
$this->printData();
}
public function printData() {
// TODO: Implement printData() method.
dd("From Interface");
}
}
Run Code Online (Sandbox Code Playgroud) 我最近遇到了与类创建/建模相关的问题.
模型动物王国作为类.使用您的类创建表示为虚拟动物园的各种对象.将20只动物添加到这个虚拟动物园(在你的程序的主要方法中,让每只动物发出声音.这不应该是20种不同的物种).
我想到了这个问题,并决定使用抽象类Animal而不是接口.然后我添加了两个类Bird,Mammal由Animal继承.
我的实施
public abstract class Animal{
abstract Sound MakeSound();
}
public class Bird : Animal
{
private Sound _sound;
public Bird(Sound sound){
_sound = sound;
}
public Sound MakeSound(){
return _sound;
}
}
//same as above with Mammals
public class Program
{
Animal crow = new Bird("Crow sound");
Console.WriteLine(crow.MakeSound());
Animal dog = new Mammal("Dog sound");
Console.WriteLine(dog.MakeSound());
// and so on with other animals
}
Run Code Online (Sandbox Code Playgroud)
我的问题是 -
请在网上找到一些重要信息,以便我可以改善这些问题.谢谢.
我一生都无法在Kotlin或Android的Kotlin中实例化片段之外的接口。这是Java中的标准过程,其内容如下:
MyInterface mInterfaceListener = new MyInterface(this);
mInterfaceListener.invokeSomeGenericMethod();
Run Code Online (Sandbox Code Playgroud)
请注意,mInterfaceListener是指接口,而不是onCLickListener或类似的东西
如何在Kotlin中实例化接口?如何制作“监听器”并触发界面功能?
以下是我出于学习目的而在一个非常简单的应用程序中进行的一些尝试。注意变量mPresenterListener是一个接口
class QuoteActivity : QuoteContract.ViewOps, AppCompatActivity() {
private lateinit var vText: TextView
private lateinit var vFab: FloatingActionButton
private lateinit var mPresenterListener: QuoteContract.PresenterOperations
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mPresenterListener = this.mPresenterListener
vText=findViewById(R.id.main_quote)
vFab=findViewById(R.id.main_fab)
vFab.setOnClickListener{
mPresenterListener.onQuoteRequested()
}
}
override fun displayQuote(quote: String) {
vText.text = quote
}
}
Run Code Online (Sandbox Code Playgroud)
而我的主持人:
class QuotePresenter(private val viewListener: QuoteContract.ViewOps): QuoteContract.PresenterOperations {
private lateinit var modelListener: QuoteContract.ModelOperations
init {
modelListener = this.modelListener
}
override fun …Run Code Online (Sandbox Code Playgroud) 我发现很难用英语描述这个,因为它不是我的第一语言所以我希望问题/标题有意义.
我需要知道我是否可以创建一个可以返回实现中任何类型的方法的接口.
我想制作一个可以注入另一个类的通用映射器.但我想知道我是否可以制作通用映射器,或者我是否需要为不同类型制作不同的接口.对我来说,制作相同但只有不同返回类型的不同接口看起来毫无意义?
有点像这样:
public interface IMapper()
{
T MapObject() where T = the return type
List<T> MapList() where T = the return type;
}
public class OrderMapper : IMapper()
{
OrderDetails MapObject()
{
mapping code...
return OrderDetails;
}
}
public class CustomerMapper : IMapper()
{
CustomerDetails MapObject()
{
mapping code...
return CustomerDetails;
}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?或者我只是迟钝,我需要制作不同的接口.
我刚试图实现一个不可变的接口属性后发现,C#显然不支持我正在寻找的行为.我计划的很简单,对象会订阅一个包含不可变泛型属性的接口,这意味着没有setter的属性.但是,令我惊讶的是,订阅类仍然可以添加setter而不会出现任何错误.事实上,似乎C#接口完全忽略了属性的签名.它所关心的只是属性类型和名称.
例如:
namespace Some.Arbitrary.Framework
{
public interface IIdentifiable<T>
{
/// <summary>
/// Classes that subscribe to this interface
/// may still add a 'set;' signature.
/// </summary>
T Identifier { get; } // the lack of 'set;' is ignored
}
}
Run Code Online (Sandbox Code Playgroud)
我在StackOverflow上看到很多帖子说C#不支持这种行为,但我的问题是:为什么C#不支持我所描述的接口中的不可变属性?我打算做什么背后的基本设计缺陷?
我对c#的了解很少.
做以下事情的正确方法是什么?
目标是创建一个getvalue()可以跨多种结构工作的接口.
public interface IBOB
{
T GetValue<T>() where T:struct
}
class YAY:IBOB
{
public bool GetValue<bool>()
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)