标签: instance-variables

财产拒绝合成

我一直在浏览这里的截屏视频,学习如何编写基于表格的iPhone应用程序,到目前为止它一直很顺利.现在我正处于第三集的中途,它开始遇到障碍.

为了删除表的顶层的临时硬编码,本教程NSMutableDicitonary为所有条目及其数据创建一个,然后NSArray使用该forKeys语句创建一个数组,只需要在表格单元格中显示的词条.

我遇到的问题是数组的变量拒绝合成.

违规变量在AppDelegate.h文件中定义,其余属性如下:

@property (readonly) NSArray *recipes;
Run Code Online (Sandbox Code Playgroud)

然后在AppDelegate.m文件中合成并实现如下:

@synthesize recipes;

- (NSArray *)recipes {
    return [data allKeys];
}
Run Code Online (Sandbox Code Playgroud)

我询问了截屏视频的作者,他建议如下AppDelegate.h:

@class Foo :NSObject {
    NSArray *_recipes;
}

@property(nonatomic, retain)NSArray *recipes;

@end
Run Code Online (Sandbox Code Playgroud)

这个用于AppDelegate.m:

@implementation Foo

@synthesize recipes = _recipes;

@end
Run Code Online (Sandbox Code Playgroud)

我试过这个方法,但它创造了比以前更多的错误.是什么让这个变量定义与任何其他@property不同,我怎样才能使它表现出来?

cocoa properties objective-c instance-variables

0
推荐指数
1
解决办法
274
查看次数

如何从form.cs文件调用变量到program.cs文件

我无法访问我的两个变量.我在互联网上看了一下,发现我需要使用类似的东西form.dlg.selectedpath来调用它,但我得到了三个错误.有人说form.dlg无法访问,下一个说需要对象引用.我也尝试访问另一个,并说表单不包含dlg2的定义.

这是我想要变量的代码.

var di = new DirectoryInfo(Form1.dlg.SelectedPath);  
           di.CopyTo(Form1.dlg2.SelectedPath, true);
Run Code Online (Sandbox Code Playgroud)

