我希望子类从其父级继承类级实例变量,但我似乎无法弄明白.基本上我正在寻找这样的功能:
class Alpha
class_instance_inheritable_accessor :foo #
@foo = [1, 2, 3]
end
class Beta < Alpha
@foo << 4
def self.bar
@foo
end
end
class Delta < Alpha
@foo << 5
def self.bar
@foo
end
end
class Gamma < Beta
@foo << 'a'
def self.bar
@foo
end
end
Run Code Online (Sandbox Code Playgroud)
然后我想要这样输出:
> Alpha.bar
# [1, 2, 3]
> Beta.bar
# [1, 2, 3, 4]
> Delta.bar
# [1, 2, 3, 5]
> Gamma.bar
# [1, 2, 3, 4, 'a']
Run Code Online (Sandbox Code Playgroud)
显然,这段代码不起作用.基本上我想为父类继承的类级实例变量定义一个默认值.对于子子类,子类中的更改将是默认值.我希望这一切都能在不改变影响其父母或兄弟姐妹的一个类的价值的情况下发生.Class_inheritable_accessor给出了我想要的行为......但是对于一个类变量.
我觉得我可能会问得太多.有任何想法吗?
我有一个叫做Ruby的类LibraryItem.我想将此类的每个实例与一组属性相关联.这个数组很长,看起来像
['title', 'authors', 'location', ...]
Run Code Online (Sandbox Code Playgroud)
请注意,这些属性实际上不应该是方法,只是一个属性列表LibraryItem.
接下来,我想创建一个LibraryItem被调用的子类,LibraryBook它具有包含所有属性的属性数组,LibraryItem但也包含更多属性.
最终,我会想的几个子类LibraryItem每一个都有自己的版本阵@attributes上,但每个加入LibraryItem的@attributes(如LibraryBook,LibraryDVD,LibraryMap,等).
所以,这是我的尝试:
class LibraryItem < Object
class << self; attr_accessor :attributes; end
@attributes = ['title', 'authors', 'location',]
end
class LibraryBook < LibraryItem
@attributes.push('ISBN', 'pages')
end
Run Code Online (Sandbox Code Playgroud)
这不起作用.我收到了错误
undefined method `push' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
如果要工作,我会想要这样的东西
puts LibraryItem.attributes
puts LibraryBook.attributes
Run Code Online (Sandbox Code Playgroud)
输出
['title', 'authors', 'location']
['title', 'authors', 'location', 'ISBN', 'pages']
Run Code Online (Sandbox Code Playgroud)
(2010年5月2日添加)对此的一个解决方案是创建@attributes一个简单的实例变量,然后LibraryBoot …
如何使用rspec测试我的邮件程序中是否设置了某个实例变量?分配回来未定义..
require File.dirname(__FILE__) + '/../../spec_helper'
describe UserMailer do
it "should send the member user password to a User" do
user = FG.create :user
user.create_reset_code
mail = UserMailer.reset_notification(user).deliver
ActionMailer::Base.deliveries.size.should == 1
user.login.should be_present
assigns[:person].should == user
assigns(:person).should == user #both assigns types fail
end
end
Run Code Online (Sandbox Code Playgroud)
返回的错误是:
undefined local variable or method `assigns' for #<RSpec::Core::ExampleGroup::Nested_1:0x007fe2b88e2928>
Run Code Online (Sandbox Code Playgroud) 我正在关注Michael Hartl的RoR教程,它涵盖了密码加密的基础知识.这是目前的用户模型:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email,: password, :password_confirmation
email_regex = /^[A-Za-z0-9._+-]+@[A-Za-z0-9._-]+\.[A-Za-z0-9._-]+[A-Za-z]$/
#tests for valid email addresses.
validates :name, :presence => true,
:length => {:maximum => 50}
validates :email, :presence => true,
:format => {:with => email_regex},
:uniqueness => {:case_sensitive => false}
validates :password, :presence => true,
:length => {:maximum => 20, :minimum => 6},
:confirmation => true
before_save :encrypt_password
private
def encrypt_password
self.encrypted_password = encrypt(password)
end
def encrypt(string)
string
end
end
Run Code Online (Sandbox Code Playgroud)
我发布了一个关于before_save不工作的上一个问题,事实证明我不小心做的是将我的encrypt_password写成:
def …Run Code Online (Sandbox Code Playgroud) 假设我们在C++中有一个结构:
struct foobar
{
int age;
bool hot;
String name
};
Run Code Online (Sandbox Code Playgroud)
有没有办法以编程方式查询上面的结构来提取其实例成员?例如:
String[] members = magicClass.getInstanceMembers(foobar);
Run Code Online (Sandbox Code Playgroud)
成员会有["age", "hot", "name"]它的价值观.
可能?我问的原因是因为我的结构随着时间的推移发生了变化(添加/删除了变量).我希望能够使用此保存的数据创建自动生成Lua文件.
谢谢
我正在开发构建iOS 6应用程序.
我有一个类TDBeam,它继承自超类TDWeapon.
超类TDWeapon在TDWeapon.h文件中声明了@property:
@interface TDWeapon : UIView
@property (nonatomic) int damage;
@end
Run Code Online (Sandbox Code Playgroud)
我没有明确地@synthesize属性,因为我让Xcode自动这样做.
在子类TDBeam中,我覆盖了TDBeam.m文件中的getter:
#import "TDBeam.h"
@implementation TDBeam
- (int)damage {
return _damage;
}
@end
Run Code Online (Sandbox Code Playgroud)
Xcode按预期自动完成getter方法名称.但是当我尝试引用_damage实例变量(继承自超类)时,我收到编译器错误:
Use of undeclared identifier '_damage'
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?我已经尝试显式添加@synthesize,并更改_damage ivar的名称,但编译器不会"看到"它或来自超类的任何其他ivars.我认为ivars是可见的,可以从子类访问?
有人可以解释一个类如何访问其超类的实例变量以及它不是如何继承?我在谈论'Ruby Programming Language'和示例
class Point
def initialize(x,y) # Initialize method
@x,@y = x, y # Sets initial values for instance variables
end
end
class Point3D < Point
def initialize(x,y,z)
super(x,y)
@z = z
end
def to_s
"(#@x, #@y, #@z)" # Variables @x and @y inherited?
end
end
Point3D.new(1,2,3).to_s => "(1, 2, 3)"
Run Code Online (Sandbox Code Playgroud)
如果不继承类,那么如何Point3D访问类x和y内部to_s?这本书说:
"它们有时似乎是继承的原因是实例变量是由首先为它们赋值的方法创建的,而这些方法通常是继承或链接的."
但我无法弄清楚它的真正含义.
在Obj-C中,属性可以配置为弱/强.实例变量.喜欢以下 -
@interface MyClass {
NSObject *a;
}
Run Code Online (Sandbox Code Playgroud)
MyClass的对象是保持弱引用a还是强引用或其他什么?我认为iVar在其对象发布之前不会发布.为什么我们不为iVar属性指定弱/强?
我对以下代码有点困惑:
public class Test{
int x = giveH();
int h = 29;
public int giveH(){
return h;
}
public static void main(String args[])
{
Test t = new Test();
System.out.print(t.x + " ");
System.out.print(t.h);
}
}
Run Code Online (Sandbox Code Playgroud)
这里的输出是0 29,但我认为这必须是一个编译器错误,因为当涉及到该方法时,变量h应该尚未初始化giveH().那么,编译是从上到下进行的?这为什么有效?为什么值为x0而不是29?
我想做以下事情:
我想声明迭代字典的类的实例变量.
我们假设我有这个哈希值
hash = {"key1" => "value1","key2" => "value2","key3" => "value3"}
Run Code Online (Sandbox Code Playgroud)
我希望将每个键作为类的实例变量.我想知道我是否可以声明迭代遍历该哈希的变量.像这样的东西:
class MyClass
def initialize()
hash = {"key1" => "value1","key2" => "value2","key3" => "value3"}
hash.each do |k,v|
@k = v
end
end
end
Run Code Online (Sandbox Code Playgroud)
我知道这不起作用!我只是把这段代码放在一边看看你能不能理解我想要的更清楚.
谢谢!
ruby ×5
class ×2
inheritance ×2
ios ×2
objective-c ×2
c ×1
c++ ×1
compilation ×1
hash ×1
iterator ×1
java ×1
oop ×1
properties ×1
rspec ×1
scope ×1
self ×1