天道酬勤,学无止境

subclass

Is there any technical difference between a subclass and an inherited class?

Is there any technical difference between a class which inherits from another class and a class which is a subclass of it? What's the difference between A and B in the following code: A) public class foo { ... private class bar {...} } B) public class foo { ...} private class bar extends foo {...}

2021-06-15 10:56:31    分类:问答    oop   class   subclass   inheritance

Cloning subclasses in Java

I need to clone a subclass in Java, but at the point in the code where this happens, I won't know the subclass type, only the super class. What's the best design pattern for doing this? Example: class Foo { String myFoo; public Foo(){} public Foo(Foo old) { this.myFoo = old.myFoo; } } class Bar extends Foo { String myBar; public Bar(){} public Bar(Bar old) { super(old); // copies myFoo this.myBar = old.myBar; } } class Copier { Foo foo; public Foo makeCopy(Foo oldFoo) { // this doesn't work if oldFoo is actually an // instance of Bar, because myBar is not copied Foo newFoo = new Foo(oldFoo)

2021-06-15 05:59:31    分类:问答    java   clone   subclass

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

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?

2021-06-14 10:17:53    分类:问答    typescript   static   subclass

Python: how to override type hint on an instance attribute in a subclass?

Before you dive in, here is my question: how can I use type hints in a subclass to specify a different type on an instance attribute? If you are unclear on what that means, read below, where I have drawn up an example to clarify things. Full Explanation I have an abstract class Foo, and a subclass of Foo called SubclassOfFoo. Foo has an abstract method get_something that returns an object of type Something. Something has a subclass called SubclassOfSomething. SubclassOfSomething has an additional method something_special. SubclassOfFoo overrides get_something to return an object of type

2021-06-14 06:08:10    分类:问答    python   pycharm   subclass   type-hinting   typing

Python typing for a subclass of list

I want to be able to define what the contents of a subclass of list have to be. The class would look like the following. class A(list): def __init__(self): list.__init__(self) I want to include typing such that the following would happen. import typing class A(list: typing.List[str]): # Maybe something like this def __init__(self): list.__init__(self) >> a = A() >> a.append("a") # No typing error >> a.append(1) # Typing error

2021-06-14 00:55:32    分类:问答    python   python-3.x   list   subclass   python-typing

继承中的Java内存使用(Java memory usage in inheritance)

问题 扩展基类时,Java 中的内存使用情况如何。 子类是否包含基类的实例(具有自己的开销和所有开销)还是仅具有 16 字节的开销? class Foo { int x; } class Bar extends Foo { int y; } 那么,更具体地说,一个 Bar 实例的内存使用情况是多少? 是Foo (including overhead) + Bar(including overhead) 或只是Foo (excluding overhead + Bar(including overhead) 回答1 没有双重开销。 Java 将获取类、超类,计算所有字段所需的空间,并为单个实例分配所需的空间。 仅从内存的角度来看,根本不存在超类的概念,有Foo的实例只需要一个int的内存,还有Bar的实例需要两个int的内存,其中一个是因为Bar碰巧扩展了 Foo。 所以开销(或簿记或任何你想调用的)只发生一次。 但是,在使用 Java 进行开发时,通常最好不要过多关心内存内容,除非您有非常具体(我的意思是非常非常非常具体)的用例,在这些用例上,簿记开销会导致您出现严重问题。 在这种情况下,还应考虑 8 字​​节填充。 通常,除了不用担心每个单个实例的内存开销之外,还有许多其他方法可以改善应用程序的内存占用量或整体性能。 回答2 每个对象只有一个类头,所以只有后者。 顺便说一句

2021-06-12 13:48:34    分类:技术分享    java   inheritance   memory   subclass

How to subclass int and make it mutable

Is it possible to subclass int and make it mutable? Consider the following class: class CustomBitVector(int): # bit 7 @property def seventh_property(self): return bool(self & (1 << 7)) @seventh_property.setter def seventh_property(self, val): self |= bool(val) << 7 # bit 6 @property def sixth_property(self): return bool(self & (1 << 6)) @sixth_property.setter def sixth_property(self, val): self |= bool(val) << 6 # ... a few more of these ... # bit 0 @property def zeroth_property(self): return bool(self & (1 << 0)) @zeroth_property.setter def zeroth_property(self, val): self |= bool(val) << 0 I

2021-06-12 10:58:59    分类:问答    python   subclass   immutability

使用 Swift 创建 SKSpriteNode 子类(Make SKSpriteNode subclass using Swift)

问题 我正在尝试创建类,它是 SKSpriteNode 的子类,我想向它添加其他属性和函数。 但是在第一步中,我遇到了错误。 这是我的代码: import SpriteKit class Ball: SKSpriteNode { init() { super.init(imageNamed: "ball") } } 不是编译错误,是运行时错误。 它说: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)和fatal error: use of unimplemented initializer 'init(texture:)' for class Project.Ball 。 我该如何解决? 回答1 您必须为SKSpriteNode调用指定的初始化SKSpriteNode 。 我真的很惊讶你没有收到关于没有完全实现SKSpriteNode另一个错误,你可能使用的是旧的 Xcode6 beta 吗? 由于必须使用指定的初始化程序,因此需要调用super.init(texture: '', color: '', size: '') 。 它会是这样的: class Ball: SKSpriteNode { init() { let texture = SKTexture(imageNamed: "ball")

2021-06-12 09:57:30    分类:技术分享    ios   swift   sprite-kit   subclass   skspritenode

Swift 子类化 - 如何覆盖 Init()(Swift subclassing - how to override Init())

问题 我有以下类,带有 init 方法: class user { var name:String var address:String init(nm: String, ad: String) { name = nm address = ad } } 我正在尝试对此类进行子类化,但在super.init()部分不断出现错误: class registeredUser : user { var numberPriorVisits: Int // This is where things start to go wrong - as soon as I type 'init' it // wants to autocomplete it for me with all of the superclass' arguments, // and I'm not sure if those should go in there or not: init(nm: String, ad: String) { // And here I get errors: super.init(nm: String, ad: String) // etc.... Apple 的 iBook 有子类化的例子,但没有那些具有带有任何实际参数的init()方法的特征类。 他们所有的 init 都没有参数。 那么

2021-06-12 05:57:27    分类:技术分享    initialization   swift   subclass   superclass

如何在 ASP.NET 页面上使用子类控件?(How to use a Subclassed Control on an ASP.NET Page?)

问题 我已经将DropDownList子类化以添加特定于我的应用程序的功能: public class MyDropDownList : DropDownList { ... } ...然后在Web.Config引用它,这是我认为事情开始出错的地方: <pages theme="Main"> <controls> <add tagPrefix="bob" tagName="MyDropDownList" src="~/Components/MyDropDownList.cs" /> </controls> </pages> 我对它的引用不起作用: <tr><td>Category</td> <td><bob:MyDropDownList runat="server" ID="Category"... /> 我最好的线索是编译器错误消息: "The file 'src' is not a valid [sic] here because it doesn't expose a type." 我想我在这里误用了如何创建 Web 用户控件的知识。 我想要做的是在 ASP.NET 页面上引用这个控件,就像我在父DropDownList 。 重构回包含DropDownList的 Web 用户控件是不可取的,因为我想对其应用RequiredFieldValidator 。 回答1 <pages

2021-06-12 05:01:37    分类:技术分享    c#   asp.net   user-controls   subclass