方法的重写Overriding和重载Overloading是Java多态性的不同表现。重写Overriding是父类与子类之间多态性的一种表现,重载Overloading是一个类中多态性的一种表现。如果在子类中定义某方法与其父类有相同的名称和参数,我们说该方法被重写 (Overriding)。子类的对象使用这个方法时,将调用子类中的定义,对它而言,父类中的定义如同被“屏蔽”了。如果在一个类中定义了多个同名的方法,它们或有不同的参数个数或有不同的参数类型,则称为方法的重载(Overloading)。Overloaded的方法是可以改变返回值的类型。
Overload是重载,它用与现有成员相同的名称来声明属性或方法,但参数列表与原始成员不同。
Override 主要用于父类和子类之间的方法重写,即指定属性或方法可以在派生类中重写,其参数列表要求相同。
GobjectClass定义了一些Method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | static void g_object_do_class_init (GObjectClass * class ) { /* read the comment about typedef struct CArray; on why not to change this quark */ quark_closure_array = g_quark_from_static_string ( "GObject-closure-array" ); quark_weak_refs = g_quark_from_static_string ( "GObject-weak-references" ); quark_toggle_refs = g_quark_from_static_string ( "GObject-toggle-references" ); pspec_pool = g_param_spec_pool_new (TRUE); property_notify_context.quark_notify_queue = g_quark_from_static_string ( "GObject-notify-queue" ); property_notify_context.dispatcher = g_object_notify_dispatcher; class ->constructor = g_object_constructor; class ->constructed = g_object_constructed; class ->set_property = g_object_do_set_property; class ->get_property = g_object_do_get_property; class ->dispose = g_object_real_dispose; class ->finalize = g_object_finalize; class ->dispatch_properties_changed = g_object_dispatch_properties_changed; class ->notify = NULL; |
子类可以重写(Override)父类的一些API:
比如:
GObject
|_____GstObject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | static void gst_object_class_init (GstObjectClass * klass) { GObjectClass *gobject_class = G_OBJECT_CLASS (klass); //..... //..... gobject_class->set_property = gst_object_set_property; gobject_class->get_property = gst_object_get_property; //.... /* see the comments at gst_object_dispatch_properties_changed */ gobject_class->dispatch_properties_changed = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed); gobject_class->dispose = gst_object_dispose; gobject_class->finalize = gst_object_finalize; |
这一句比较重要:GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
然后override了5个Method:
gobject_class->set_property = gst_object_set_property;
gobject_class->get_property = gst_object_get_property;
gobject_class->dispatch_properties_changed = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
gobject_class->dispose = gst_object_dispose;
gobject_class->finalize = gst_object_finalize;
再如:
GObject
|_____GstObject
|______GstElement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | static void gst_element_class_init (GstElementClass * klass) { GObjectClass *gobject_class = (GObjectClass *) klass; //..... //..... parent_class = g_type_class_peek_parent (klass); //..... //..... gobject_class->dispose = gst_element_dispose; gobject_class->finalize = gst_element_finalize; } |
GObject
|_____GstObject
|______GstElement
|_____GstBin
1 2 3 4 5 6 7 8 9 10 11 12 13 | static void gst_bin_class_init (GstBinClass * klass) { GObjectClass *gobject_class = (GObjectClass *) klass; gobject_class->set_property = gst_bin_set_property; gobject_class->get_property = gst_bin_get_property; gobject_class->dispose = gst_bin_dispose; } |
GObject
|_____GstObject
|______GstElement
|_____GstBin
|______GstPipeline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | static void gst_pipeline_class_init (GstPipelineClass * klass) { GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass); GstBinClass *gstbin_class = GST_BIN_CLASS (klass); g_type_class_add_private (klass, sizeof (GstPipelinePrivate)); gobject_class->set_property = gst_pipeline_set_property; gobject_class->get_property = gst_pipeline_get_property; /** * GstPipeline:delay * * The expected delay needed for elements to spin up to the * PLAYING state expressed in nanoseconds. * see gst_pipeline_set_delay() for more information on this option. * * Since: 0.10.5 **/ g_object_class_install_property (gobject_class, PROP_DELAY, g_param_spec_uint64 ( "delay" , "Delay" , "Expected delay needed for elements " "to spin up to PLAYING in nanoseconds" , 0, G_MAXUINT64, DEFAULT_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); /** * GstPipeline:auto-flush-bus: * * Whether or not to automatically flush all messages on the * pipeline's bus when going from READY to NULL state. Please see * gst_pipeline_set_auto_flush_bus() for more information on this option. * * Since: 0.10.4 **/ g_object_class_install_property (gobject_class, PROP_AUTO_FLUSH_BUS, g_param_spec_boolean ( "auto-flush-bus" , "Auto Flush Bus" , "Whether to automatically flush the pipeline's bus when going " "from READY into NULL state" , DEFAULT_AUTO_FLUSH_BUS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); gobject_class->dispose = gst_pipeline_dispose; |
per-object ——逐对象
2025年3月25日 18:43
I love this blog, which is why I keep coming back. 바카라사이트