inheritance

How do you pull specific objects from an arraylist?

∥☆過路亽.° 提交于 2020-01-16 18:35:10
问题 I've made an Animal superclass, Shark and Whale subclasses. What would I use to print out just the Shark objects from this arraylist? Driver: import java.util.ArrayList; public class Creator { public static void main(String[] args){ ArrayList<Animal> obj = new ArrayList<Animal>(); obj.add(new Shark("James")); obj.add(new Shark("Mike")); obj.add(new Whale("Steve")); obj.add(new Whale("Tommy")); for (Animal a: obj){ System.out.println(a.getName()); } } } 回答1: You can use the instanceof to check

Polymorphism misunderstanding / redefined virtual function not working

偶尔善良 提交于 2020-01-16 15:45:32
问题 I think I should start by simplifying my class structure so I can better explain my problem, which I suspect might just be a misunderstanding of the use of virtual. I have: class Controller{ .. virtual void InitialiseController(){ //std::cout confirms this is running } .. } class AIController : public Controller{ .. virtual void InitialiseController(){ //some logic here } .. } class ComController : public AIController{ .. virtual void InitialiseController(){ //actually the same logic as

Polymorphism misunderstanding / redefined virtual function not working

∥☆過路亽.° 提交于 2020-01-16 15:43:51
问题 I think I should start by simplifying my class structure so I can better explain my problem, which I suspect might just be a misunderstanding of the use of virtual. I have: class Controller{ .. virtual void InitialiseController(){ //std::cout confirms this is running } .. } class AIController : public Controller{ .. virtual void InitialiseController(){ //some logic here } .. } class ComController : public AIController{ .. virtual void InitialiseController(){ //actually the same logic as

How to properly use protected in singly linked list

核能气质少年 提交于 2020-01-16 14:10:08
问题 I've successfully programmed a singly linked list by the following program: The header file is: #ifndef SLL_H_ #define SLL_H_ #include <iostream> class node { protected: public: int key; node *next; node(); ~node(); }; class SLL : public node{ private: node *Head = NULL; int SLL_SIZE = 0; public: //Constructor SLL(); //SLL(int n); //Destructor ~SLL(); //Modifiers void Push_Front(int a); void Push_Back(SLL A,int b); void Traverse(); //Access function int SLL_size(); int Get(node* p); /

Extend already defined class in JavaScript

蓝咒 提交于 2020-01-16 07:03:10
问题 The traditional way of extending classes in JS is something like this: // define constructor function function Fuu(){} // extend prototype and override prototype's constructor Fuu.prototype = Object.create(OtherClass.prototype, { constructor: { value: Fuu, enumerable: false, writable: true, configurable: true } }); Then you add the methods you want to the prototype Fuu.prototype.method = function() {} And just like that you have a function extending another. A nice example of inheritance in

Order of initialization/instantiation of class variables of derived class and invoking of base class constructor

醉酒当歌 提交于 2020-01-16 05:34:05
问题 I want to figure out the order of 1) initialization/instatiation of derived class variables 2) invoking of base class constructor in this code snippet public class base { int y = 1; public base() { y = 2; function(); } void function () { System.out.println("In base Value = " + String.valueOf(y)); } public static class derived extends base { int y = 3; public derived() { function(); } void function () { System.out.println("In derived Value = " + String.valueOf(y)); } } public static void main

Are friend functions inherited? and why would a base class FRIEND function work on a derived class object?

ぐ巨炮叔叔 提交于 2020-01-16 05:26:27
问题 class baseClass { public: friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; } baseClass(int x) : baseInt(x) {} private: int baseInt; }; class derivedClass : public baseClass { public: derivedClass(int x, int y) : baseClass(x), derivedInt(y) {} private: int derivedInt; }; in the function friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; } I don't understand why would the friend function of the base class work for the derived class? should not passing derived

Web API 2.2, Inherited Controller with Route Overrides (Is this possible)?

瘦欲@ 提交于 2020-01-16 05:17:30
问题 So I have a generic base controller implementing CRUD, which derives from APIController . public class GenericController<TEntity> : ApiController where TEntity : class { private readonly GenericModel<TEntity> _model; public IModel Model { get { return _model; } } public GenericController(IGenericModel<TEntity> model) { _model = (GenericModel<TEntity>)model; } [HttpPost, Route] public IHttpActionResult Post(TEntity entity) { TEntity newEntity = _model.Create(entity); String urlLink = $"

subclass properties accessed in generic method with superclass input

荒凉一梦 提交于 2020-01-16 05:16:52
问题 I want to make an inventory/equipment system to interface with my item system. My item system is an inherited class system based around shared properties. I store types in enums and give the items an enum value. My inheritance structure: Item { Equippable { Armor {Chest, Head} Weapon {Melee, Ranged} } } I have each subType define an enum variable in its parent class, so Chest would define the armorTypes enum in an explicit base constructor call with the type. e.g. public enum armorTypes

Why can't I have an private abstract method?

倖福魔咒の 提交于 2020-01-16 04:49:20
问题 Let's say I have my classic: public abstract class Mammal { private int numLegs; private String voice; private Coat coat; public abstract void eat(); public abstract void speak(); public abstract void sleep(); private abstract void ingestFood(Food f); //The abstract method ingestFood in type Mammal can only set a visibility modifier, one of public or protected } With these concrete implementations: public class Dog extends Mammal { private int numLegs = 4; private String voice = "Woof!";