所以也许这是一个愚蠢的问题,我在想这个,但我有以下情况.我正在制作一个"类Shell",它可以运行抽象的"类Action"对象.它是唯一应该创建或使用这些对象的类.操作对象需要访问Shell以对其执行特定操作,但我试图避免为此添加公共接口(不应该允许其他人这样做).
我原来有一个简单的(不那么优雅)
class Shell
{
public:
bool checkThing();
// etc...
private:
bool _thing;
};
class Action
{
public:
virtual void execute( Shell &s )=0;
};
class ChangeAction : public Action
{
public:
void execute( Shell &s )
{
// requires friendship or public mutator!
s._thing = true;
}
};
Run Code Online (Sandbox Code Playgroud)
所以我考虑了一个嵌套类Action,但是我想把它变成私有的(为什么让其他人做除了Shell之外的具体动作,对吧?)
class Shell
{
public:
bool checkThing();
// etc...
private:
bool _thing;
class Action;
};
class Shell::Action
{
public:
virtual void execute( Shell &s )=0;
};
class ChangeAction : public Shell::Action …Run Code Online (Sandbox Code Playgroud) 在弄乱嵌套类并将类型名称输出到控制台窗口时,我注意到了一些好奇.我想知道是否有人可以为我解释一下.当调用GetType()主类时,它返回我期望的内容,即相关名称空间后的类的名称.即Namespace.Namespace.Classname
但是,当我从封闭类中调用一个函数来返回嵌套类的类型时,我得到返回的值,如下所示:
Namespace.Namespace.ClassNameEnclosing + ClassNameNested.
Run Code Online (Sandbox Code Playgroud)
为什么它不是简单地以点符号返回.为什么+符号?我只是好奇在后台发生了什么导致这种表示法.
我有一些看起来像这样的代码:
namespace myLibrary
{
class A
{
public:
struct Nested
{
...
};
...
};
}
Run Code Online (Sandbox Code Playgroud)
在代码的其他部分,我需要访问A.因为我喜欢可读的代码,我也喜欢using指令:
using myLibrary::A;
...
A a;
Run Code Online (Sandbox Code Playgroud)
现在,在某些时候我还需要访问我的嵌套类,所以我想写这样的东西:
using myLibrary::A::Nested;
Run Code Online (Sandbox Code Playgroud)
显然,编译器不能知道这是一个嵌套类而不是类成员,并给我一个错误:
error : using declaration can not refer to class member
Run Code Online (Sandbox Code Playgroud)
我无法理解的是为什么这不能解决问题:
using typename myLibrary::A::Nested;
Run Code Online (Sandbox Code Playgroud)
编译器仍然给我完全相同的错误!
幸运的是,我有其他选择:
// Using a typedef
typedef myLibrary::A::Nested Nested;
// Using the new C++11 syntax for type aliasing
using Nested = myLibrary::A::Nested;
Run Code Online (Sandbox Code Playgroud)
但我想了解为什么使用typename指令不起作用.它不符合我的想法吗?或者它不是由编译器实现的?如果是后者,是否有理由呢?
在以下测试用例中:
class Package
class Component
def initialize
p [:initialize,self]
end
end
end
class Package_A < Package
end
class Package_B < Package
end
# Why are the following components of type Package and not Package_A and Package_B
component=Package_A::Component.new
p component
component=Package_B::Component.new
p component
Run Code Online (Sandbox Code Playgroud)
结果是:
[:initialize, #<Package::Component_1:0x2c0a8f8>]
#<Package::Component:0x2c0a8f8>
[:initialize, #<Package::Component_1:0x2c0a5b0>]
#<Package::Component:0x2c0a
Run Code Online (Sandbox Code Playgroud)
如何获取特定的Package_A.component和Package_B.component?
我有一个问题,你在那里linq专家!在组件实例的嵌套列表中,我需要知道其中是否存在特定类型的组件.可以用linq表达吗?考虑到可能有application.Components [0] .Components [0] .Components [0] ...我的问题是面向linq中的递归查询!
我告诉你实体让你对模型有所了解.
public class Application
{
public List<Component> Components { get; set; }
}
public class Component
{
public ComponentType Type { get; set; }
public List<Component> Components { get; set; }
}
public enum ComponentType
{
WindowsService,
WebApplication,
WebService,
ComponentGroup
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个嵌套类以便使用AsyncTask,但是eclipse在类SendData上给出了一个错误,说"令牌上的语法错误"类,"无效类型"为什么它会给我这个错误?
package com.example.myfirstapp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.Bundle;
import android.app.Activity;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.content.Intent;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
String url = "http://www.mySIte.com/phpFIle";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
new SendData().execute();
}
private class SendData() extends AsyncTask<Void, Void, Void>{ …Run Code Online (Sandbox Code Playgroud) 我想知道嵌套类如何在for循环中工作:
这是代码:
class Outer {
int outer_x = 100;
void test() {
for(int i=0; i<10; i++) {
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
Inner inner = new Inner();
inner.display();
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}
Run Code Online (Sandbox Code Playgroud) 我按照http://developer.android.com/reference/android/app/AlertDialog.html链接,我尝试创建这样的新AlertDialog
AlertDialog myAlertDialog = new AlertDialog.Builder(MainActivity.this).create();
Run Code Online (Sandbox Code Playgroud)
根据文档,AlerDialog是外部类,Builder是AlertDialog中的内部类.现在我在访问内部类时使用java链接相同的概念,这样Outer myOuter2 = new Outer.Inner();当我尝试访问时这段错误,这里是完整的java代码
package com.test;
public class Outer {
public void OuterMethod() {
System.out.println("OuterMethod");
}
public static void main(String[] args) {
Outer myOuter = new Outer();
myOuter.OuterMethod();
Outer myOuter2 = new Outer.Inner();//this piece of code gives error
}
class Inner {
Inner() {
System.out.println("constructor Inner");
}
public void InnerMethod() {
System.out.println("Inside InnerMethod");
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我在这里的问题是如何在android中理解相同的内部类概念并访问其中的方法
我有一个通用类X<T>; 这个类有一个协变部分,我希望能够协同访问.所以我将它分解为一个界面IX<out T>.但是,我希望这个接口只对类本身可见,因为它还包含了方法private.
也就是说,在课堂内部,我可以升级IX<T>并且共同使用它.例如:
class X<T> : IX<T> {
private interface IX<out T>{ // the private covariant interface
void foo();
}
// It grants access to the private method `foo`
private T foo(){...}
public T IX.foo(){ return foo(); }
private static void someMethod(IX<T> x) {
// Here I can use `x` covariantly
}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?我之前从未听说过private嵌套接口,因为私有接口通常根本没有任何意义.然而,对于泛型,这种接口对于实现"仅私有协方差"是必要的.
当我尝试编译它时,我收到以下错误:
foo.cs(1,14): error CS0246: The type or namespace name `IX' could not be found. Are …Run Code Online (Sandbox Code Playgroud) 我有这个(简化)情况:
class Tree {
class Iterator {
class Stack {
// ...
}
public:
// ...
}
public:
//...
}
Run Code Online (Sandbox Code Playgroud)
我不想弄乱类的定义,我决定只在类本身内编写方法声明.稍后(下面是waaay)我想要定义,比如复制赋值运算符,如下所示:
Tree::Iterator::Stack& Tree::Iterator::Stack::operator = (const Stack& p_stack) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
我必须处理这些令人讨厌的范围决议.我想知道,如果有一种方法,以缩短他们,因为using和typedef,因为我知道他们,不提供我任何东西.
编辑:由于这不是CodeReview,@ Yulian要求澄清,这里是简短的版本:
我正在进行迭代的Red-Black Tree实现.提到的class Iterator是后期遍历(因此它是特定于订单后的顺序),并且class Stack是它的实用程序类.在这个简短的程序中,只class Tree使用Iterator,而且只Iterator使用Stack.
在@Yulian的提醒之后,我回想一下,如果提到的类被单独定义(甚至可能作为模板),它会更加面向对象,但这是一个小的,自包含的程序,我试图保持这种方式.
编辑:自包含也意味着它是一个独立的单文件程序,所以没有.h文件或外部代码重新使用.为什么?因为ACADEMIA(和相关的任意限制).
nested-class ×10
c# ×3
c++ ×3
java ×3
android ×2
inheritance ×2
covariance ×1
eclipse ×1
for-loop ×1
friend ×1
generics ×1
linq ×1
recursion ×1
ruby ×1
scope ×1
syntax-error ×1
tree ×1
typename ×1
types ×1
using ×1
visibility ×1