标签: nested-class

在 asInstanceOf 中引用嵌套类

我正进入(状态 not found: value Duck

    class Type
    class Value(val t: Type)
    class Duck extends Type {
        class Val extends Value(this)
    }
    def f(individual: Value) = individual.t match {
        // case t: Duck => individual.asInstanceOf[Value] //this is ok
         case t: Duck => individual.asInstanceOf[Duck.Val] //but I need this
    }
Run Code Online (Sandbox Code Playgroud)

在这里添加一些细节以提高问题质量。正式的质量检查不会出错。如果更多的字母改善了你的问题,那就一定是这种情况。现在,我的问题好多了,可以发布了。

casting scala nested-class inner-classes

4
推荐指数
1
解决办法
124
查看次数

如何访问封闭类的实例?

我的问题,抽象地说,是:

非静态内部类如何访问封闭类的实例?为了将实例用作方法调用中的参数以及访问内部类和封闭类之间具有相同名称的方法和变量,这是必需的。

像关键字一样thissuper允许您访问类和父类中具有相同名称的方法和变量的特定版本,是否有关键字来访问封闭类和封闭类中的方法和变量的版本?

如果您感到困惑,请继续阅读:

考虑以下代码示例,其中包含两个类和一个内部类。Main 和 Outer 是“myPackage”包中的两个类(附带说明,由于未知原因,我无法在没有包/默认包的情况下使以下代码工作)。Inner 是一个内部的、非静态的 Outer 类。

package myPackage;

public class Outer {
    public void activate() {
        System.out.println("Outer.activate");
    }
    
    public class Inner {
        public void go() {
            activate();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
package myPackage;

import myPackage.Outer.Inner;

public class Main {

    public static void main(String[] args) {
        Outer myOuter = new Outer();
        Inner myInner = myOuter.new Inner();
        myInner.go();
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,我构造了一个Innerwith myOuter.new Inner()。由于 Inner 是非静态的,它必须在其封闭类的现有实例之上构造:在这种情况下,myOuter. 因此,当我调用myInner.go(),myInner …

java nested-class

4
推荐指数
1
解决办法
1066
查看次数

模板,嵌套类和"预期构造函数,析构函数或'''令牌之前的转换"

在使用一些模板并使用迭代器编写自己的基本容器类时,我发现自己需要将成员函数体从模板类移动到单独的文件中以符合样式指南.但是,我遇到了一个有趣的编译错误:

runtimearray.cpp:17:错误:'&'令牌之前的构造函数,析构函数或类型转换runtimearray.cpp:24:错误:'&'令牌之前的构造函数,析构函数或类型转换runtimearray.cpp:32:错误: '&'标记之前的预期构造函数,析构函数或类型转换runtimearray.cpp:39:错误:'&'标记之前的构造函数,析构函数或类型转换runtimearray.cpp:85:错误:预期的构造函数,析构函数或类型转换在"RuntimeArray"之前runtimearray.cpp:91:错误:在'RuntimeArray'之前的预期构造函数,析构函数或类型转换

runtimearray.h:

#ifndef RUNTIMEARRAY_H_
#define RUNTIMEARRAY_H_

template<typename T>
class RuntimeArray
{
 public:
  class Iterator
  {
    friend class RuntimeArray;
   public:
    Iterator(const Iterator& other);

    T& operator*();
    Iterator& operator++();
    Iterator& operator++(int);
    Iterator& operator--();
    Iterator& operator--(int);
    bool operator==(Iterator other);
    bool operator!=(Iterator other);

   private:
    Iterator(T* location);

    T* value_;
  };

  RuntimeArray(int size);
  ~RuntimeArray();

  T& operator[](int index);

  Iterator Begin();
  Iterator End();

 private:
  int size_;
  T* contents_;
};

#endif  // RUNTIMEARRAY_H_
Run Code Online (Sandbox Code Playgroud)

runtimearray.cpp:

#include "runtimearray.h"

template<typename T>
RuntimeArray<T>::Iterator::Iterator(const Iterator& other)
    : value_(other.value_)
{
}

template<typename T>
T& …
Run Code Online (Sandbox Code Playgroud)

c++ templates nested-class

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

VBScript嵌套类数据结构

我的问题可以在下面的示例代码中说明,该代码设置了一个朋友的数据数组,每个朋友都可以有几个电话号码:

Class clsPhoneNo
  Dim strType
  Dim strNumber
End Class

Class clsPerson
  Dim strName
  Dim aclsPhoneNo()
End Class

Dim clsFriends()
ReDim clsFriends(3)
Set clsFriend(0) = New Person
clsFriend(0).strName = "Fred"
Set clsFriends(0).aclsPhoneNo(0) = New clsPhoneNo
ReDim clsFriend(0).aclsPhoneNo(2)
Set clsFriend(0).aclsPhoneNo(0).strType = "Home"
Set clsFriend(0).aclsPhoneNo(0) = "01234567890"
Set clsFriend(0).aclsPhoneNo(1).strType = "Work"
Set clsFriend(0).aclsPhoneNo(1) = "09876543210"
Run Code Online (Sandbox Code Playgroud)

但是,VBScript说

Microsoft VBScript compilation error: Expected end of statement
Run Code Online (Sandbox Code Playgroud)

之前 .在第二个ReDim声明中

我需要有aclsPhoneNo元素变量长度,因为我的代码实际上并不是地址簿,但这是一个演示问题的简单示例.

有任何想法吗?

arrays vbscript class nested-class

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

映射到嵌套类

我的应用程序运行时收到以下错误:

System.InvalidOperationException:未映射"ContactModels + Contact"类型.使用Ignore方法或NotMappedAttribute数据批注检查未明确排除类型.验证类型是否已定义为类,不是原始类,嵌套类还是通用类,并且不从EntityObject继承.

当我的DBContext类尝试初始化实体时失败:

public class DB : DbContext
{
    public DbSet<ContactModels.Contact> Contacts { get; set; }
    ....
}
Run Code Online (Sandbox Code Playgroud)

Contact模型如下:

public class ContactModels
{
    public class Contact
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

连接字符串:

<add name="DB" connectionString="Data Source=XXXXX;Initial Catalog=XXXX;Trusted_Connection=True"
  providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)

我得到的错误是数据库是否存在,或者即使它不存在,我也将其初始化:

protected void Application_Start()
{
    Database.SetInitializer(new CreateDatabaseIfNotExists<Models.DB>());
    ....
}
Run Code Online (Sandbox Code Playgroud)

这是我第一次使用EF,我已经遵循了一些教程,但我使用的是SQL Server 2008 R2,并且更愿意自己创建数据库,而不是让EF为我创建它.虽然在这一点上我会采取任何一个,如果它的工作.

.net c# sql-server nested-class entity-framework-4.1

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

在类中嵌套自定义异常类?(蟒蛇)

我想在我自己的类中嵌套一个Exception子类,如下所示:

class Foo(object):

    def bar(self):
        #does something that raises MyException

    class MyException(Exception):
        pass
Run Code Online (Sandbox Code Playgroud)

这样,我只需要从另一个模块调用bar()时导入Foo(而不是MyException).但是我在下面的内容不起作用:

from foo_module import Foo

foo = Foo()

try:
    foo.bar()
except Foo.MyException as e:
    print e
Run Code Online (Sandbox Code Playgroud)

Python给出了这个错误:

类型对象'Foo'没有属性'MyException'

有没有办法做到这一点?

python scope nested class nested-class

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

使用TabActivity的BadTokenException ActivityGroup

我需要在我的应用程序中实现TabHost中的嵌套活动.我使用这样的ActivityGroup类:

  public class CustomActivityGroup extends ActivityGroup {

View rootView;


public static CustomActivityGroup group;


private ArrayList<View> history;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.history = new ArrayList<View>(); group = this;



      View view = getLocalActivityManager().startActivity("LoginActivity", new Intent(this,RegisterActivity.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
      .getDecorView();
      replaceView(view);


}

@Override
protected void onResume() {

    super.onResume();
    this.history = new ArrayList<View>();
    group = this;

}

public void replaceView(View v) {

    if (history.size() == 0) {
        if (rootView != null) {
            history.add(rootView);
            rootView = null;
        }
    }
    history.add(v);  
    setContentView(v);
}

public void …
Run Code Online (Sandbox Code Playgroud)

android nested-class fragment tabactivity activitygroup

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

两级嵌套的c ++类与GCC一起使用但与Clang失败

我在定义声明它的类之外的内部类时遇到了问题.

struct Base {
    struct A {
        struct B;
    };
    struct A::B {
    };
};
Run Code Online (Sandbox Code Playgroud)

它编译并与GCC一起工作但在Clang上失败并出现此错误:

innerclass.cpp:6:12: error: non-friend class member 'B' cannot have a qualified name
    struct A::B {
           ~~~^
Run Code Online (Sandbox Code Playgroud)

如果省略最外面的类Base,则代码适用于Clang.

以这种方式定义内部类是不合法的吗?如果是这样,应该怎么做?

平台:
OS X 10.8.3
XCode 4.6.2
Clang Apple LLVM版本4.2(clang-425.0.24)(基于LLVM 3.2svn)
GCC gcc版本4.2.1(基于Apple Inc. build 5658)(LLVM build 2336.11. 00)

c++ macos clang nested-class

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

为什么我需要使用关键字"this"来调用我的封闭方法或变量

我有一个疑问,如果我有一个非静态嵌套类,为什么我需要使用关键字"this"来调用我的封闭类的方法或变量?我认为如下:如果非静态嵌套类可以访问其封闭类的方法和变量,并且非静态嵌套对象实例已经与其封闭对象实例相关联,为什么我需要使用"this"?例如,我有以下代码:

public class ClassA {

    public class ClassB {

        public void bye() {
            ClassA.this.hello();
            // Why not just ClassA.hello()?
        }
    }

    public void hello() {

    }
}
Run Code Online (Sandbox Code Playgroud)

如果从我的封闭类的方法我想调用我的一个非静态类的方法我该怎么办?例如,如果从我的方法hello()我想调用bye()我应该如何键入?

java nested-class

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

Java - 能够访问静态方法的实例

我刚刚开始使用Java,正在调查这个Nested Classes主题,并尝试了一些东西,突然间,这发生了:

class Encloser
{
  static int i;

  static void m1()
  {
    System.out.println(i);
  }

  static void m2()
  {
    Enclosee.accessEncloser();
  }

  static class Enclosee
  {
    static void accessEncloser()
    {
      i = 1;
      m1();
    }

    static void accessEncloserNew()
    {
      m2();
    }
  }
}

class EncloserTest
{

  public static void main(String[] args)
  {
    Encloser ee = new Encloser();
    Encloser.Enclosee e = new Encloser.Enclosee();
    ee.m1();
    ee.m2();
    e.accessEncloser();
    e.accessEncloserNew();Encloser.m1();
    Encloser.m2();
    Encloser.m1();
    Encloser.Enclosee.accessEncloserNew();
    Encloser.Enclosee.accessEncloser();
  }

}
Run Code Online (Sandbox Code Playgroud)

运行上面的代码不会给出任何错误/异常.它只是运行.这里的困惑是,实例如何能够在Static Methods这里调用?不Static Methods喜欢Class …

java static instance-variables nested-class

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