Tag: inheritance

C#静态成员“inheritance” – 为什么这存在呢?

在C#中,超类的静态成员被“inheritance”到子类作用域中。 例如: class A { public static int M() { return 1; } } class B : A {} class C : A { public new static int M() { return 2; } } […] AM(); //returns 1 BM(); //returns 1 – this is equivalent to AM() CM(); //returns 2 – this is not equivalent […]

Java / JAXB:根据属性将Xml解组到特定的子类

是否有可能使用JAXBparsingxml到一个特定的Java类基于XML的属性? <shapes> <shape type="square" points="4" square-specific-attribute="foo" /> <shape type="triangle" points="3" triangle-specific-attribute="bar" /> </shapes> 我想有一个包含三angular形和正方形的Shape对象列表,每个都有自己的形状特定的属性。 IE: abstract class Shape { int points; //…etc } class Square extends Shape { String square-specific-attribute; //…etc } class Triangle extends Shape { String triangle-specific-attribute; //…etc } 我现在只是把所有的属性放在一个大的“形状”类,并不理想。 我可以得到这个工作,如果形状是正确命名的XML元素,但不幸的是我没有控制我正在检索的XML。 谢谢!

如何调用超类的重载方法

如何在代码中使用myAnimal实例调用Animal类的吃喝方法? public class Animal { public void eat() { System.out.println("Animal Eats"); } public void drink() { System.out.println("Animal Drinks"); } } public class Cat extends Animal { @Override public void eat() { System.out.println("Cat Eats"); } @Override public void drink() { System.out.println("Cat Drinks"); } public static void main(String[] args) { Cat myCat = new Cat(); myCat.eat(); myCat.drink(); […]

使用多重inheritance时,如何避免死亡钻石?

http://en.wikipedia.org/wiki/Diamond_problem 我知道这意味着什么,但是我可以采取什么措施来避免呢?

为什么不自动调用Python的超类__init__方法?

为什么Pythondevise者决定,子类的__init__()方法不像其他一些语言那样自动调用其超类的__init__()方法? Pythonic和推荐的成语真的如下? class Superclass(object): def __init__(self): print 'Do something' class Subclass(Superclass): def __init__(self): super(Subclass, self).__init__() print 'Do something else'

使用AspectJ模拟接口和方法的注解inheritance

通常人们会问这样的AspectJ问题,所以我想在稍后可以轻松链接的地方回答。 我有这个标记注释: package de.scrum_master.app; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Inherited @Retention(RetentionPolicy.RUNTIME) public @interface Marker {} 现在我注释一个接口和/或像这样的方法: package de.scrum_master.app; @Marker public interface MyInterface { void one(); @Marker void two(); } 这里是一个小驱动程序的应用程序,它也实现了接口: package de.scrum_master.app; public class Application implements MyInterface { @Override public void one() {} @Override public void two() {} public static void main(String[] args) { […]

Javascript类inheritance的function

我已经build立了一个标准的基类: MyBase = function() { this.m_Stuff = 0; // etc }; MyBase.prototype.MySuperFunction = function (arg1) { alert("Hello" + arg1); }; 接下来,我设置了inheritanceMyBase的另一个类 MyChild = function () { MyBase.call(this); this.m_OtherStuff = 1; // etc }; MyChild.prototype = new MyBase(); // innherit 但是,然后(这是我不知道该怎么做)我想用一个更好的覆盖MyBase的MySuperFunction,但在过程中调用基类函数: MyChild.prototype.MySuperFunction = function (arg1, arg2) { MyBase.MySuperFunction(arg1); // THIS LINE IS THE LINE I DONT […]

inheritancejava和超类(Object,Class)

所有自定义类/对象的java.lang.Object超类是隐式inheritance的吗? 我以为java不支持多重inheritance。 我问的原因是,如果我已经从我的自定义类中的另一个类inheritance,并再次java强制java.lang.Object上的隐式inheritance,是不是多重inheritance? 另外,java.lang.class类也是所有自定义类/对象的超类吗? 如果没有,如何在Javareflection我们可以得到任何类的类传递或调用isInstance任何对象?

Python中私有和受保护方法的inheritance

我知道,Python中没有“真正的”private / protected方法。 这种方法不是隐藏任何东西,我只是想了解Python的function。 class Parent(object): def _protected(self): pass def __private(self): pass class Child(Parent): def foo(self): self._protected() # This works def bar(self): self.__private() # This doesn't work, I get a AttributeError: # 'Child' object has no attribute '_Child__private' 那么,这种行为是否意味着“受保护的”方法将会被inheritance,但是“私有的”方法将不会被inheritance? 还是我错过了什么?

如何从扩展的PHP类中的静态调用获取类名?

我有两个类: Action和MyAction 。 后者被宣布为: class MyAction extends Action {/* some methods here */} 我所需要的只是Action类中的方法(仅在这个类中,因为会有很多inheritance类,而且我不想在所有这些类中实现这个方法),它将从静态调用中返回classname。 以下是我正在谈论的内容: Class Action { function n(){/* something */} } 当我把它叫做: MyAction::n(); // it should return "MyAction" 但是父类中的每个声明都只能访问具有值“Action”的父类__CLASS__variables。 有没有办法做到这一点?