subclass

Nested classes vs Separate class files

别说谁变了你拦得住时间么 提交于 2019-12-01 17:44:38
In java at least, my compiler won't let me put two (or more) separate classes in a single class file. It forces me to create multiple files in the same package. I understand why this is, and I agree with this principle. However, I've realized that I could just as easily nest the classes and contain the entire program (excluding imported libraries) in a single file. I would imagine that this would be frowned upon, but I don't understand why. What are the dangers and disadvantages of nesting class as opposed to the standard system (outside of the severe loss in organization and neatness)? Well,

ArrayList containing different objects of the same superclass - how to access method of a subclass

依然范特西╮ 提交于 2019-12-01 17:16:23
Hi I'm wondering if there is a simple solution to my problem, I have an ArrayList : ArrayList <Animal> animalList = new ArrayList<Animal>(); /* I add some objects from subclasses of Animal */ animalList.add(new Reptile()); animalList.add(new Bird()); animalList.add(new Amphibian()); They all implement a method move() - The Bird flies when move() is called. I know I can access common methods and properties of the super class by using this public void feed(Integer animalIndex) { Animal aAnimal = (Animal) this.animalList.get(animalIndex); aAnimal.eat(); } That's fine - but now I would like to

Nested classes vs Separate class files

折月煮酒 提交于 2019-12-01 17:11:43
问题 In java at least, my compiler won't let me put two (or more) separate classes in a single class file. It forces me to create multiple files in the same package. I understand why this is, and I agree with this principle. However, I've realized that I could just as easily nest the classes and contain the entire program (excluding imported libraries) in a single file. I would imagine that this would be frowned upon, but I don't understand why. What are the dangers and disadvantages of nesting

PHP: call child constructor from static method in parent

早过忘川 提交于 2019-12-01 14:29:42
问题 I want to have a static method in a parent class that creates instances of whatever subclass i call this method on. An example to make this more clear: class parent { public static method make_objects($conditions){ for (...){ // here i want to create an instance // of whatever subclass i am calling make_objects on // based on certain $conditions } } } class sub extends parent{ ... } $objects = sub::make_objects($some_conditions); 回答1: As of php 5.3 you can use the static keyword for this <

Java: Inherited class constructor is calling Super class

穿精又带淫゛_ 提交于 2019-12-01 13:44:08
While creating a java program i encountered a problem, A subclass constructor is throwing an Error by calling the Superclass's method The code is similar to this : class Manage { public static void main(String[] args) { Manager m1 = new Manager ( 35 ); } } class Employee { int emp_id; public Employee(int id) { this.emp_id = id; } public int get_id() { return emp_id; } } class Manager extends Employee { public Manager(int id ) { this.emp_id = id ; } } class Engineer extends Employee { public Engineer(int id) { this.emp_id = id ; } } And the error is something like this : $ javac app.java app

Global variable extend application class

筅森魡賤 提交于 2019-12-01 12:51:37
问题 So I am trying extend the base Application class and add member variables to create global variables like in this first solution of the link below. Android global variable This works if the member variable is a simple data type like a String or a Boolean . But how would you do it for a more complex data type? In my case i would like the member variable to be of type HashMap<String, Boolean> . I am setting three member variables in onActivityResult() (a boolean, a String , and a HashMap<String

Why do Python immutable types (like int, str, or tuple) need to use `__new__()` instead of just `__init__()`?

假如想象 提交于 2019-12-01 11:47:22
This question is related to, but not a duplicate of, this , this , this , and this . Those links don't answer my question here. This though , almost answers my questions but doesn't, because the code in the answer doesn't run in Python 3.6 and in any case the question there isn't specifically about what I'm asking here. (See my own answer below. From the Python documentation page , I find the following text. __new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order

How can “default” values in overridden WinForm controls be prevented?

↘锁芯ラ 提交于 2019-12-01 08:37:31
I'm trying to learn and grasp what and how C# does things. I'm historically a Visual Foxpro (VFP) developer, and somewhat spoiled at the years of visual inheritance by creating my own baseline of user controls to be used application wide. In trying to learn the parallels in C#, I am stuck on something. Say I derive my own label control (control is subclass of label) defined with a font "Arial", 10 point. Then, on any form I add it to, the Designer will automatically pre-fill in some property values which can be seen in the "Designer.cs" portion of the Form class. this.LabelHdr2.AutoSize = true

Typescript: how to return subclass type from inherited static property?

前提是你 提交于 2019-12-01 08:25:57
class BaseDoc { static Collection; static find(){ // which is expected to return an instance of SubDoc when run SubDoc.find() return this.Collection.find(); } } class SubDoc extends BaseDoc {} What I hope is: When running SubDoc.find(), the app knows the type of the return value to be an instance of SubDoc rather than BaseDoc. How can I achieve that? You can create a generic version of find that will have the this parameter inferred to the type of the class it's being called on, and use InstanceType<T> to extract the actual instance type from the class type: class BaseDoc { static Collection:

Redefine *= operator in numpy

浪尽此生 提交于 2019-12-01 07:34:35
As mentioned here and here , this doesn't work anymore in numpy 1.7+ : import numpy A = numpy.array([1, 2, 3, 4], dtype=numpy.int16) B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64) A *= B A workaround is to do: def mult(a,b): numpy.multiply(a, b, out=a, casting="unsafe") def add(a,b): numpy.add(a, b, out=a, casting="unsafe") mult(A,B) but that's way too long to write for each matrix operation! How can override the numpy *= operator to do this by default? Should I subclass something? You can use np.set_numeric_ops to override array arithmetic methods: import numpy as np def unsafe