开发者

Objects with the same attributes and methods belongs to the same class?

开发者 https://www.devze.com 2022-12-29 06:36 出处:网络
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, insta开发者_JAVA百科nciate them and have \"object

Objects with the same attributes and methods belongs to the same class?

  1. Can't I declare two identical classes with the same methods and attributes, insta开发者_JAVA百科nciate them and have "objects with the same attributes and methods belonging to different classes"?

  2. 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.


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 of A (without adding any methods or variables), they are still different classes, but an object of class B also "is-a" A, because of normal inheritance rules.


Thats definitely possible.

class A
{

    public string Property1{get;set;}
    public string Method1(int value)
    {
      ......
    }

}

class B
{
   public string Property1{get;set;}
   public string Method1(int value)
   {
     ........
   }
}

Both classes are identical. Have the same properties and methods. But their instances will be different.

0

精彩评论

暂无评论...
验证码 换一张
取 消