想知道以下几点之间的区别:
案例1:基类
public void DoIt();
Run Code Online (Sandbox Code Playgroud)
案例1:继承的类
public new void DoIt();
Run Code Online (Sandbox Code Playgroud)
案例2:基类
public virtual void DoIt();
Run Code Online (Sandbox Code Playgroud)
案例2:继承的类
public override void DoIt();
Run Code Online (Sandbox Code Playgroud)
根据我运行的测试,情况1和2似乎具有相同的效果.有区别,还是首选方式?
我想测试一个类是否继承自另一个类,但似乎没有一个方法.
class A
end
class B < A
end
B.is_a? A
=> false
B.superclass == A
=> true
Run Code Online (Sandbox Code Playgroud)
我想要的一个微不足道的实现是:
class Class
def is_subclass_of?(clazz)
return true if superclass == clazz
return false if self == Object
superclass.is_subclass_of?(clazz)
end
end
Run Code Online (Sandbox Code Playgroud)
但我希望这已经存在了.
我是JavaScript OOP的新手.你能解释下面的代码块之间的区别吗?我测试了两个块都有效.什么是最佳实践,为什么?
第一块:
function Car(name){
this.Name = name;
}
Car.prototype.Drive = function(){
console.log("My name is " + this.Name + " and I'm driving.");
}
SuperCar.prototype = new Car();
SuperCar.prototype.constructor = SuperCar;
function SuperCar(name){
Car.call(this, name);
}
SuperCar.prototype.Fly = function(){
console.log("My name is " + this.Name + " and I'm flying!");
}
var myCar = new Car("Car");
myCar.Drive();
var mySuperCar = new SuperCar("SuperCar");
mySuperCar.Drive();
mySuperCar.Fly();Run Code Online (Sandbox Code Playgroud)
第二块:
function Car(name){
this.Name = name;
this.Drive = function(){
console.log("My name is " + this.Name + …Run Code Online (Sandbox Code Playgroud)javascript oop inheritance constructor prototype-programming
有什么理由禁止Java中的继承,例如使用单个私有无参数构造函数使用final类或类?使方法最终成功的理由是什么?
虽然我们可以从基类/接口继承,但为什么我们不能声明List<>
使用相同的类/接口?
interface A
{ }
class B : A
{ }
class C : B
{ }
class Test
{
static void Main(string[] args)
{
A a = new C(); // OK
List<A> listOfA = new List<C>(); // compiler Error
}
}
Run Code Online (Sandbox Code Playgroud)
有办法吗?
我今天在模块开卷考试结束时遇到了这个问题,发现自己迷路了.我正在阅读Head first Java,两个定义似乎完全相同.我只是想知道主要区别在于我自己的想法.我知道有很多类似的问题,但是,我没有看到哪些提供了明确的答案.
我有两个项目:父项目:A,子项目:B
A/pom.xml中:
<groupId>com.dummy.bla</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
Run Code Online (Sandbox Code Playgroud)
在B/pom.xml中,我有:
<parent>
<groupId>com.dummy.bla</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
</parent>
<groupId>com.dummy.bla.sub</groupId>
<artifactId>kid</artifactId>
Run Code Online (Sandbox Code Playgroud)
我希望B从父级继承该版本,所以我需要放置的唯一位置0.1-SNAPSHOT是A/pom.xml.但是,如果删除了<version>0.1-SNAPSHOT</version>从B/pom.xml下父节,Maven的抱怨缺少的版本父.
有没有办法可以使用${project.version}或类似的东西,以避免01.-SNAPSHOT在两个poms?
我知道在C++中为基类声明虚拟析构函数是一个好习惯,但是virtual即使对于作为接口的抽象类来说,声明析构函数总是很重要吗?请提供一些理由和示例原因.
function Gadget(name, color)
{
this.name = name;
this.color = color;
}
Gadget.prototype.rating = 3
var newtoy = new Gadget("webcam", "black")
newtoy.constructor.prototype.constructor.prototype.constructor.prototype
Run Code Online (Sandbox Code Playgroud)
它总是返回rating = 3的对象.
但如果我做以下事情:
newtoy.__proto__.__proto__.__proto__
Run Code Online (Sandbox Code Playgroud)
链条最终返回null.
另外在Internet Explorer中,如果没有__proto__属性,我如何检查null ?
inheritance ×10
c# ×2
c++ ×2
java ×2
javascript ×2
oop ×2
collections ×1
constructor ×1
covariance ×1
final ×1
list ×1
maven ×1
new-operator ×1
overriding ×1
polymorphism ×1
ruby ×1
struct ×1
subclass ×1
superclass ×1