考虑以下类:
public class Vehicle { ... }
public class Coverage { ... }
public class VehicleList : IEnumerable<Vehicle> { ... }
public class CoverageList : IEnumerable<Coverage> { ... }
public abstract class Quote
{
protected VehicleList vehicles;
protected CoverageList coverages;
internal Quote() { ... }
public IReadOnlyCollection<Vehicle> Vehicles
{
get { return this.vehicles.AsReadOnly(); }
}
public IReadOnlyCollection<Coverage> Coverages
{
get { return this.coverages.AsReadOnly(); }
}
...
}
public sealed class OhQuote : Quote
{
//needs to access protected fields
... …
Run Code Online (Sandbox Code Playgroud) 我试图弄清楚为什么在以下示例中需要强制转换:
bool test = new Random().NextDouble() >= 0.5;
short val = 5;
// Ex 1 - must cast 0 to short
short res = test ? 5 : 0; // fine
short res = test ? val : 0; // error
short res = test ? val : (short)0; // ugly
// Ex 2 - must cast either short or null to short?
short? nres = test ? val : null; // error
short? nres = test ? (short?)val : …
Run Code Online (Sandbox Code Playgroud) 假设我有两个模块:
module Test1
attr_accessor :a, :b
@a = 0.0
@b = 0.0
end
module Test2
attr_accessor :c, :d
@c = 0.0
@d = 0.0
end
Run Code Online (Sandbox Code Playgroud)
现在,我想有条件地将这些模块混合到一个类中.这就是我尝试过的:
require './Test1.rb'
require './Test2.rb'
class MyClass
def initialize(mode)
if mode == 0
(class << self; include Test1; end)
elsif mode == 1
(class << self; include Test2; end)
else
class << self
include Test1
include Test2
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我看到的行为:
obj = MyClass.new(0)
obj.a #=> nil
Run Code Online (Sandbox Code Playgroud)
也@a
就是nil
在类中的实例方法.我觉得我不理解这里重要的事情.我想了解为什么我所做的不起作用,以及实现我所需功能的正确方法是什么.
我正在编写一个程序,将四个XML文件中的数据加载到四个不同的数据结构中.它有这样的方法:
def loadFirst(year)
File.open("games_#{year}.xml",'r') do |f|
doc = REXML::Document.new f
...
end
end
def loadSecond(year)
File.open("teams_#{year}.xml",'r') do |f|
doc = REXML::Document.new f
...
end
end
etc...
Run Code Online (Sandbox Code Playgroud)
我最初只使用了一个线程并且一个接一个地加载了一个文件
def loadData(year)
time = Time.now
loadFirst(year)
loadSecond(year)
loadThird(year)
loadFourth(year)
puts Time.now - time
end
Run Code Online (Sandbox Code Playgroud)
然后我意识到我应该使用多个线程.我的期望是,从一个单独的线程上的每个文件加载将非常接近所有顺序执行它的速度的四倍(我有一个带有i7处理器的MacBook Pro):
def loadData(year)
time = Time.now
t1 = Thread.start{loadFirst(year)}
t2 = Thread.start{loadSecond(year)}
t3 = Thread.start{loadThird(year)}
loadFourth(year)
t1.join
t2.join
t3.join
puts Time.now - time
end
Run Code Online (Sandbox Code Playgroud)
我发现使用多个线程的版本实际上比另一个慢.这怎么可能?差异大约是20秒,每次大约需要2到3分钟.
线程之间没有共享资源.每个都打开一个不同的数据文件,并将数据加载到与其他数据不同的数据结构中.
虽然SO和其他地方有很多关于之前发生过的关系的帖子,但我很难找到对我的问题的确切答案.
考虑两个Java线程:
最初,flag == false
和data == 0
T1
data = 42;
synchronized(m) {
flag = true;
}
Run Code Online (Sandbox Code Playgroud)
T2
boolean f;
synchronized(m) {
f = flag;
}
if (f) {
int x = data;
}
Run Code Online (Sandbox Code Playgroud)
根据上面的代码,我相信f
可以分配价值,true
或者false
没有保证.它是否正确?
现在,如果将两个synchronized
语句更改为synchronized(flag)
,我认为指令flag = true
将始终在指令之前发生f = flag
,因此f
将始终为该指令赋值true
.它是否正确?
考虑以下类:
class A
{
public virtual string Name { get { return "A"; } }
}
class B : A
{
public override string Name { get { return "B"; } }
}
class C : A
{
public override string Name { get { return "C"; } }
}
Run Code Online (Sandbox Code Playgroud)
并且列表包含两个类型为B的对象和一个类型为C的对象:
List<A> l = new List<A>();
l.Add(new B());
l.Add(new C());
l.Add(new B());
Run Code Online (Sandbox Code Playgroud)
是否有某种方法可以在运行时强制检查类型,并仅在此列表中对类型B的对象进行迭代(缺少编写列表的自定义实现)?就像是:
foreach (B obj in l) // runtime error
Console.WriteLine(obj.Name);
// desired output:
// B
// B
Run Code Online (Sandbox Code Playgroud)
我没有在一个项目中遇到过这个问题,但这个想法刚刚发生在我身上,我很好奇是否可以做到这一点.我知道这个功能的需要可能表明存在设计缺陷.
编写您知道可能失败的代码并忽略异常,是不是风格和/或性能不好?例如(在C#中):
long l = 1;
try {
l = (long)castObject;
} catch(InvalidCastException e) { }
Run Code Online (Sandbox Code Playgroud)
在这种情况下,程序员并不特别关心是否InvalidCastException
抛出了一个.
c# ×4
ruby ×2
casting ×1
concurrency ×1
inheritance ×1
java ×1
mixins ×1
oop ×1
performance ×1
polymorphism ×1
protected ×1