overriding

Java Set collection - override equals method

这一生的挚爱 提交于 2019-11-28 23:10:17
Is there any way to override the the equals method used by a Set datatype? I wrote a custom equals method for a class called Fee . Now I have a LnkedList of Fee and I want to ensure that there are no duplicated entries. Thus I am considering using a Set insted of a LinkedList , but the criteria for deciding if two fees are equal resides in the overriden equals method in the Fee class. If using a LinkedList , I will have to iterate over every list item and call the overriden equals method in the Fee class with the remaining entries as a parameter. Just reading this alone sounds like too much

When should you call base.Method() in overridden method, and how to mark this when you write code in team?

北城以北 提交于 2019-11-28 22:55:15
When using some framework/api, sometimes it's pretty unclear if you must call base.Method if you override it, for example you can be pretty sure that you should call base.Maethod() when you are overriding event invocater, to propagate the event, in other situations it can be not so clear especially when there is no source code available, and no comments. I wounder how other programmers decide should they call base method or not in this situation, and if you are about to write some framework how to inform other programmers that you expect base method to be called or not in virtual members. Alex

Override == operator in Ruby

不羁岁月 提交于 2019-11-28 22:31:47
According to the docs , Array.include? uses the == comparison on objects. I come from Java where such things are (usually) done with .equals() which is easy to override for a particular object. How can I override == in Ruby to allow me to specify the behaviour of Array.include? for my particular object? Many thanks. In Ruby == is just a method (with some syntax sugar on top allowing you to write foo == bar instead of foo.==(bar) ) and you override == just like you would any other method: class MyClass def ==(other_object) # return true if self is equal to other_object, false otherwise end end

c++ multiple definitions of operator<<

梦想的初衷 提交于 2019-11-28 22:21:53
问题 I am attempting to override the << operator for a class. The purpose is basically to implement a toString() like behavior for my class, so that sending it to cout will produce useful output. Using a dummy example, I have the code below. When I attempt to compile, I get the foollowing error: $ g++ main.cpp Rectangle.cpp /tmp/ccWs2n6V.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)': Rectangle.cpp:(.text+0x0): multiple definition of `operator<<

How to “properly” override a base class method?

北城以北 提交于 2019-11-28 22:09:30
问题 Whenever i override a method of a base class, other than my implementation of this method, i seem to have 3 choices. 1) Call base.Method(), and then provide my implementation. 2) Provide my implementation and then call base.Method() 3) Just provide my implementation. Recently while using a library i have realized few bugs that were introduced because of not implementing the method as expected by the library. I am not sure if that is bad on part of library, or something wrong in my

Create a custom DNS error page

流过昼夜 提交于 2019-11-28 22:07:55
I am building a new extension and I would like to customize the default error page in Google Chrome. I have gone through the "Override Pages" documentation here but have yet to find anything about customizing the page I have specified. Any suggestions are much appreciated. Thank you. The error page I want to customize is: This webpage is not available The server at _ ___ can't be found, because the DNS lookup failed. DNS is the network service that translates a website's name to its Internet address. This error is most often caused by having no connection to the Internet or a misconfigured

Overriding ToString() of List<MyClass>

烈酒焚心 提交于 2019-11-28 21:17:06
I have a class MyClass, and I would like to override the method ToString() of instances of List: class MyClass { public string Property1 { get; set; } public int Property2 { get; set; } /* ... */ public override string ToString() { return Property1.ToString() + "-" + Property2.ToString(); } } I would like to have the following: var list = new List<MyClass> { new MyClass { Property1 = "A", Property2 = 1 }, new MyClass { Property1 = "Z", Property2 = 2 }, }; Console.WriteLine(list.ToString()); /* prints: A-1,Z-2 */ Is it possible to do so? Or I would have to subclass List<MyClass> to override the

Overriding GetHashCode [duplicate]

穿精又带淫゛_ 提交于 2019-11-28 21:07:43
This question already has an answer here: What is the best algorithm for an overridden System.Object.GetHashCode? 19 answers As you know, GetHashCode returns a semi-unique value that can be used to identify an object instance in a collection. As a good practice, it is recommended to override this method and implement your own. My question is - do you override this method when working on custom objects? If so, what algorithm do you use to generate the unique ID? I was thinking about generating a GUID and then getting integer data from that identificator. When you override GetHashCode() you also

Python Method overriding, does signature matter?

穿精又带淫゛_ 提交于 2019-11-28 21:02:47
Lets say I have class Super(): def method1(): pass class Sub(Super): def method1(param1, param2, param3): stuff Is this correct? Will calls to method1 always go to the sub class? My plan is to have 2 sub classes each override method1 with different params Python will allow this, but if method1() is intended to be executed from external code then you may want to reconsider this, as it violates LSP and so won't always work properly. Python permits "overriding" methods with different signatures but you can never have two methods with the same name in the same class even if their signatures are

Overriding a static method in python

♀尐吖头ヾ 提交于 2019-11-28 21:02:19
Referring to the first answer about python's bound and unbound methods here, I have a question: class Test: def method_one(self): print "Called method_one" @staticmethod def method_two(): print "Called method_two" @staticmethod def method_three(): Test.method_two() class T2(Test): @staticmethod def method_two(): print "T2" a_test = Test() a_test.method_one() a_test.method_two() a_test.method_three() b_test = T2() b_test.method_three() produces output: Called method_one Called method_two Called method_two Called method_two Is there a way to override a static method in python? I expected b_test