Tag: inheritance

在JDK中默认是Java中的多重inheritance吗?

JDK 8中提供的新function允许您添加到现有界面,同时保持二进制兼容性。 语法就像 public interface SomeInterface() { void existingInterface(); void newInterface() default SomeClass.defaultImplementation; } 这种方式对于所有现有的SomeInterface实现,当他们升级到这个新版本时,他们并不都突然在newInterface()周围编译错误。 虽然这很整齐,但是当你实现两个接口时,会发生什么事情,这两个接口都添加了一个你没有实现的新的默认方法? 让我以一个例子来解释。 public interface Attendance { boolean present() default DefaultAttendance.present; } public interface Timeline { boolean present() default DefaultTimeline.present; } public class TimeTravelingStudent implements Attendance, Timeline { } // which code gets called? new TimeTravelingStudent().present(); 这是否被定义为JDK 8的一部分呢? 我发现Java神在这里谈论类似的东西http://cs.oswego.edu/pipermail/lambda-lib/2011-February/000068.html ,但它的私人邮件列表的一部分,我不能直接问他们。 有关如何在JDK […]

指针值是不同的,但他们比较相等。 为什么?

一个简短的例子输出一个奇怪的结果! #include <iostream> using namespace std; struct A { int a; }; struct B { int b; }; struct C : A, B { int c; }; int main() { C* c = new C; B* b = c; cout << "The address of b is 0x" << hex << b << endl; cout << […]

使用Serializable属性和实现ISerializable有什么区别?

使用Serializable属性和实现ISerializable接口有什么区别?

从具体类inheritance的任何好例子?

背景: 作为一名Java程序员,我从接口广泛地inheritance(而不是实现),有时我devise抽象基类。 然而,我从来没有真的觉得有必要对一个具体的(非抽象的)类进行子类化(在我这样做的情况下,后来certificate另一个解决scheme,比如代表团会更好)。 所以现在我开始觉得,从具体的课程中inheritance是不合适的。 一方面, Liskov替代原则似乎几乎不可能满足非平凡的阶级; 在这里还有许多其他的问题似乎回应了类似的意见。 所以我的问题是: 在哪种情况下(如果有)从具体类inheritance是否有意义? 你能否给出一个具体的,真实的例子,从另一个具体的类inheritance的类,你觉得这是最好的devise给定的约束? 我会特别感兴趣的例子,满足LSP(或例如满足LSP似乎不重要)。 我主要有一个Java背景,但我对任何语言的例子感兴趣。

Javascript的inheritance:调用超级构造函数或使用原型链?

最近我读了关于MDC中JavaScript调用的使用 https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call 下面显示的一个例子,我仍然不明白。 为什么他们这样使用inheritance Prod_dept.prototype = new Product(); 这是必要的吗? 因为有一个对超级构造函数的调用 Prod_dept() 无论如何,这样的 Product.call 这是不是常见的行为? 什么时候使用超级构造函数调用或使用原型链更好? function Product(name, value){ this.name = name; if(value >= 1000) this.value = 999; else this.value = value; } function Prod_dept(name, value, dept){ this.dept = dept; Product.call(this, name, value); } Prod_dept.prototype = new Product(); // since 5 is less than 1000, value […]

超级骨干

当我重写Backbone.Model的clone()方法时,有没有办法从我的植入调用这个重写方法? 像这样的东西: var MyModel = Backbone.Model.extend({ clone: function(){ super.clone();//calling the original clone method } })

不能使用<union-subclass>(TABLE_PER_CLASS)标识列密钥生成

com.something.SuperClass: @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class SuperClass implements Serializable { private static final long serialVersionUID = -695503064509648117L; long confirmationCode; @Id @GeneratedValue(strategy = GenerationType.AUTO) // Causes exception!!! public long getConfirmationCode() { return confirmationCode; } public void setConfirmationCode(long confirmationCode) { this.confirmationCode = confirmationCode; } } com.something.SubClass: @Entity public abstract class Subclass extends SuperClass { private […]

Python中的inheritance点是什么?

假设你有以下情况 #include <iostream> class Animal { public: virtual void speak() = 0; }; class Dog : public Animal { void speak() { std::cout << "woff!" <<std::endl; } }; class Cat : public Animal { void speak() { std::cout << "meow!" <<std::endl; } }; void makeSpeak(Animal &a) { a.speak(); } int main() { Dog d; Cat […]

如何扩展/inheritanceAngular2组件?

题 我想为已经部署在Angular 2中的一些组件创build扩展,而不必几乎完全重写它们,因为基本组件可能发生变化,并希望这些变化也反映在派生的组件中。 例 我创build了这个简单的例子,试图解释更好的问题: 使用以下基本组件app/base-panel.component.ts : import {Component, Input} from 'angular2/core'; @Component({ selector: 'base-panel', template: '<div class="panel" [style.background-color]="color" (click)="onClick($event)">{{content}}</div>', styles: [` .panel{ padding: 50px; } `] }) export class BasePanelComponent { @Input() content: string; color: string = "red"; onClick(event){ console.log("Click color: " + this.color); } } 您是否想创build另一个派生组件,例如,在示例颜色app/my-panel.component.ts的情况下,基本组件行为: import {Component} from 'angular2/core'; import {BasePanelComponent} from […]

Ruby自定义错误类:inheritance消息属性

我似乎无法find有关自定义exception类的很多信息。 我所知道的 你可以声明你的自定义错误类,让它inheritance自StandardError ,所以可以rescue d: class MyCustomError < StandardError end 这可以让你用下面的方法提高它: raise MyCustomError, "A message" 之后,在救援时得到这个消息 rescue MyCustomError => e puts e.message # => "A message" 我不知道 我想给我的例外一些自定义字段,但我想inheritance父类的message属性。 我发现读这个主题 @message不是一个exception类的实例variables,所以我担心我的inheritance将无法正常工作。 任何人都可以给我更多的细节呢? 我将如何实现具有object属性的自定义错误类? 以下是正确的: class MyCustomError < StandardError attr_reader :object def initialize(message, object) super(message) @object = object end end 接着: raise MyCustomError.new(anObject), "A message" 要得到: rescue […]