overriding

Why is my superclass calling my subclass method?

 ̄綄美尐妖づ 提交于 2021-02-20 19:33:09
问题 When I call a method that was overrided from my constructor, I am getting an error and it says that it is missing an argument (due to the subclass requiring a second argument). However, I called the method in the super(), so why doesn't it call the super class version of that method? This is best illustrated with a short example: class A: def __init__(self): self.do() def do(self): print("A") class B(A): def __init__(self): super().__init__() self.do("B") def do(self, arg2): super().do()

can i override a method with a derived class as its parameter in c#

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-17 06:12:33
问题 I have a question about override in c#. Why I can't override a method with a derived class as its parameter in c#? like this: class BaseClass { } class ChildClass:BaseClass { } abstract class Class1 { public virtual void Method(BaseClass e) { } } abstract class Class2:Class1 { public override void Method(ChildClass e) { } } 回答1: Because of type invariance/contravariance Here's a simple thought experiment, using your class definitions: Class1 foo = new Class1(); foo.Method( new BaseClass() );

How to fix the error: " AlphaSorter must implement the inherited abstract method java.lang.Comparable<AlphaSorter>.compareTo(AlphaSorter)?

时光总嘲笑我的痴心妄想 提交于 2021-02-16 15:39:05
问题 I have been asking questions about creating a sorting method to sort a linked list of contact names read in from a text file and have advanced from my previous question:(What is a better method to sort strings alphabetically in a linked list that is reading in lines from a text file?), but am now running into two errors after creating a separate class called AlphaSorter.java with the method compareTo() which is intended to override my sort() method in my Linked List program/class called

How to write override methods in Java

半腔热情 提交于 2021-02-11 15:54:54
问题 This is part of a homework assignment. I am trying to make a class named RomanNumeral that has several methods. The first method, RomanNumeral checks whether a string rn is a Roman Numeral or not. The second method converts the Roman numeral into a decimal value. My problem lies with the rest of the methods. I am asked to create an override file for .equals() , .compareTo() and toString() . My question is how would I even begin doing this? What does it mean to override? Am I supposed to look

Scala Traits and Inheritance

大兔子大兔子 提交于 2021-02-11 13:37:55
问题 I have my Scala classes structured as below: trait ActualClass extends ParentClass { override def method1(inputStr:String):String = { "actual "+ inputStr } def execute():String = { this.method1("test") } } trait WithClass extends ParentClass { override def method1(inputStr:String):String = { "with "+ inputStr } } class ParentClass { def method1(inputStr:String):String = { "parent "+ inputStr } } object TestClass extends App{ val actualClass = new ActualClass with WithClass { } println

Avoiding recursion in toString method

 ̄綄美尐妖づ 提交于 2021-02-10 16:14:52
问题 this is my huge mindblowing problem with a Java exercise. So, we've got this: public class CyclicEmployee { private int age; private String name; private CyclicEmployee boss; private List<CyclicEmployee> subordinate } and our goal is to overwrite toString method by cutting fields which may lead to recursive infinity. Finally it's supposed to look like a printed object with a name, age, boss and subordinate. Employee[age=30,name='Mike',boss=Employee[age=45,name='Ann'], subordinate=[Employee

How to check if string exists in Enum of strings?

给你一囗甜甜゛ 提交于 2021-02-10 07:10:49
问题 I have created the following Enum: from enum import Enum class Action(str, Enum): NEW_CUSTOMER = "new_customer" LOGIN = "login" BLOCK = "block" I have inherited from str , too, so that I can do things such as: action = "new_customer" ... if action == Action.NEW_CUSTOMER: ... I would now like to be able to check if a string is in this Enum, such as: if "new_customer" in Action: .... I have tried adding the following method to the class: def __contains__(self, item): return item in [i for i in

How to check if string exists in Enum of strings?

风格不统一 提交于 2021-02-10 07:10:15
问题 I have created the following Enum: from enum import Enum class Action(str, Enum): NEW_CUSTOMER = "new_customer" LOGIN = "login" BLOCK = "block" I have inherited from str , too, so that I can do things such as: action = "new_customer" ... if action == Action.NEW_CUSTOMER: ... I would now like to be able to check if a string is in this Enum, such as: if "new_customer" in Action: .... I have tried adding the following method to the class: def __contains__(self, item): return item in [i for i in

How to check if string exists in Enum of strings?

倖福魔咒の 提交于 2021-02-10 07:05:28
问题 I have created the following Enum: from enum import Enum class Action(str, Enum): NEW_CUSTOMER = "new_customer" LOGIN = "login" BLOCK = "block" I have inherited from str , too, so that I can do things such as: action = "new_customer" ... if action == Action.NEW_CUSTOMER: ... I would now like to be able to check if a string is in this Enum, such as: if "new_customer" in Action: .... I have tried adding the following method to the class: def __contains__(self, item): return item in [i for i in

When I override the “put(K key, V value)” in a TreeMap, how can I change “value”?

99封情书 提交于 2021-02-10 05:01:16
问题 I extend a TreeMap, override "put()", and do something equivalent to: public class MyMap<K, Integer> extends TreeMap<K, Integer> { @Override public Integer put(K key, Integer value) throws ClassCastException, NullPointerException { java.lang.Integer newValue = java.lang.Integer.valueOf(123); super.put(key, newValue); // <--- error message here return newValue; } } error message: no suitable method found for put(K, java.lang.Integer)... java.lang.Integer cannot be converted Integer. I know it