你有一个Hibernate实体的公共基类,即具有id,version和其他常见属性的MappedSuperclass吗?有什么缺点吗?
例:
@MappedSuperclass()
public class BaseEntity {
private Long id;
private Long version;
...
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
@Version
public Long getVersion() {return version;}
...
// Common properties
@Temporal(TemporalType.TIMESTAMP)
public Date creationDate() {return creationDate;}
...
}
@Entity
public class Customer extends BaseEntity {
private String customerName;
...
}
Run Code Online (Sandbox Code Playgroud) 凡在onStart(),onStop(),onDestroy()的活动我打电话super.onStart(),super.onStop(),super.onDestroy()?
考虑以下代码:
#include <iostream>
namespace N {
class A {};
void f(A a) { std::cout << "N::f\n"; }
}
void f(int i) { std::cout << "::f\n"; }
template <typename T>
class Base {
public:
void f(T x) { std::cout << "Base::f\n"; }
};
template <typename T>
class X : public Base<T> {
public:
void g() {
T t;
f(t);
}
};
int main()
{
X<N::A> x1;
x1.g();
X<int> x2;
x2.g();
}
Run Code Online (Sandbox Code Playgroud)
该代码旨在研究名称查找在C++中的工作方式.
如果我用GNU C++(版本6.1.0)编译该程序,它会打印:
N::f
::f
Run Code Online (Sandbox Code Playgroud)
但如果我用Microsoft Visual Studio 2015编译它,它会打印:
Base::f …Run Code Online (Sandbox Code Playgroud) 由于此问题在这里,我想编写处理,你继承列表或集合,然后额外的属性,将其添加的情况下自定义JsonConverter.因此,一种方法是忽略所有基类属性,并仅序列化已定义类中的属性.(从技术上讲这不会起作用,因为如果你继承了那个子类,你就会破坏序列化,但它确实让我想知道......)
是否有可能通过反射(我知道答案是肯定的,因为Reflector正是如此,但我不知道如何)只获得在类本身定义的成员而不是继承的成员?例如...
public class MyBaseClass
{
public string BaseProp1 { get; set; }
public string BaseProp2 { get; set; }
}
public class MySubClass : MyBaseClass
{
public string SubProp1 { get; set; }
public string SubProp2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我想反映的MySubClass,只有获得SubProp1和SubProp2而忽略BaseProp1和BaseProp2.所以,可以说是如何做呢?
中号
我偶然发现了C#的这个"特性" - 实现接口方法的基类不必从它派生.
例:
public interface IContract
{
void Func();
}
// Note that Base does **not** derive from IContract
public abstract class Base
{
public void Func()
{
Console.WriteLine("Base.Func");
}
}
// Note that Derived does *not* provide implementation for IContract
public class Derived : Base, IContract
{
}
Run Code Online (Sandbox Code Playgroud)
会发生什么事情,Derived神奇地采用公共方法Base.Func,并决定它将实施IContract.Func.
这魔术背后的原因是什么?
恕我直言:这种"准实现"功能非常不直观,使代码检查更加困难.你怎么看?
我明白为什么会发生这种情况,但我一直试图解决它...这是我的代码在我的程序退出时生成错误(因此导致崩溃)时所做的事情...
pure virtual method called
SomeClass::~SomeClass()
{
BaseClassObject->SomePureVirtualMethod(this);
}
Run Code Online (Sandbox Code Playgroud)
void DerivedClass::SomePureVirtualMethod(SomeClass* obj)
{
//Do stuff to remove obj from a collection
}
Run Code Online (Sandbox Code Playgroud)
我从来没有打过电话,new SomeClass但我有一个QList<SomeClass*>我追加SomeClass*对象的东西.这个析构函数的目的SomeClass是告诉从它的集合中DerivedClass删除一个特定的实例.SomeClassQList<SomeClass*>
所以,在一个具体的例子......
BaseClass = Shape
DerivedClass = Triangle
SomeClass= ShapeProperties拥有对的引用Shape
所以,我从来没有打过电话,new ShapeProperties但我有一个QList<ShapeProperties*>内心Triangle.析构函数ShapeProperties是告诉从它的集合中Triangle删除特定属性.ShapePropertiesQList<ShapeProperties*>
我已经阅读了几个类似的问题,但似乎没有解决我面临的问题.典型的答案是作为派生类进行转换,但我不能因为我不知道派生类类型.
这是我的例子:
class WireLessDevice { // base class
void websocket.parsemessage(); // inserts data into the wireless device object
}
class WiFi : WireLessDevice { // derived class
GPSLoc Loc;
}
Run Code Online (Sandbox Code Playgroud)
还可以导出无线设备以制作蓝牙,Wi-Max,蜂窝等设备,因此我不知道哪种类型的无线设备将接收数据.
当在基类中的websocket上接收到GPS数据包时,我需要更新派生设备的位置.
我想或许可以通过队列发送消息或创建事件处理程序并在事件参数中发送位置,但是当数据停留在类中时,这些看起来有点笨拙.
是否有内置于该语言中的内容可以让我在不知道类型的情况下从基类调用我的派生设备?
令我感到困惑的是,我无法在任何地方找到明确的解释.为什么以及何时需要在子类的同名方法中调用基类的方法?
class Child(Base):
def __init__(self):
Base.__init__(self)
def somefunc(self):
Base.somefunc(self)
Run Code Online (Sandbox Code Playgroud)
当你不想完全覆盖基类中的方法时,我猜你这样做了.是真的有它的全部吗?
是否可以使C#基类只能在它编译成的库程序集中访问,同时使其他继承它的子类公开?
例如:
using System.IO;
class BaseOutput: Stream // Hidden base class
{
protected BaseOutput(Stream o)
{ ... }
...lots of common methods...
}
public class MyOutput: BaseOutput // Public subclass
{
public BaseOutput(Stream o):
base(o)
{ ... }
public override int Write(int b)
{ ... }
}
Run Code Online (Sandbox Code Playgroud)
在这里,我希望BaseOutput我的库的客户端无法访问该类,但允许子类MyOutput完全公开.我知道,C#不允许基类具有比子类更严格的访问,但有获得同样的效果的一些其他合法的方式?
UPDATE
我对这个特殊的库解决方案是让基类public和abstract,并与记录它"不要直接使用这个基类".我还创建了基类的构造函数internal,它有效地防止外部客户端使用或继承该类.
(这很遗憾,因为其他OO语言让我有隐藏的基类.)
我最近创建了一个支持+, - 等操作的不可变类,它在更改时返回该类的新实例.
我想创建该类的子类来添加一些状态和功能,但现在我遇到了一个问题,即所有原始类的方法都返回自身的实例而不是子类.
基于我目前对Scala的有限知识,我可以想出这个:
class Foo(val bar:Int) {
def copy(newBar:Int) = new Foo(newBar)
def + (other:Foo):This = copy(this.bar + other.bar)
}
class Subclass(barbar:Int) extends Foo(barbar) {
override def copy(newBar:Int) = new Subclass(newBar)
override def + (other:Subclass) = super.+(other).asInstanceOf[Subclass]
}
Run Code Online (Sandbox Code Playgroud)
这里的问题非常明显 - 返回新实例的超类的所有操作都必须在子类中使用强制转换重新定义.
起初"this.type"似乎很有希望,但"this.type"只包含"this"而不包含任何其他相同类型的对象.
是否存在使不可变类易于子类化的标准模式?就像是:
class Foo(val bar:Int) {
def copy(newBar:Int):SameType = new Foo(newBar)
def + (other:Foo) = copy(this.bar + other.bar)
}
class Subclass(barbar:Int) extends Foo(barbar) {
override def copy(newBar:Int):SameType = new Subclass(newBar)
override def + (other:Subclass) = super.+(other).asInstanceOf[Subclass]
} …Run Code Online (Sandbox Code Playgroud) base-class ×10
c# ×4
inheritance ×3
c++ ×2
android ×1
class ×1
entities ×1
hibernate ×1
interface ×1
java ×1
methods ×1
name-lookup ×1
overwrite ×1
polymorphism ×1
private ×1
pure-virtual ×1
python ×1
reflection ×1
scala ×1
subclass ×1
templates ×1