superclass

Subclasses and Superclasses

做~自己de王妃 提交于 2019-12-11 08:25:31
问题 I'm trying to build a program that has certain requirements, the main being I have a class, and then make a subclass that adds a feature. I create the class DVD, and then I create the subclass. I'm adding a method to add the year to the list, as well as a restocking fee which will be added to the final inventory value that prints. I built the subclass, created the overriding methods, but it is not being added to the output displayed. Not only that, but it is placing the input year in the

Casting an object of a superclass into an object of a subclass creates a new object?

只愿长相守 提交于 2019-12-11 04:47:24
问题 Suppose I have a superclass Item and a subclass MovingItem. If I create an array of items and then try to cast one of the already created items into a MovingItem and store it in to a vector, does it mean I use a reference or I create a new object, for instance. Item[] itms = new Item[2]; itms[0] = new Item(); itms[1] = new Item(); Vector<MovingItem> movingItms = new Vector<MovingItem>(); movingItms.add((MovingItem) itms[0]); What happens when I cast the object of type Itm found in array itms

Checking programmatically if a .class file extends particular class

最后都变了- 提交于 2019-12-11 01:59:25
问题 I have a problem I've been trying to solve for quite some hours. In an Eclipse plugin, I have an ArrayList that contains the full paths (as strings) of some java .class files. What I'd like to do is check if the classes that are included in the list extend a particular class. I thought about parsing the file, looking for what is after "extends," however we are talking about a .class file and not of a .java file. Any other ideas? Thanks! 回答1: Use an URLClassLoader to load the file (Problem:

Making a variable final and static in one subclass, while keeping it variable in others in Java

你离开我真会死。 提交于 2019-12-10 23:52:07
问题 I have a superclass and two subclasses that extend it. I'd like to initialize a variable called radius which can be set freely in subclass A, but is always the same for subclass B. I could initialize the variable every time I create an object B, but I was wondering if it's possible to make the variable final and static in subclass B, while still being able to implement a getter in my superclass. I might implement more subclasses which have a radius that's either final or variable. If not I'm

Object has null values when subclass attempts to use it. Why?

自古美人都是妖i 提交于 2019-12-10 22:46:27
问题 I'm a newbie Java guy so I'm probably doing this entire thing completely wrong. I have to do this giant project for software engineering class. The code is about 2,000 lines long so this is skeleton code public class BookRental extends JFrame{ public Client currentClient = new Client(); // creating client object //rest of declared variables. public class Client{ //Client class containing all get/set methods for each variable private username; private void setUsername(String u){ username = u;

Get instance of subclass with superclass static method

穿精又带淫゛_ 提交于 2019-12-10 17:45:25
问题 I have a superclass that I would like to forward a static method called getInstance() to all subclasses. When creating an instance of a subclass, I then register the instance in the superclass (perhaps using a hashtable, where the key is based on getClass() ). Then, I wish to use the aforementioned static method ( getInstance ) where the superclass method will return the instance of the correct type. For example, I have a superclass A, and a subclass B extends A. I want to write a static

Getting all superclasses in python 3

旧街凉风 提交于 2019-12-10 17:21:53
问题 How I can get a list with all superclasses of given class in python? I know, there is a __subclasses__() method in inspent module for getting all subclasses, but I don't know any similar method for getting superclasses. Can you help me? 回答1: Use the mro method: >>> class A(object): pass >>> class B(object): pass >>> class C(A, B): pass >>> C.mro() [<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <type 'object'>] 回答2: Here's 2 methods which work both for python 2 and 3.

Smalltalk superclass vs metaclass?

梦想与她 提交于 2019-12-10 13:05:59
问题 I new to OOP, but with a "procedural" background. I'm currently trying to get my head around OOP via GNU Smalltalk and Lovejoy's "Smalltalk: Getting The Message". I'm confused as to the the heck the metaclass and Metaclass class are, vs superclass. I can see the inheritance flow of superclass -> class -> subclass; but I don't see how/where metaclass fits in. TIA... 回答1: There is an excellent description in the free online book Pharo by Example, Chapter 13 (Classes and metaclasses). The things

Getting clone of superclass

蓝咒 提交于 2019-12-10 12:01:33
问题 Here is my playground snippet: class Box { func clone() -> Box { return Box() // <- how to return superclass here? } } class VBox:Box { } let vb = VBox() let cBox = vb.clone() // now cBox is a Box, not a VBox My clone function returns a Box class in all cases. But for the subclass I want it to return the superclass (so above it should return VBox ). I know I could override the clone function inside VBox but I wonder if there's a smarter way. 回答1: You mean subclass. Box is the superclass,

Calling overridden methods in JavaScript

老子叫甜甜 提交于 2019-12-10 11:22:18
问题 i tried to find a way to call overridden method of superclass in JS and got this. function A() { this.superclass = new Array(A.prototype); } A.prototype.call_method = function(method, args, pos) { if (!(pos >= 0)) pos = this.superclass.length - 1; while (!this.superclass[pos].hasOwnProperty(method)) if ((--pos) < 0) return; if (this.superclass[pos][method]) this.superclass[pos][method].apply(this, args); if (pos) this.call_method(method, args, pos - 1); } A.prototype.add = function() { this