常识表明,对于返回类型,子类型应该是协变的,而对于参数类型,则应该是反变体。因此,由于E.f的严格协变参数类型,应该拒绝以下内容
interface C {
f (o: C): void
}
interface D extends C {
g (): void // give D an extra service
}
class E implements C {
// implement f with a version which makes stronger assumptions
f (o: D): void {
o.g() // rely on
现在我已经开始掌握相反的情况了,尽管我正在试图找出什么是当一个接口可以被使用时的优势。很明显我漏掉了什么。
下面是c#4示例
class Dog : Animal {
public Dog(string name) : base(name) { }
}
class Animal {
string _name;
public Animal(string name) {
_name = name;
}
public void Walk() {
Co
据我所知,在C# 2.0中不可能做到以下几点
public class Father
{
public virtual Father SomePropertyName
{
get
{
return this;
}
}
}
public class Child : Father
{
public override Child SomePropertyName
{
get
{
return this;
}
我正试图更好地了解以下行为:
scala> class C[-A, +B <: A]
<console>:7: error: contravariant type A occurs in covariant position
in type >: Nothing <: A of type B
class C[-A, +B <: A]
^
然而,以下工作:
scala> class C[-A, +B <% A]
defined class C
我可以
我已经听说/阅读了很多关于C#中协方差问题的文章,我想提出几个问题&场景,希望我能澄清我在这个问题上的困惑。
在所有这些例子中,请假定始终定义了以下内容:
public class Apple : Fruit {}
我的第一个例子:
IList<Apple> apples = GetApples();
IList<Fruit> fruits = apples;
这应该能行,对吧?我在C#中测试了几次,它编译得很好&运行得很好(我对第一个示例的测试略高于此,因为我有多态调用,可以将内容打印到控制台)。
第二个例子:
IList<Apple> a
我很想知道为什么抽象基类中的接口实现不能满足子类中的要求。下面是一个例子:
public interface IBase { }
public interface IConcrete : IBase { }
public interface IBaseManager<out T>
where T : IBase
{
T Create();
IEnumerable<T> SelectAll();
}
public interface IConcreteManager : IBaseManager<IConcrete> { }
pu
这是第4.4讲的一个例子
class Array[+T] {
def update(x: T) = ???
}
这会导致repl中出现错误:
error: covariant type T occurs in contravariant position in type T of value x
在幻灯片上,马丁说这是一个“有问题的组合”。为什么会这样呢?
给定消耗T类型值的简单特性:
trait Consumer[-T] extends (T => Unit) {
def +[U, P](other: Consumer[U]): Consumer[P] = ???
}
如何实现将两个使用者组合为一个接受有效的+超级类型的AnyRef或更宽的AnyVal类型的AnyVal函数?就下列案件而言:
具有公共超级类型的-AnyRefs:
trait Base
trait A extends Base
trait B extends Base
val c1: Consumer[A] = _
val c2: Consumer[B] = _
va
我想知道是否有人可以向我解释一下geoR软件包是如何计算协方差函数的?我的意思是你会怎么手工做呢?
library(geoR)
#suppose I have the following coordinates
X = c(60,30,20,40)
Y = c(50,20,50,50)
my_coordinates = cbind(X,Y)
print(my_coordinates)
#computing covariance
my_cov= varcov.spatial(my_coordinates,cov.model="exp", cov.pars=c(0.2,25))
当涉及到接口时,我不清楚C#中的协方差的概念。严格基于我下面的例子,这是一个协方差的例子,请描述为什么或为什么不是。
class Program
{
static void Main()
{
ICarInterface car = new Car();
}
}
interface ICarInterface
{
void Start();
}
class Car : ICarInterface
{
public void Start()
{
}
}
我有类MyClass<T>,其中T是一些接口:
class MyClass<T> where T: IMyInterface
我编写了几个类,它们使用MyClass的一些实现来扩展IMyInterface,例如:
class MySecondClass : MyClass<MyInterfaceImplementation>
为什么不允许将MySecondClass实例分配给具有MyClass<IMyInterface>类型的变量?
MyClass<IMyInterface> x = new MySecondClass()
当我添加隐
下面的代码在C#中是不合法的,有什么合理的理由吗?
class X: IA, IB
{
public X test() // Compliation Error, saying that X is not IB
{
return this;
}
}
interface IA
{
IB test();
}
interface IB { };
对于下面的代码,我无法得到A、B和C的单个值的标准错误。我尝试了几种不同的方法,要么得到0,要么得到一个单数值。下面的方法是我尝试过的最新方法,但没有奏效。有人有什么建议吗?我是Python新手。
import numpy as np
import pandas as pd
import scipy
import pylab
import matplotlib
import matplotlib.pyplot as plt
import math
import sympy as sp
from scipy.optimize import curve_fit
x = np.array([600,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Variance
{
class A { }
class B : A { }
class C<out T> { }
class Program
{
static void Main(string[] args)
{
var v = new C<B>();
CA(v);
}
stati