具有相同属性和方法的对象属于同一个类吗?
具有相同属性和方法的对象属于同一个类吗?
我不能声明两个具有相同方法和属性的相同类,实例化它们并拥有“具有属于不同类的相同属性和方法的对象”吗?
我不能声明一个类A和一个子类B(类A的子类)都具有相同的方法和属性(并且不在类B上声明任何新的属性或方法),实例化它们并具有“具有属于不同类的相同属性和方法的对象”?
这个问题与良好实践无关……而是关于标题问题的逻辑值(真或假)。
Objects with the same attributes and methods belongs to the same class?
Can't I declare two identical classes with the same methods and attributes, instanciate them and have "objects with the same attributes and methods belonging to different classes"?
Can't I declare a class A and a sub-class B (children of the class A) both with the same methods and attributes (and don't declare any new attribute or method on the class B), instanciate them and have "objects with the same attributes and methods belonging to different classes"?
This question is not about good practices... It's about the logical value (true or false) of the question on the title.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有指定您正在谈论的语言;也许有一些外来语言具有您所描述的属性,但至少在 Java 和 C++ 这样的语言中,不存在根据两个类声明的方法/实例变量来推断它们“相同”的概念。
所以,答案是“不,尽管它们看起来相同,但它们不是同一类”。
子类的情况有点不同:如果将
B
声明为A
的子类(不添加任何方法或变量),它们仍然是不同的类,但是由于正常的继承规则,B
类的对象也是“is-a
”A
。You don't specify what language(s) you're talking about; perhaps there's some exotic language that has the property you describe, but at least in languages like Java and C++ there's no concept of inferring that two classes are "the same" based on what methods/instance variables they declare.
So, the answer is "no, they are not the same class, even though they look the same".
The sub-class case is a little different: if you declare
B
as a subclass ofA
(without adding any methods or variables), they are still different classes, but an object of classB
also "is-a
"A
, because of normal inheritance rules.那绝对是可能的。
两个类都是相同的。具有相同的属性和方法。但他们的实例会有所不同。
Thats definitely possible.
Both classes are identical. Have the same properties and methods. But their instances will be different.