这是我的代码我正在设置一个变量

 public partial class Form1 : Form    
    {  
        FolderBrowserDialog dlg = new FolderBrowserDialog();


        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            if (dlg.ShowDialog() == DialogResult.OK)
Run Code Online (Sandbox Code Playgroud)

第二个变量从这里引用.

private void button1_Click(object sender, EventArgs e)  
        {  
            FolderBrowserDialog dlg2 = new FolderBrowserDialog();  
            if (dlg2.ShowDialog() == DialogResult.OK)  
            //do whatever with dlg.SelectedPath  
            {  
                backgroundWorker1.RunWorkerAsync(dlg2.SelectedPath);  
            }  
        }  
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

.net c# variables instance-variables

0
推荐指数
1
解决办法
2097
查看次数

术语:实例变量的单字Java术语?

我正在寻找在类(非静态)中声明的实例变量的替代OO/Java术语,或者更具体地说,在使用JPA注释"装饰"的Java类中:

@Entity
@Table(name = "Departments")
@IdClass(value = DepartmentId.class)
public class Department implements Serializable
{
    @Id
    @Column(name = "company_id", insertable = false, updatable = false)
    private Integer companyId;

    @Id
    @Column(name = "internal_code")
    private String internalCode;

    @Column(name = "name")
    private String name;

    @ManyToOne
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private Company company;

    ...
}
Run Code Online (Sandbox Code Playgroud)

他们被称为财产吗?属性?会员?场?只是参考?除了实例变量之外什么都没有?

如果有的话,我想听一个单词的用语."实例变量"对于我目前正在做的事情来说太长了.

java jpa terminology instance-variables javabeans

0
推荐指数
1
解决办法
276
查看次数

我何时在Java中使用类变量与实例变量?

这是我定义的泛型类,我想知道的是当我使用类变量创建更具体的类(例如CAR类)时?我个人对类变量的理解是,在类中声明的类变量的单个副本将使用关键字static声明,并且已从类中实例化的每个对象将包含该类的单个副本变量.

实例变量允许从类创建的类/对象的每个实例每个对象都有一个实例变量的单独副本?

因此,实例变量对于定义类/数据类型的属性很有用,例如House会有一个位置,但现在我何时在House对象中使用类变量?或者换句话说,在设计类时正确使用类对象是什么?

public class InstanceVaribale {
public int id; //Instance Variable: each object of this class will have a seperate copy of this variable that will exist during the life cycle of the object.
static int count = 0; //Class Variable: each object of this class will contain a single copy of this variable which has the same value unless mutated during the lifecycle of the objects.

InstanceVaribale() {
    count++;

}
public static void main(String[] args) {

    InstanceVaribale A …
Run Code Online (Sandbox Code Playgroud)

java oop instance-variables class-variables

0
推荐指数
1
解决办法
3580
查看次数

在另一个方法中使用viewDidLoad中创建的NSString变量

在我的viewDidLoad方法中,我设置以下变量:

// Get requested URL and set to variable currentURL
NSString *currentURL = self.URL.absoluteString;
//NSString *currentURL = mainWebView.request.URL.absoluteString;
NSLog(@"Current url:%@", currentURL);

//Get PDF file name
NSArray *urlArray = [currentURL componentsSeparatedByString:@"/"];
NSString *fullDocumentName = [urlArray lastObject];
NSLog(@"Full doc name:%@", fullDocumentName);

//Get PDF file name without ".pdf"
NSArray *docName = [fullDocumentName componentsSeparatedByString:@"."];
NSString *pdfName = [docName objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

我希望能够在另一个方法中使用这些变量(即- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {)

如何在viewDidLoad方法之外重用这些变量?我是新手......帮助会非常感激

variables objective-c instance-variables nsstring ios

0
推荐指数
1
解决办法
1297
查看次数

Java - 访问私有实例变量

我需要从以下类列表(Species.java)访问私有变量,以便在KlingonOx.java类中使用它们.

KlingonOx.java类的目的是确定大象物种的数量将在多少年后大于克林贡牛种的数量.

这是Species.java类:

import java.util.Scanner;

public class Species
{
private String name;
private int population;
private double growthRate;

public void readInput()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("What is the species' name?");
    name = keyboard.nextLine();

    System.out.println("What is the population of the species?");
    population = keyboard.nextInt();

    while(population < 0)
    {
        System.out.println("Population cannot be negative.");
        System.out.println("Reenter population:");
        population = keyboard.nextInt();
    }

    System.out.println("Enter growth rate (% increase per year):");
    growthRate = keyboard.nextDouble();
}

public void writeOutput()
{
    System.out.println("Name = " + name);
    System.out.println("Population …
Run Code Online (Sandbox Code Playgroud)

java instance-variables private-members

0
推荐指数
1
解决办法
3万
查看次数

初始化结束后类属性丢失值

有人可以帮我理解为什么class属性会丢失initialize方法之外的值吗?

2.0.0-p0 :031 > $arr = [1, 2, 3, 4]
 => [1, 2, 3, 4] 
2.0.0-p0 :032 > class Class1
2.0.0-p0 :033?>   def initialize
2.0.0-p0 :034?>     val1 = $arr[0]
2.0.0-p0 :035?>     puts val1
2.0.0-p0 :036?>     end 
2.0.0-p0 :037?>   end 
=> nil 
2.0.0-p0 :038 > cl1 = Class1.new
1   
=> #<Class1:0x007fe8ac16be70> 
2.0.0-p0 :039 > puts cl1.val1

=> nil 
2.0.0-p0 :040 > 
Run Code Online (Sandbox Code Playgroud)

ruby scope instance-variables

0
推荐指数
1
解决办法
289
查看次数

为什么Ruby类允许通过方法返回来访问实例变量?

我有一个同事的Ruby代码:

class Leaky

   def initialize
      @internalOnly = [[1, 2, 3], [3, 4, 5]]
   end

   def foo
      if 5 > 4
         temp = @internalOnly
      end
   end

   def printInternal
      puts " here is my @internalOnly: #{@internalOnly}"
   end

end

a = Leaky.new
a.printInternal

b = a.foo  # 1) Gets Leaky:@internal!
b[1] = 666 # 2) Modifies it!

a.printInternal
Run Code Online (Sandbox Code Playgroud)

它产生这个输出:

 here is my @internalOnly: [[1, 2, 3], [3, 4, 5]]
 here is my @internalOnly: [[1, 2, 3], 666]
Run Code Online (Sandbox Code Playgroud)

在语句中# 1),Ruby显然返回了对Leaky实例变量的引用 …

ruby encapsulation reference instance-variables

0
推荐指数
1
解决办法
112
查看次数

rails何时使用|| =设置实例变量

我正在阅读一篇文章,并遇到了第一个示例代码.在模型中,设置实例变量以避免不必要的查询.我也在其中一个railscast中看到了这个(第二个例子).另一方面,我在更多的文章中读到,如果我使用这种模式,那么我的应用可能不会是线程安全的,所以我不能利用我的puma网络服务器.

可以告诉我何时/何地应该使用这种模式?

第一个例子:

def first_order_date
  if first_order
    first_order.created_at.to_date
  else
    NullDate.new 'orders'
  end
end

private

def first_order
  @first_order ||= orders.ascend_by_created_at.first
end
Run Code Online (Sandbox Code Playgroud)

第二个例子

def shipping_price
  if total_weight == 0
    0.00
  elsif total_weight <= 3
    8.00
  elsif total_weight <= 5
    10.00
  else
    12.00
  end
end

def total_weight
  @total_weight ||= line_items.to_a.sum(&:weight)
end
Run Code Online (Sandbox Code Playgroud)

更新的问题

第一个例子

正如我所看到的那样,'first_order_date'总是在一个对象上调用(https://robots.thoughtbot.com/rails-refactoring-example-introduce-null-object),所以我并不完全看到额外的查询是如何避免.我确定我错了,但根据我的知识,它可能只是

def first_order_date
  if orders.ascend_by_created_at.first
    first_order.created_at.to_date
  else
    NullDate.new 'orders'
  end
end
Run Code Online (Sandbox Code Playgroud)

或者也可以使用@first_order其他地方?

第二个例子

原始问题中的代码与此不相同?

def shipping_price
  total_weight = line_items.to_a.sum(&:weight)
  if total_weight == 0
    0.00
  elsif total_weight …
Run Code Online (Sandbox Code Playgroud)

ruby conditional ruby-on-rails instance-variables

0
推荐指数
1
解决办法
782
查看次数

父类可以看到子类受保护的变量吗?

我对继承如何运作非常模糊,只是想确保我正在朝着正确的方向前进.

根据我的理解,我理解这一点

  1. 所有包都可以访问公共变量,
  2. 单个包内的默认值,
  3. 私人只在课堂上,
  4. 并用子类保护.

我知道子类可以看到父类的受保护变量.

我的问题:它是否相反?

java class protected object instance-variables

0
推荐指数
1
解决办法
220
查看次数