spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Using Polymorphism with incompatible signatures


Your person objects are instances of Types which just happen to inherit from some the same type. As an instance of Person1, the P1 object is not going to have any knowledge of methods contained in Person2 because it is a different Type.

I am not quite sure what you are after, but here is one way to construct it:

Public MustInherit Class Person
    Public Property Name As String

    Public Sub New(n As String)
        Name = n
    End Sub

    ' "I dont care HOW they do it, but child types

    '    must provide a Greeting() function"
    MustOverride Function Greeting() As String

    ' overload: takes an arg/different sig
    Public Overridable Function
Greeting(strGreeting As String) As String
        Return String.Format("Greetings, {0} {1}",
strGreeting, Name)
    End Function

End Class

Public Class Person1
    Inherits Person

    Public Sub New(n As String)
        MyBase.New(n)
    End Sub

    ' override with a specific form of address
    Public Overrides Function Greeting() As String
        Return "Hello, Mr " & Name
    End Function

End Class

Public Class Person2
    Inherits Person

    Public Sub New(n As String)
        MyBase.New(n)
    End Sub

    Public Overrides Function Greeting() As String
        Return "Hello, Senorita " & Name
    End Function

End Class

Then:

Dim Pa1 As New Person1("Ziggy")
Dim Pa2 As New Person2("Zoey")

Console.WriteLine(Pa1.Greeting)
Console.WriteLine(Pa2.Greeting("Ms"))   ' uses
base class overload
Console.WriteLine(Pa2.Greeting())

Output:

Hello, Mr Ziggy                ' from
P1
Greetings, Ms Zoey             ' from base class
Hello, Senorita Zoey           ' from P2 

The base class more commonly might define a default Greeting for all the inherited types, then some base classes might override it and others not depending on the class needed.


Categories : Vb.Net

Related to : Using Polymorphism with incompatible signatures
When are type signatures necessary in Haskell?
Polymorphic recursion needs type annotations, in general. f :: (a -> a) -> (a -> b) -> Int -> a -> b f f1 g n x = if n == (0 :: Int) then g x else f f1 (z h -> g (h z)) (n-1) x f1 (Credit: Patrick Cousot) Note how the recursive call looks badly typed (!): it calls itself with five arguments, despite f having only four! Then remember that b can be instantiated

Categories : Haskell
Why the difference between type signatures of the same F# function in module vs class?
I can only think of a few times you'd ever need to worry about the distinction between A -> B and (A -> B) (see the F# spec's section on signature conformance for related commentary): When you want to implement a module signature, only a syntactic function can serve as the implementation of something with the signature A -> B, while either a syntactic function or any other function valu

Categories : F#
Reuse same function body for multiple type signatures
One approach is to use a (polymorphic) helper function, e.g.: helper ctor xs rest = ctor [ head xs ] : rest test :: [Block] -> [Block] test ((Para xs):rest) = helper Para xs rest test ((Plain xs):rest) = helper Plain xs rest test bs = bs Note that Para and Plain are just functions of type [whatever] -> Block.

Categories : Haskell
Polymorphism in C++ vs Java
Daughter's constructor invokes the Mother constructor, which invokes maker(). In C++, at least, the object is only considered to be a Mother at this point as the Daughter construction is incomplete. Thus Mother::maker() is invoked, so this is doing the correct thing. However, it is generally considered a strong code smell to invoke virtual functions during construction - for exactly these sorts of

Categories : Java
Polymorphism Python
self.work is a function, thus your behaviour. In the work functions, instead of doing a print, use return: def work(self): return self.name + " makes food") And then, you can use return "Name is {0} and salary is{1} and work is {2}".format(self.name,self.salary,self.work()) (note the () at the end, self.work(). You will call the function)

Categories : Python
Recently Add
Streamwriter FilleSystem TextFile vb.net
How to add multiple rows
Backgroundworker doesn't move files to correct directories vb.net
XAML child window subclass: "InitializeComponent shadows member in base class." How can I resolve this warning without modifying autogenerated code?
Create and remove controls dynamically
Visual Basic Text Box Array for random number generator - For Homework
How to catch cellmouseclicks in dynamically created datagridviews on TabPages
How to Avoid RunTime Error in List(Of Enum(asByte)).AddRange(List(of Byte))
Using Dynamic Linq to set column name
Set Crystal Reports Jet database password programmatically
resize control.size width designer in vb.net winform ,but this action affected other control size
Overflow Exceptions was unhandled
Read String From Serial port Visual Basic
How to high light rows in list control in vb.net
How Can i know if a string is on MD5 format
activate windows application always
Select item from drop down list using text
Paste text into vb label on formload
vb.net combobox hide item
Deleting record from query table composed of other table's columns(?)
Multithreading - Waiting for threads
Vb.net checkbox won't load from text file
Calculating the value with currency symbols
VB.NET Make Windows shortcuts to different forms in VB.NET program
Visual Basic 2008, Random sting generator with custom length, how to make it?
LINQ - SELECT certain columns
linq any function vb.net
Why am i not receiving serial data in visual studios but i am in putty?
Search a listbox, results will not highlight when searching results found
NullReferenceException using StreamReader in vb
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.