我可能做了一些愚蠢的事情,但我无法弄清楚它是什么.
我从这个程序看到的输出是
foo
test
Run Code Online (Sandbox Code Playgroud)
我期待看到的是
foo
abc
test
Run Code Online (Sandbox Code Playgroud)
有没有人在这里看到任何明显的错误
class Foo
def initialize(l)
@label = l
end
def label
@label
end
def abc
@abc
end
def abc=(abc)
@abc
end
end
foo = Foo.new("foo")
foo.abc=("abc")
puts foo.label
puts foo.abc
puts "test"
Run Code Online (Sandbox Code Playgroud) 我很确定我只是在这里忽略了这一点而感到困惑.任何人都可以告诉我如何为一个对象编写一个简单的描述,该对象将打印出其实例变量的值到控制台.
另外:无论如何都要把信息作为一个块来呈现(例如,如果你有10个iVars,它将会让所有人逐一返回)
@interface CelestialBody : NSObject {
NSString *bodyName;
int bodyMass;
}
- (NSString *)description {
return (@"Name: %@ Mass: %d", bodyName, bodyMass);
}
Run Code Online (Sandbox Code Playgroud)
干杯 - 加里 -
我正在尝试编写此代码以获取第一个initialCapacity素数,然后使用java按顺序打印它们.由于两个原因,它无法正常工作,首先我得到了错误
41:无法从静态上下文引用非静态变量listOfPrimeNumbers
当我尝试运行程序时,即使我将变量更改为静态并运行程序,它也只会打印出"1".所以它只是在构造函数Primes中迭代while循环一次,然后停止,无论我看起来多么努力,我都无法找到问题!有人能帮助我,即使你可以快速看看并告诉我可能出错的地方,我真的很感激.
另外,与非静态和静态变量和方法有关的故事是什么?使用这些时的最佳做法是什么?如果有人可以将我链接到描述这个的页面(我已经google了无效!)我很想读:)
非常感谢你们!
import java.util.*;
public class Primes {
private ArrayList<Integer> listOfPrimeNumbers;
public static void main(String[] args) {
ArrayList<Integer> listOfPrimeNumbers;
Primes generator=new Primes(50);
print();
}
public Primes( int initialCapacity) {
listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity);
int index=0;
int counter=0;
while (counter != initialCapacity ) {
if (isPrime(index)) {
listOfPrimeNumbers.add(index);
counter++;
System.out.println(counter);
index++;
}
else {
index++;
}
}
}
public boolean isPrime(int candidateNo) {
Iterator<Integer> iter = listOfPrimeNumbers.iterator( );
//in here ! ?
int i=2;
while ( iter.hasNext( ) …Run Code Online (Sandbox Code Playgroud) Flash告诉我实例名称必须包含"字母数字字符,支持的符号,没有空格".我发现下划线是允许的,但不是减号.
有没有人有一个所有支持的符号的确定列表?
对于下面的代码,我创建了一个实例变量,其类名作为返回类型
class classtype{
static classtype x;
public static void main(String...a){
System.out.println(x);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码输出到null指示这个具有类名作为返回类型的实例变量保持字符串类型值,显然但是当我尝试初始化它时
static classtype x="1";
Run Code Online (Sandbox Code Playgroud)
它给出了类型不匹配错误 java.Lang.String
如果有人能解释,请
我不明白使用构造函数Rational()创建Rational对象时会发生什么.我的书说它将创建一个Rational对象,其值为0但内部存储为0/1.这个(0)如何存储为0/1?不是num和den 0的实例变量的默认值吗?
public class Rational{
public Rational(){
this(0);
}
public Rational(int n){
this(n,1);
}
public Rational(int x, int y){
num = x;
den = y;
}
private int num;
private int den;
}
Run Code Online (Sandbox Code Playgroud) 我刚开始学习Java ,它很棒.有一件事我需要理解,在类中我们可以通过两种方式访问实例变量:
class Box {
// Instance variables
private int width;
private int height;
private int depth;
// First way
public void set_volume(int a, int b, int c) {
this.width = a;
this.height = b;
this.depth = c;
}
// Second way
public void set_volume_v2(int a, int b, int c) {
width = a;
height = b;
depth = c;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,Instance变量可以在没有this关键字的情况下访问.那么最好的方法是什么?或者它们之间有什么区别?
我是Ruby on Rails的新手,并且已经坚持了这个bug很长一段时间了.我希望有人能给我一些有用的信息来解决这个bug.我有一种感觉,我忽略了一些微不足道的事情.无论如何,我已经在我的代码中包含了绘制错误(在视图中)以及我的控制器中的代码.虽然我在发布之前完成了我的研究,但我可能不得不将我的实例变量放在我的控制器中 - 但是,我不确定该过程如何进行,然后如何从我的View中调用它.我将不胜感激任何帮助!提前致谢 :)
我得到的错误:
NameError in Search#indx
'@' is not allowed as an instance variable name
Run Code Online (Sandbox Code Playgroud)
这是我在我的视图(apps/views/search/index.html.erb)中绘制错误的代码行:
<% @search = instance_variable_get("\@#{params[:model].to_s.downcase.pluralize}")%>
Run Code Online (Sandbox Code Playgroud)
这是我的controller(apps/controllers/search_controller.rb)中的代码:
class SearchController < ApplicationController
def index
@containers = Container.search(params[:q])
@cpus = Cpu.search(params[:q])
@disks = Disk.search(params[:q])
@firmwares = Firmware.search(params[:q])
@hardwares = Hardware.search(params[:q])
@hosts = Host.search(params[:q])
@interfaces = Interface.search(params[:q])
@lans = Lan.search(params[:q])
@licenses = License.search(params[:q])
@rams = Memory.search(params[:q])
@networks = Network.search(params[:q])
@products = Product.search(params[:q])
@roles = Role.search(params[:q])
@sites = Site.search(params[:q])
@vmpools = Vmpool.search(params[:q]) …Run Code Online (Sandbox Code Playgroud) 我看到我的代码中一直出现这种情况
class Foo
def initialize(foo)
@foo = foo
end
#...
end
Run Code Online (Sandbox Code Playgroud)
这不是太糟糕,但它变得更糟:
class Foo
def initialize(foo,baz,bar,a,b,c,d)
@foo = foo
@baz = baz
@bar = bar
#etc...
Run Code Online (Sandbox Code Playgroud)
你可以通过做类似的事情来解决这个问题
@foo, @baz, @bar = foo, baz, bar
Run Code Online (Sandbox Code Playgroud)
但即使这样做感觉不对,也很烦人.有没有更好的方法根据参数定义实例变量?
编辑:这个问题似乎有2个不同的解决方案.看到:
我正在学习使用Python3"用Python学习Python"这本书.作者用这个例子介绍了OOP的概念:
class Song (object):
def __init__(self,lyrics):
self.lyrics = lyrics
def sing_me_a_song(self):
for line in self.lyrics:
print (line)
happy_bday = Song(["Happy birthday to you", "I don't want to get sued","So I'll stop right there"])
bulls_on_parade = Song(["They rally around the family","With pockets full of shells"])
happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()
Run Code Online (Sandbox Code Playgroud)
OOP对我来说很有趣.作者建议我们应该将代码"垃圾","破解"和"捶打"一下.
我试图打印对象是"实例化"的变量名称(不确定"instanced"是否是正确的单词,也许正确的单词是"实例化").为了尝试这一点,我在类Songs()上添加了以下方法:
class Song (object):
def __init__(self,lyrics):
self.lyrics = lyrics
def sing_me_a_song(self):
for line in self.lyrics:
print (line)
def name_of_var(self):
print (Song)
def name_of_var_2(self):
print (object)
def name_of_var_3(self):
print (self)
def name_of_var_3(self):
print (self) …Run Code Online (Sandbox Code Playgroud) java ×4
ruby ×3
oop ×2
static ×2
this ×2
flash ×1
objective-c ×1
primes ×1
python ×1
python-3.x ×1
return-type ×1
syntax ×1