如何在继承两个接口的类中重写两个不同接口中具有相同名称和签名的函数。是否可以 ?
注:这是我被问到的一个面试问题。
例子:
interface example1
{
int add(int a, int b);
}
interface example2
{
int add(int a, int b);
}
public class cls : example1, example2
{
public int add(int a, int b)
{
int c = a + b;
return c;
}
}
Run Code Online (Sandbox Code Playgroud) 我有这个界面
public interface Deserializable{
static <T> T deserialize(byte[] bytes) {
ReflectionDeserializer rd = new ReflectionDeserializer();
return rd.Deserialize(bytes);
}
}
Run Code Online (Sandbox Code Playgroud)
存在一个问题,ReflectionDeserializer 在其构造函数中需要一个类实例。在这种情况下,我想要传入的类是该implements语句所在的类的类,因此如果我有 public class Cat implements Deserializable,我希望将接口放入Cat.class构造函数中。
我该怎么做呢?
我正在尝试在实用程序包中实现一个函数,用于对给定的任何类型的切片进行分页。它应该接受一部分接口加上页面和页面大小,并且必须返回相同类型的接口。
但是,当我尝试使用该函数时,出现错误:我的输入与输入不
interface{}匹配
无法使用结果([]entity.Something 类型的变量)作为 utility.PaginateSlice 参数中的 []interface{} 值
这是我的功能:
// PaginateList, paginates a slice based upon its page and pageSize.
func PaginateSlice(x []interface{}, page, pageSize int) []interface{} {
var maxSize int = len(x)
start := (page - 1) * pageSize
end := start + pageSize - 1
if start > maxSize || page < 1 || pageSize < 1 {
start = 0
end = 0
} else if end > maxSize {
end = maxSize
}
return …Run Code Online (Sandbox Code Playgroud) 在 go 中,您可以将内联接口与函数签名结合起来吗?或者这是不可能的,唯一的选择是创建结合较小接口的第三个接口?
// _Interfaces_ are named collections of method
// signatures.
package main
import (
"fmt"
"math"
)
// Here's a basic interface for geometric shapes.
type geometry interface {
area() float64
perim() float64
}
type appearance interface {
color() string
texture() string
}
// For our example we'll implement this interface on
// `rect` and `circle` types.
type rect struct {
width, height float64
}
type circle struct {
radius float64
}
// To implement an interface in …Run Code Online (Sandbox Code Playgroud) 众所周知(???),Delphi 对接口对象的接口引用进行引用计数,并在其引用计数降至零时销毁这些对象,通常是因为所有相关的接口变量都超出了范围。
假设我使用全局接口变量来引用持久对象:
type
tMySyncClass = class(TInterfacedObject,TmyInterface); ...
tMyAsyncClass = class(TInterfacedObject,TmyInterface); ...
var MyInterfaceVar : TmyInterface;
procedure MyProc();
begin
MyInterfaceVar := tMySyncClass.create();
....
MyInterfaceVar := tMyAsyncClass.create();
end
Run Code Online (Sandbox Code Playgroud)
调用 后MyProc,我有一个 的实例,并对其进行了tMyAsyncClass一个(全局)引用。MyInterface
我还有一个未引用的MySyncClass对象吗?重新分配是否会MyInterfaceVar触发第一个 ( SyncClass) 对象的销毁?
我在C#模块中有这个代码:
if (customer is IBuyer) { customer.WaiveServiceFee(); }
Run Code Online (Sandbox Code Playgroud)
只要客户是实现IBuyer的对象,这就可以很好地编译.但是使用条件的整个想法是测试客户对象是否实现了IBuyer.如果没有,我得到一个编译时错误,客户不包含WaiveServiceFee的定义(WaiveServiceFee是由IBuyer实现产生的方法 - 当然,它不是客户类的一部分).
我对C#不太熟悉,知道如何应用上面的逻辑在运行时调用WaiveServiceFee并使程序可编译?
谢谢.
我开始认为我不像我想的那样理解多态性.
我有以下情况:
public class Testing {
public static void main(String[] args){
interTest myTest = new classTest();
myTest.classMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
使用给定的界面:
public interface interTest {
public boolean checkBoolean();
public void method();
}
Run Code Online (Sandbox Code Playgroud)
然后是具体的课程:
public class classTest implements interTest{
public classTest() {
// TODO Auto-generated constructor stub
}
public void classMethod(){
System.out.println("fail");
}
// Both method() and checkBoolean() are overridden here & do nothing.
}
}
Run Code Online (Sandbox Code Playgroud)
Oracle文档演示了如何实现接口然后添加其他方法,甚至实现多个接口(因此包括不在其中一个接口中的方法),我认为这很常见,直到我遇到试图自己尝试这样做的问题.
在这种情况下,我无法访问,classMethod因为它不在界面内.
The method classMethod() is undefined for the type interTest
Run Code Online (Sandbox Code Playgroud)
我对多态性的理解是什么?我想在表单中声明一个变量:
Interface object …Run Code Online (Sandbox Code Playgroud) 希望你能帮助理解这一点,这样做似乎很自然:
public interface IJob
{
bool started { get; set; }
}
public interface IJobManager
{
void StartJob(IJob job);
}
public class SomeJob : IJob
{
public bool started { get; set; }
public void OnlySomeJobDoesThis();
}
public class SomeJobManager : IJobManager
{
public void startJob(SomeJob job) { } // --> Error'SomeJobManager' does not implement interface member 'IJobManager.StartJob(IJob)'
}
Run Code Online (Sandbox Code Playgroud)
但正如你所看到的,它不会编译.我无法想象为什么,..只要SomeJob实现IJob接口,编译器就不应该真正关心?
我知道IEnumerable是一个接口,我也知道当一个类实现一个接口时,它必须为该接口中的方法实现特定的主体.最近我一直在写一个程序,我觉得我需要实现IEnumerable上我的课之一,以允许它在foreach循环使用.我这样做后这样做:
class FData: IEnumerable<EClass> {
// Removed the irrelevant contents
public IEnumerator<EnumerableClass> GetEnumerator()
{
for (int i = 0; i < Emp_Id.Count; i++) {
yield return (new EnumerableClass { Emp_Id = Emp_Id[i], Salary = Salary[i] });
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
Run Code Online (Sandbox Code Playgroud)
我看到我的FData类可以像List一样使用,就好像它继承了所有可枚举的方法,如Select(),All(),Any()等.
我想知道如果在接口中没有定义这些方法的行为,这究竟是怎么发生的?