我是新手(golang)。这就是为什么我的问题可能无关紧要(或无法回答)。
我创建了两个结构。这两个都嵌入了另一个结构。现在我想更新函数内嵌入结构的字段。
package main
import (
"fmt"
"reflect"
"time"
)
type Model struct {
UpdatedAt time.Time
}
type Fruit struct {
Model
label string
}
type Animal struct {
Model
label string
}
func update(v interface{}) {
reflectType := reflect.TypeOf(v)
reflectKind := reflectType.Kind()
if reflectKind == reflect.Ptr {
reflectType = reflectType.Elem()
}
m := reflect.Zero(reflectType)
fmt.Println(m)
}
func main() {
apple := &Fruit{
label: "Apple",
}
tiger := &Animal{
label: "Tiger",
}
update(apple)
update(tiger)
fmt.Println(apple)
fmt.Println(tiger)
}
Run Code Online (Sandbox Code Playgroud)
我希望实现该update函数,以便它将当前时间放入 …
我知道在 Python 中我们使用鸭子类型而不是实现一个具体的接口。
因此,如果您有一个函数接收需要能够进食和饮水的动物对象,您只需调用.eat()and.drink()和传入的任何对象,只需确保以任何适合它们的方式包含这些方法。
调用您的函数时,无需制作每个人都实现的接口。
但是为了文档和新的 Python3 类型提示,是否有任何正确的方法来创建接口。
就像是 ...
interface Animal:
eat()
drink()
def foo(a: Animal):
a.eat()
a.drink()
Run Code Online (Sandbox Code Playgroud)
...其中接口Animal纯粹用于文档和foo函数中的非强制类型提示。它永远不会在运行时使用,那些将动物传入函数的人仍然遵循鸭子类型协议。
这在 Python 中可能吗?
哪些规则使接口能够用作尾随 lambda参数?
我认为唯一的规则是让它在其定义上具有独特的功能,但是我遇到了以下问题:
我有这个Java接口
public interface ToolbarFragmentCallBack {
void onNavigationClick();
}
Run Code Online (Sandbox Code Playgroud)
从 Java 类调用:
public void addToolBar(int container, String title, boolean isParent,
ToolbarFragment.ToolbarFragmentCallBack callback) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
它是从 Kotlin 和 Java 文件中调用的:
科特林 (1):
addToolBar(R.id.toolbar_fragment, toolbarTitle, toolbarParent) {
presenter.onClickNavigationToolBar()
}
Run Code Online (Sandbox Code Playgroud)
爪哇(2):
addToolBar(R.id.toolbar_fragment, definition.getTitle(), false, () -> {
activity.onBackPressed();
});
Run Code Online (Sandbox Code Playgroud)
但是,我最近将界面迁移到了 Kotlin:
interface ToolbarFragmentCallBack {
fun onNavigationClick()
}
Run Code Online (Sandbox Code Playgroud)
现在 **Kotlin (1) ** 实现调用无法编译,并带有消息
类型不匹配:推断的类型是 () -> Unit 但 ToolbarFragment.ToolbarFragmentCallBack!预料之中
这个程序编译没有任何问题。
interface X{}
class A{
public void mA(){
System.out.println("mA of A");
}
}
class Demo{
public static void main(String args[]){
X ob=null;
A a1=null;
ob=(X) a1; //Legal
a1=(A)ob; //Legal
}
}
Run Code Online (Sandbox Code Playgroud)
但是下面的代码有问题?
interface X{}
final class A{
public void mA(){
System.out.println("mA of A");
}
}
class Demo{
public static void main(String args[]){
X ob=null;
A a1=null;
ob=(X) a1; //Illegal
a1=(A)ob; //Illegal
}
}
Run Code Online (Sandbox Code Playgroud)
Error:
error: incompatible types: A cannot be converted to X
ob=(X) a1; //Illegal
^
1 error
Compilation …Run Code Online (Sandbox Code Playgroud) 此代码无效,因为 T 不能同时具有 in 和 out 修饰符:
public interface IInOut<in out T>
{
}
Run Code Online (Sandbox Code Playgroud)
但是您可以执行此“解决方法”:
public interface IInOutWorkaround<in TIn, out TOut>
{
TOut Test(TIn value);
}
public class InOutWorkaround<T> : IInOutWorkaround<T, T>
{
public T Test(T value)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
第二个示例有效并且 InOutWorkaround 类对于 TIn 和 TOut 具有相同的类型,那么为什么不能直接在接口中将这两个修饰符添加到同一个 T 中呢?或者是否可以使用不同的语法?
根据 Adaic 的说法,Ada 中的接口被定义为“没有组件和具体操作的标记类型”。它可以用于多重继承。相反,抽象类型是一种标记类型,旨在用作其他类型的祖先,但不允许拥有自己的对象。
两者的本质区别仅仅是接口的多重继承特性吗?否则这些类型可以互换使用吗?
例如:
func compare(a interface{}, b interface{}) {
if a > b {
fmt.Printf("%v is bigger than %v", a, b)
}
}
Run Code Online (Sandbox Code Playgroud)
但最后,我得到了编译器错误:
cannot compare a > b (operator > not defined for interface{})compiler
Run Code Online (Sandbox Code Playgroud) 我使用的是 Android 默认评级栏,但在某些设备(三星)上,它只显示您在照片中看到的一颗星。我不知道是什么导致了这个问题以及如何解决它。感谢你的帮助。
<RatingBar
android:id="@+id/ratingBar"
android:layout_below="@+id/questionText"
android:layout_marginTop="10dp"
android:numStars="5"
android:stepSize="1"
android:max="5"
android:theme="@style/RatingBar"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用单个方法(当然)在 Java 中创建一个功能接口,该方法可以采用任何类型的参数并返回任何数据类型(即泛型方法)。
这是我到目前为止:
计算器.java
public interface Calculator<T> {
T operation(T n1, T... n2); //this is where the optional parameter comes in
}
Run Code Online (Sandbox Code Playgroud)
主程序
public static void main(String[] args) {
Calculator<Integer> addition = (n1, n2) -> n1 + n2; //this gives an error
}
Run Code Online (Sandbox Code Playgroud)
错误说:
二元运算符“+”的错误操作数类型
我有THuman实现 interface 的类ICanTalk。
但是每当我尝试检查人类是否可以说话时,Supports函数都会破坏对象实例,尽管代码中有引用。
我误解了什么?
procedure TForm1.Button1Click(Sender: TObject);
var
Obj:TInterfacedObject;
begin
Obj:=THuman.Create('Great guy');
// if Supports(Obj, ICanTalk) then //Object destroyed here if uncommented
(Obj as ICanTalk).TalkTo(Memo1.Lines);
end;
Run Code Online (Sandbox Code Playgroud)
执行
ICanTalk = interface
['{57E5EF90-EB11-421C-AAFB-18CD789C0956}']
procedure TalkTo(List:TStrings);
end;
THuman = class(TInterfacedObject, ICanTalk)
private
FName: string;
public
procedure TalkTo(List:TStrings);
property Name:string read FName;
constructor Create(const AName:string);
end;
constructor THuman.Create(const AName: string);
begin
FName:=AName;
end;
procedure THuman.TalkTo(List: TStrings);
begin
List.Add(Name+' says Hello World!');
end;
Run Code Online (Sandbox Code Playgroud)