请考虑以下代码:
class Dog
attr_accessor :name, :color
def initialize(name, color)
end
end
Run Code Online (Sandbox Code Playgroud)
在Ruby对象中,是直接访问实例变量(即@name = name)还是使用setter/getter方法(即name = name)的约定?
前者对我来说更清楚,但如果你实现自己的setter/getter方法(例如同时增加一个类变量),那么你最终必须使用两种方法(即@name = name ; color = color).
Ruby社区中的惯例是什么?我应该如何编写代码以便将其他人阅读?
我是Objective-C开发的新手,我无法弄清楚以下内容之间的区别:
首先让我解释一下我的情况.我有一个NSMutableArray,我在我的.h文件中创建并插入它.现在当我为它分配一个数组时
self.myMutableArray=myArray
Run Code Online (Sandbox Code Playgroud)
我收到一个错误; 然而就是
myMutableArray=myArray
Run Code Online (Sandbox Code Playgroud)
工作良好.
我对解决错误不感兴趣.我只是想知道放在self前面有什么区别?为什么我也可以使用变量而没有self带来什么限制?
在我的空闲时间里,我一直在学习Java,到目前为止,我对如何正确使用变量一无所知.我的意思是,我应该更喜欢使用实例变量,还是只使用单个方法中的变量.
例如,这是我写的一段代码:
public class arsenalTroop {
String[][] troopStats;
String[][] weaponStats;
String[][] armorStats;
String[][] animalStats;
String[] troops;
String[] weapon;
String[] armor;
String[] animal;
JLabel[] troopsArray;
int troopTotal;
int weaponTotal;
int armorTotal;
int lordTotal;
int animalTotal;
JFrame arsenalLordFrame = new JFrame();
JTextField name = new JTextField();
JLayeredPane lP = new JLayeredPane();
JLayeredPane fP = new JLayeredPane();
JLayeredPane ldP= new JLayeredPane();
JLabel siegeLabel = new JLabel();
JComboBox weaponCB;
JComboBox armorCB;
JComboBox animalCB;
JLabel siegeText = new JLabel();
JButton addWep = new JButton();
JButton …Run Code Online (Sandbox Code Playgroud) 我很好奇得到一个OOP问题的答案,但到目前为止找不到任何信息.
在这里,编写类和方法是为每个方法传递参数或使用实例/字段变量和$ this-> x更快;
哪个在运行时会更快?
class ExampleByParameter(){
function SomeMethod($a,$b){
echo $a." ".$b;
return;
}
}
Run Code Online (Sandbox Code Playgroud)
要么
class ExampleByInstance(){
function __construct($a,$b){
$this->a=$a;
$this->b=$b;
}
function SomeMehtod(){
$a=$this->a;
$b=$this->b;
echo $a." ".$b;
return;
}
}
Run Code Online (Sandbox Code Playgroud)
我想他们与上面的例子没什么区别,但我认为可能与更复杂的代码有显着差异.
更明确一点,当我在使用()创建对象时尝试访问实例变量时出现编译时错误,但是当我不这样做时,代码将按预期编译并运行.此外,此问题仅适用于默认构造函数.我想了解原因.
using namespace std;
#include <iostream>
class Student {
public:
int gpa;
Student() {
gpa = 4;
}
Student( int x ) {
gpa = x;
}
};
int main() {
Student zero;
Student sally( 2 );
Student jack();
cout << zero.gpa << endl; //prints 4
cout << sally.gpa << endl; // prints 2
cout << jack.gpa << endl; //error: request for member 'gpa' in 'jack', which is of non-class type 'Student()'
}
Run Code Online (Sandbox Code Playgroud) 我有这样的场景
public class Test{
static int a;
int b;
public static void main(String[] args){
Test t1 = new Test();
Test t2 = new Test();
}
}
Run Code Online (Sandbox Code Playgroud)
对象t1和对象t2中的变量是什么?
根据我的理解,因为变量a是静态变量,所以它将在对象1和对象2中.
而b将两者的对象被创建单独的副本.
但是,当我为变量b ie(int b=1)赋值并将其称为System.out.println(t1.b),System.out.println(t2.b)
我没有得到错误,而是从两个对象获得1作为输出.
这是为什么?
为什么控制器中的新动作需要初始化实例变量@article?我已经测试过,在新操作中没有实例变量的情况下,记录可以很好地保存到数据库中的表中。
class ArticlesController < ApplicationController
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
end
Run Code Online (Sandbox Code Playgroud) 这是我正在尝试做的一个例子:
class Parent():
def __init__():
self.parent_var = 'ABCD'
x = Child(self) # self would be passing this parent instance
class Child():
def __init__(<some code to pass parent>):
print(self.parent_var)
foo = Parent()
Run Code Online (Sandbox Code Playgroud)
现在我知道您在想什么,为什么不将parent_var 本身传递给子实例呢?我的实际实现在 Parent 中有超过 20 个类变量。我不想手动将每个变量传递给在 Parent 中实例化的 Child 实例的 __init__ - 有没有办法使所有 Parent 类变量可供 Child 使用?
编辑 - 已解决:这是我发现有效的方式:
class Parent():
def __init__(self):
self.parent_var = 'ABCD' # but there are 20+ class vars in this class, not just one
x = Child(self) # pass this parent instance to …Run Code Online (Sandbox Code Playgroud) 如果基于提供给静态方法的参数在静态方法中检索实例变量,如果不同的调用者在完全相同的时间调用静态方法,是否可以踩下实例变量?我调用的方法定义如下,我想知道实例变量发票是否可以被破坏...任何澄清将非常感谢!
public static void SendInvoiceReceipt(int invoiceId, string recipientEmailAddress)
{
var invoice = ObjectFactory.GetInvoiceDAL().GetInvoiceByInvoiceId(invoiceId);
var htmlBody = BuildHtmlInvoiceReceipt(invoice);
var txtBody = BuildTextInvoiceReceipt(invoice);
UtilitiesManager.Emails.EmailUtil.Send(SiteConfigUtilities.GetSMTPServer(),
"referral@realtors.net", recipientEmailAddress, String.Empty,
"Payment Receipt", htmlBody, txtBody);
}
Run Code Online (Sandbox Code Playgroud) 我想替换这个:
self.fajerImage = [UIImage imageNamed:@"FirstViewBG_5N.png"];
self.shrogImage = [UIImage imageNamed:@"FirstViewBG_4N.png"];
self.dohorImage = [UIImage imageNamed:@"FirstViewBG_3N.png"];
self.aaserImage = [UIImage imageNamed:@"FirstViewBG_2N.png"];
self.mgribImage = [UIImage imageNamed:@"FirstViewBG_1N.png"];
self.eeshaImage = [UIImage imageNamed:@"FirstViewBG_0N.png"];
Run Code Online (Sandbox Code Playgroud)
有一个for循环..我不知道如何在循环中一个接一个地调用ivars ..
请注意,在循环之前将它们放在一个数组中是个好主意,我没有成功实现..
谢谢!
class ×2
java ×2
objective-c ×2
static ×2
variables ×2
accessor ×1
c# ×1
c++ ×1
coding-style ×1
conventions ×1
for-loop ×1
inheritance ×1
nsarray ×1
oop ×1
parameters ×1
parent ×1
performance ×1
php ×1
python ×1
ruby ×1
setter ×1
swing ×1