我有一个具有抽象getType()方法的基类.我希望子类能够实现此方法并提供要使用的实际类.
在代码中,类似于以下内容:
public abstract class A {
public static interface Tile;
protected abstract Class<Tile> getTileClass();
}
public class B extends A {
public static class MyTile implements A.Tile { }
@Override
protected abstract Class<A.Tile> getTileClass() {
MyTile t = new MyTile(); // WORKS
return MyTile; // ERROR HERE
}
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是我在标记的行中得到"MyTile无法解析".所以我试图回复这个:
返回新的MyTile().getClass()
但现在Eclipse告诉我:
类型不匹配:无法转换类
<捕获#1-of?将B.MyTile扩展>到Class<A.Tile>
我甚至不确定Eclipse中是否有可能存在错误(捕获#1?).
接下来,我放弃接口并尝试使用抽象基础Tile类.在Eclipse的帮助下,我最终得到了以下似乎编译的代码:
public abstract class A {
public static abstract class Tile;
protected abstract Class<? extends Tile> getTileClass();
} …Run Code Online (Sandbox Code Playgroud) 我很好奇在涉及.NET中嵌套类型的某些场景时,什么是好的做法.
假设您有一个Wheel类,Wheel类包含Bearing对象.Bearing对象仅在Wheel中有意义,并且您不希望允许它独立创建,因此将Bearing类嵌套在Wheel对象中是有意义的.但是,假设您有一个场景,您现在需要读取Wheel类之外的Wheel.Bearings属性.现在需要将嵌套的Bearing类设为public.
在这种情况下,最好的选择是什么?
1 - 创建嵌套在Wheel类
2中的公共Bearing类- 创建一个独立的Bearing类,在其构造函数中获取Wheel对象
3 - 创建Wheel命名空间并在此命名空间内创建一个独立的Bearing类.
4 - 别的什么?
更新:我正在更新这个更多的细节,并反映到目前为止的一些建议.ClassParent是父类,ClassChild是子类.ClassChild总是ClassParent的一个孩子,单独存在是没有意义的.问题是ClassChild有一些需要公开暴露的属性,而所有其他属性只能从ClassParent调用.一个示例是ClassChild.Delete函数,该函数不应公开公开,因为它只应从ClassParent调用,因为ClassParent需要执行适当的清理和修改.
在审查了这些建议之后,我提出的设计对我来说看起来有些不洁,所以我想我会要求输入.我有:
public class Parent
{
ChildNested childObj
public DeleteChild()
{
//expose this method publically
childObj.DeleteChild()
//other functionality
}
public Child GetChild()
{
//expose Child, not ChildNested publically
return childObj
}
private class ChildNested:Child
{
public Child()
{
Base.Child()
}
public DeleteChild()
{
Base.Delete()
}
}
public abstract class Child
{
protected Child()
{
}
protected Delete()
{
}
public PublicPropertyToExpose()
{
}
}
Run Code Online (Sandbox Code Playgroud) 考虑您有以下课程
public class OuterClass {
...
private static class InnerClass {
int foo;
int bar;
}
}
Run Code Online (Sandbox Code Playgroud)
我想我已经阅读过(但不是官方的Java教程),如果我将静态成员类属性声明为私有,编译器必须生成某种访问器方法,以便外部类实际上可以访问静态成员类(这实际上是一个包 - 私有顶级类)属性.
有什么想法吗?
C#中的嵌套类型可以访问父级的私有属性.有这种语言功能的具体原因是什么?在我看来,这打破了封装.如果我将嵌套类型设为public,那么我将能够通过它公开父类的私有属性.
以下代码无法在Visual Studio 2005中编译:
class OriginalClass
{
public:
class Delegate
{
virtual void original_func()=0;
};
};
class BaseClass
:public OriginalClass::Delegate // Problem line 1
{
public:
class Delegate
{
virtual void base_func(int x) = 0;
};
void original_func()override{} // Problem line 2
};
class DerivedClass : public BaseClass::Delegate
{
public:
virtual void base_func(int x) override {};
};
int main ()
{
DerivedClass derived_object;
derived_object.base_func(10);
}
Run Code Online (Sandbox Code Playgroud)
构建输出是:
1>inherit\main.cpp(26) : error C3668: 'DerivedClass::base_func' : method with override specifier 'override' did not override any …Run Code Online (Sandbox Code Playgroud) 我正在研究Reflection,但我在执行递归时遇到困难.
代码:
public class User {
public string Name;
public int Number;
public Address Address;
}
public class Address {
public string Street;
public string State;
public string Country;
}
Run Code Online (Sandbox Code Playgroud)
现在我正在打印价值观.
Type t = user.GetType();
PropertyInfo[] props = t.GetProperties();
foreach (PropertyInfo prp in props)
{
if(!prp.GetType().IsPrimitive && prp.GetType().IsClass)
{
// Get the values of the Inner Class.
// i am stucked over here , can anyone help me with this.
Type ty = prp.GetType();
var prpI = ty.GetProperties();
//var tp …Run Code Online (Sandbox Code Playgroud) 我的类实现了ActionListener.我在下面实现了以下嵌套类:
JMenuItem mntmNew = new JMenuItem("New...");
mntmNew.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
doNew(e); //calls to outer class for cleaner code
}
});
mnFile.add(mntmNew);
JMenuItem mntmLoad = new JMenuItem("Load...");
mntmLoad.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
doLoad(e); //calls to outer class for cleaner code
}
});
mnFile.add(mntmLoad);
//etc. for the rest of the menu system
Run Code Online (Sandbox Code Playgroud)
但是,Eclipse仍然告诉我,我的类必须实现继承的抽象方法ActionListener.actionPerformed(ActionEvent e).你能不能以这种方式在嵌套类中实现覆盖方法?
我有以下设置:
template< class T >
struct Foo {
struct Bar {
Bar ( const T &t ) : otherT_( t ) {}
T otherT_;
};
Foo ( const T &t ) : myT_( t ) {}
T myT_;
};
Run Code Online (Sandbox Code Playgroud)
现在,我想制作Foo< T >::Barstremable到std :: cout和朋友的实例.我试过这个:
template< class T >
std::ostream& operator<< ( std::ostream &os,
const typename Foo< T >::Bar &bar ) {
os << "<bar: " << bar.otherT_ << ">";
return os;
}
Run Code Online (Sandbox Code Playgroud)
但是以下代码无法编译:
Foo< int > foo( 5 …Run Code Online (Sandbox Code Playgroud) 基本上只是想知道附件中的图像是否显示有效的CSS用法?我最近使用了很多嵌套的div,这就是我在CSS中定位它们的方式。它确实很好用,但我只是好奇这是否是个好方法?还是有一种更简单/更有效的方法?
谢谢!
请考虑以下代码示例:
class Outer
{
public class Nested { }
}
class SubOuter : Outer { }
class Test
{
Test()
{
Outer.Nested x; // Makes sense.
SubOuter.Nested y; // Compiles, but why?
}
}
Run Code Online (Sandbox Code Playgroud)
似乎嵌套类是由子类"继承"的.那是什么意思?如果我不能(或不能轻易)做到这个功能,我该怎么办?是不是Outer.Nested和SubOuter.Nested完全等同?
澄清:当然它是编译的,因为C#规范是这样说的.我明白那个.我在问为什么 C#是这样设计的,因为它似乎没有为语言增加一些东西.如果您有一个相反的例子,即使用此功能更容易/更短/更好的一些代码,请在答案中分享它,我很乐意接受它.
nested-class ×10
nested ×4
.net ×3
c# ×3
java ×3
c++ ×2
inheritance ×2
actionevent ×1
class ×1
css ×1
friend ×1
interface ×1
namespaces ×1
oop ×1
recursion ×1
reflection ×1
swing ×1
templates ×1